一、多对一 | 一对一
<resultMap type="com.zktx.platform.entity.tb.Module" id="BaseResultMap">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="class_name" jdbcType="VARCHAR" property="class_name"/>
<result column="description" jdbcType="VARCHAR" property="description"/>
<result column="name" jdbcType="VARCHAR" property="name"/>
<result column="priority" jdbcType="INTEGER" property="priority"/>
<result column="sn" jdbcType="VARCHAR" property="sn"/>
<result column="url" jdbcType="VARCHAR" property="url"/>
<result column="parent_id" jdbcType="INTEGER" property="parent_id"/>
<association column="parent_id" property="parent_Module" select="selectById"></association>
</resultMap>
<select id="queryList" resultMap="BaseResultMap">
select * from tb_module m1 left join tb_module m2 on m1.parent_id =m2.id
</select>
<select id="selectById" parameterType="java.lang.Integer" resultType="com.zktx.platform.entity.tb.Module">
select * from tb_module where id=#{parent_id}
</select>
dao层:
List<Module> queryList();
二、一对多|多对多
xml配置文件
在com.dataManage.mapper.UmTGroupMapper.xml中
<resultMap id="umTGroup" type="com.dataManage.entry.UmTGroup">
<id column="id" property="id"/>
<result column="code" property="code"/>
<result column="name" property="name"/>
<result column="pid" property="pid"/>
<result column="description" property="description"/>
<result column="areaid" property="areaid"/>
<collection property="umTRoles" column="id" fetchType="lazy" select="com.dataManage.mapper.UmTRoleMapper.findUmTRolesByGId"></collection>
</resultMap>
<select id="selectAll" resultMap="umTGroup">
SELECT * FROM um_t_group
</select>
在
com.dataManage.mapper.UmTRoleMapper.xml中:
<select id="findUmTRolesByGId" resultMap="umTRoleMapper">
SELECT * FROM um_t_role r LEFT JOIN um_t_g_r gr ON r.id=gr.rid WHERE gr.gid=#{groupid}
</select>
Dao层
在UmTRoleMapper.java中:
public List<UmTRole> findUmTRolesByGId(@Param("groupid") Integer groupid);
在UmTGroupMapper.java中
public List<UmTGroup> selectAll();