collection和association的使用方法
1. 关联-association
2.集合-collection
用collection的一对多
public class User{
private Card card_one;
private List<Card> card_many;
}
用association的一对一
public class Card implements Serializable{
private Integer id;
private String code;
//省略set和get方法.
}
public class Person implements Serializable{
private Integer id;
private String name;
private String sex;
private Integer age;
//人和身份证是一对一的关系
private Card card;
//省略set/get方法.
}
分页三层结构写法
封装类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RolesPathVo {
private Integer id;
private String name;
private String link;
}
entity层
public class Roles implements Serializable {
@ApiModelProperty(value = "id")
private Integer id;
@ApiModelProperty(value = "名字")
private String name;
@ApiModelProperty(value = "言论")
private String remark;
@ApiModelProperty(value = "状态")
private Integer status;
private List<Menus> menus;
}
mapper层
List<RolesPathVo> findRoles(@Param("start") Integer start, @Param("limit") Integer limit);
service层
List<RolesPathVo> findRoles(Integer page,Integer pageSize);
impl层
@Override
public List<RolesPathVo> findRoles(Integer page, Integer pageSize) {
Integer start = (page - 1) * pageSize;
Integer limit = pageSize;
return rolesMapper.findRoles(start,limit);
}
.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.systop.tanzhen.mapper.RolesMapper">
<resultMap id="selectRolesPath1" type="com.systop.tanzhen.entity.Roles">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<collection property="menus" ofType="com.systop.tanzhen.entity.Menus">
<result property="link" column="link"/>
</collection>
</resultMap>
<!-- 查询用户列表-->
<select id="findRoles" resultType="com.systop.tanzhen.vo.RolesPathVo">
SELECT `roles`.`id`,`roles`.`name`,`menus`.`link` FROM `role_menu` LEFT JOIN `menus` ON `menus`.`id`=`role_menu`.`menu_id` JOIN `roles` ON `role_menu`.`role_id`=`roles`.`id`
<if test="start != null and limit != null">
limit #{start}, #{limit}
</if>
</select>
</mapper>