Java 如何在Mybatis中的一对一嵌套查询中嵌套一对多查询
这是我的SQL和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="
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);