Mybatis多对多查询

在shiro安全管理中,系统的后台管理员与管理员权限是多对多关系

实体类设计:Admin与Authority保留对方的一个集合引用

数据库中:中间表中两个外键指向两张表的主键

mapper文件的映射关系及其查询语句如下:

Mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.baizhi.mapper.AdminMapper">
<resultMap type="Admin" id="au">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="authorityList" ofType="Authority">
<id property="id" column="auid" />
<result property="operate_right" column="operate_right" />
</collection>
</resultMap>
<select id="AdminAuthority" resultMap="au" parameterType="String">
select a.id,a.name,a.duty,aa.*,au.id auid,au.operate_right
from admin a,`admin_ authority` aa,authority au
where a.id=aa.admin_id
and au.id=aa.` authority_id` and a.name=#{name}
</select>
</mapper>

猜你喜欢

转载自www.cnblogs.com/lhc-hhh/p/10229212.html