Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Mybatis中的一对一嵌套查询中嵌套一对多查询_Java_Sql_Mybatis - Fatal编程技术网

Java 如何在Mybatis中的一对一嵌套查询中嵌套一对多查询

Java 如何在Mybatis中的一对一嵌套查询中嵌套一对多查询,java,sql,mybatis,Java,Sql,Mybatis,这是我的SQL和resultmap: <resultMap id="playerMap" type="player"> <id column="playerId" property="id"/> <result column="playerName" property="name"/> <association property="team" javaType="Team"> <id column="

这是我的SQL和
resultmap

 <resultMap id="playerMap" type="player">
    <id column="playerId" property="id"/>
    <result column="playerName" property="name"/>
    <association property="team" javaType="Team">
        <id column="teamId" property="id"/>
        <result column="teamName" property="name"/>
    </association>
</resultMap>
<select id="selectPlayerById" resultMap="playerMap">
    select p.id playerId,p.name playerName,
    t.name teamName,t.id teamId
    from t_player p,t_team t
    where p.tid=t.id and p.id=#{id}
</select>


但“players”列为空。如何更改我的SQL语句,以便本专栏可以查询多个参与者的信息?

首先,您必须弄清楚您想要表示什么样的关系

对我来说,看起来你有一个成员球员的班级球员。那么,一个球员可以有球员吗

如果要选择全部或多个玩家,则必须添加第二个
选择
-元素或将现有的玩家更改为类似的内容:

<select id="selectPlayers" resultMap="playerMap">
    select p.id playerId,p.name playerName,
    t.name teamName,t.id teamId
    from t_player p,t_team t
    where p.tid=t.id
</select>
我们还可以将这两种选择组合为如下内容:

<select id="selectPlayers" resultMap="playerMap">
    select p.id playerId,p.name playerName,
    t.name teamName,t.id teamId
    from t_player p,t_team t
    where p.tid=t.id
</select>
映射器xml中只有一个
选择
-元素

<select id="selectPlayers" resultMap="playerMap">
    select p.id playerId,p.name playerName,
    t.name teamName,t.id teamId
    from t_player p,t_team t
    where p.tid=t.id
    <choose>
        <when test="id != null" >
            and p.id=#{id}
        </when>
        <when test="name != null" >
            and p.name=#{name}
        </when>
        <otherwise>

        </otherwise>
    </choose>
</select>
List<Player> selectPlayers(@Param("id") long id, @Param("name") String name);