mybatis 一对多 分页 <resutMap> 和 <collections>用来聚合查询结果

mybatis 一对多 分页 <resutMap> 和 <collections>用来聚合查询结果
1.mybatis 一对多 resutMap 和 collections用来聚合查询结果-- 针对分页((如果不分页也可以不传递分页参数)) !-- t_service 与 t_service_content 是一对多的关系-- 分页返回对象ListServiceBo resultList serviceMapper.queryList(); ServiceBo包含如下属性Service实体类的所有属性 多方表实体类的集合 private ListServiceContent serviceContentList; !-- com.winner.Service出现1次, com.winner.ServiceContent出现了2次-- 将如下3段粘贴到你的mapper.xml即可 第二个sql片段ofType 和 第三个sql片段 resultType的类型是一样的,都是com.winner.data.entity.vo.ServiceContent, 或都是java.lang.String select idqueryList resultMapServiceMap select ts.*, #{startDate} start_date from t_service ts where type #{type} limit #{startNum}, #{pageSize} /select resultMap idServiceMap typecom.winner.data.entity.vo.Service id columnid propertyid/ result columnuser_name propertyname/ result columnstart_date propertystartDate/ collection propertyserviceContentList 这个填写Service的属性,即包含一个list ofTypecom.winner.data.entity.vo.ServiceContent column{idid,startDatestart_date} 左边是实体类属性右边是数据库列名 selectqueryById id columnservice_id propertyserviceId / result columntitle propertytitle / result columnlanguage propertylanguage / /collection /resultMap select idqueryById resultTypecom.winner.data.entity.vo.ServiceContent select * from t_service_content where date#{startDate} name#{startDate} /select 当 ofTypejava.lang.String 时, id columncategory propertytype / 推荐简化为 id columncategory / 实体类VO用 private ListString typeList;-- 不能分页的噢 -- 使用resultMap接收一对多查询的结果 查询用户信息, 带出角色list -- SysUserResult实体类下有一个 private ListSysRole roles; 属性 select idselectByPage resultMapuserResultMap SELECT u.user_id, u.user_name, r.role_id, r.role_name FROM sys_user u LEFT JOIN sys_user_role ur ON u.user_id ur.user_id LEFT JOIN sys_role r ON r.role_id ur.role_id where u.user_id#{userId} /select resultMap iduserResultMap typecom.winner.SysUserResult id columnid propertyid / result columnname propertyname / collection propertyroles ofTypecom.winner.SysRole javaTypejava.util.List id columnrole_id propertyroleId/ result columnrole_name propertyroleName/ /collection /resultMap2.支持自增主键的表, 插入返回自增主键void insert(入参User user)返回主键user.getId()insert idaddUser useGeneratedKeystrue keyPropertyid /insert3. 不支持自增主键的表,使用selectKey标签void insert(入参User user)返回主键user.getId()select selectKey keyProper tyid orderBEFORE, resultTypejava.lang.String select right(uuid_short(),20) /selectKey insert into tablenameid,namevalues(#{id}, #{name}) /select