dao层:其实在这里无关紧要,是因为xml当中有个namespace用到这个dao路劲来表明唯一才放在这里
package com.rcz.platform.framework.sys.modules.mp.dao.breakdownGroup;
import com.rcz.platform.framework.sys.common.persistence.CrudDao;
import com.rcz.platform.framework.sys.common.persistence.annotation.MyBatisDao;
import com.rcz.platform.framework.sys.modules.mp.entity.breakdownGroup.BreakdownGroup;
import java.util.List;
/**
* 用户分组DAO接口
* @author wyl
* @version 2018-01-24
*/
@MyBatisDao
public interface BreakdownGroupDao extends CrudDao<BreakdownGroup> {
List<BreakdownGroup> getGroupList();
}
实体类:
package com.rcz.platform.framework.sys.modules.mp.entity.breakdownGroup;
import com.google.common.collect.Lists;
import com.rcz.platform.framework.sys.modules.mp.entity.common.BaseUser;
import org.hibernate.validator.constraints.Length;
import java.util.List;
/**
* 用户分组Entity
* @author wyl
* @version 2018-01-24
*/
public class BreakdownGroup {
private static final long serialVersionUID = 1L;
private String id;
private String groupName; // groupname
private List<BaseUser> userList = Lists.newArrayList();
getset........
}
被关联实体类:
package com.rcz.platform.framework.sys.modules.mp.entity.common;
/**
* Created by SkinnyBoy on 2018/1/23.
*/
import com.rcz.platform.framework.sys.modules.sys.entity.Office;
/**
* 用户基础信息类
*/
public class BaseUser {
private String userId; // 用户Id
private String name; // 用户姓名
private String departmentName; // 所在部门
private String mobile; // 手机号码
get set ...
}
这里需要注意一点:baseUser是数据库中不存在的实体类,没有对应表;
是通过 sys_user和sys_office构造出来的一张表
sys_user是真正的用户表,sys_office是部门表,他们是外键相关联的形式
xml:
<resultMap type="com.rcz.platform.framework.sys.modules.mp.entity.breakdownGroup.BreakdownGroup" id="BreakdowngroupMap">
<id property="id" column="id"/>
<result property="groupName" column="groupname"/>
<!-- 一对多的关系 -->
<!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
<collection property="userList" ofType="BaseUser" column="id" select="selectUserList">
<result column="userId" property="userId"/>
<result column="name" property="name"/>
<result column="departmentName" property="departmentName"/>
<result column="mobile" property="mobile"/>
</collection>
</resultMap>
<select id="selectUserList" parameterType="String" resultType="com.rcz.platform.framework.sys.modules.mp.entity.common.BaseUser">
select pu.userid as userid, su.name as name, su.mobile as mobile,o.name as departmentName from pm_breakdowngroupuser pu
left join sys_user su on pu.userid = su.login_name
left join sys_office o on su.office_id = o.id
where pu.groupid = #{id}
</select>
<select id="getGroupList" resultMap="BreakdowngroupMap">
SELECT
id,groupname
FROM pm_breakdowngroup
</select>
</mapper>
在这里需要指出的是mybatis的这种做法会有N+1问题,这里采用了延迟加载的办法,不了解的自行百度学习