mybatis注解模式的多表关联查询问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis注解模式的多表关联查询问题相关的知识,希望对你有一定的参考价值。

按照网上的例子写的,并且将网上的例子练习之后也可以关联查询出。但用到项目中缺关联不起来了。打印sql语句只执行了SELECT role_id roleId,uri,dbid_ from jc_role_permission where role_id=#id``` @Select("SELECT role_id roleId,uri,dbid_ from jc_role_permission where role_id=#id") public List<JcRolePermission> getUserInfoById(@Param("id") Integer id); @Select("select * from jc_role where role_id = #id") @Results( @Result(column="role_id",property="id"), @Result(column="site_id",property="site_id_param"), @Result(column="role_name",property="name"), @Result(column="priority",property="priority"), @Result(column="is_super",property="all"), @Result(column="role_level",property="level"), @Result(property="jcRolePermissionList",column="id", many = @Many(select= "com.company.project.dao.JcRoleMapper.getUserInfoById")), ) JcRole findByIdAndSetPerms(Integer id);```
是只执行了
select * from jc_role where role_id = #id

参考技术A private static void handleException(Exception e)

String msg = null;
if (e instanceof InvocationTargetException)

Throwable targetEx = ((InvocationTargetException) e)
.getTargetException();
if (targetEx != null)
追问

这是干什么

mybatis中的多表查询

1)无延迟加载的一对一关联


<resultMap type="Userbean" id="baseMap">
<id column="userid" property="userid"/>
<result column="username" property="username"/>
<collection property="dep" ofType="Dept">
<id column="did" property="did"/>
<result column="dname" property="dname"/>
</collection>

</resultMap>

<select id="queAll" resultMap="baseMap">
select userid,username,t2.dname dname from t_user t1 inner join t_dept t2 on t1.did = t2.did
</select>

2)无延迟加载的一对多关联

<resultMap type="Userbean" id="baseMap">
<id column="userid" property="userid"/>
<result column="username" property="username"/>
<association property="dep" javaType="Dept">
<id column="did" property="did"/>
<result column="dname" property="dname"/>
</association>
</resultMap>

<select id="queAll1" resultMap="baseMap">
select userid,username,t2.dname dname from t_user t1 inner join t_dept t2 on t1.did = t2.did
</select>

3)有延迟加载的一对多(一对一和一对多差不多)

 

<resultMap type="Userbean" id="baseMap">
<id column="userid" property="userid"/>
<result column="username" property="username"/>
<association property="dep" column="did" select="findDeptByDid" javaType="Dept">
<id column="did" property="did"/>
<result column="dname" property="dname"/>
</association>


<!-- <collection property="dep" column="did" select="findDeptByDid" javaType="Dept">
<id column="did" property="did"/>
<result column="dname" property="dname"/>
</collection> -->
</resultMap>

<select id="findDeptByDid" resultType="Dept" parameterType="int">
select dname,did from t_dept where did=#{did}
</select>


<select id="queAll" resultMap="baseMap">
select userid,username,did from t_user t1

</select>

 

 














































以上是关于mybatis注解模式的多表关联查询问题的主要内容,如果未能解决你的问题,请参考以下文章

关于mybatis+sqlserver 的多表关联的分页(注意要先分页在多表关联)

MyBatis的多表联合查询

mybatis中的多表查询

28.多表查询——跨关联关系的多表查询

28.多表查询——跨关联关系的多表查询

MySQL的多表关联查询