springBoot学习笔记(2.1)—— 整合mybatis之一对一,一对多查询

一、准备工作

上篇文章已经介绍了如何引入jar和整合mybatis此处就不在多做赘述。

1. 创建实体类和表

1.1创建用户类

  1. @Data
  2. public class User {
  3. /**
  4. * 主键id
  5. */
  6. private Long id;
  7. /**
  8. * 用户名
  9. */
  10. private String userName;
  11. /**
  12. * 身份证号码
  13. */
  14. private String idCardNumber;
  15. /**
  16. * 手机号码
  17. */
  18. private String phoneNumber;
  19. /**
  20. * 部门id
  21. */
  22. private Long deptId;
  23. /**
  24. * 部门名称
  25. */
  26. private String deptName;
  27. /**
  28. * 部门类
  29. */
  30. private Dept dept;
  31. }

1.2 创建用户表

  1. CREATE TABLE `user`(
  2. id BIGINT (15) NOT NULL AUTO_INCREMENT COMMENT '主键id',
  3. user_name VARCHAR (50) NOT NULL DEFAULT '' COMMENT '用户名',
  4. idCardNumber VARCHAR (50) NOT NULL DEFAULT '' COMMENT '身份证号码',
  5. phone_number VARCHAR (50) NOT NULL DEFAULT '' COMMENT '手机号码',
  6. dept_id BIGINT (15) NOT NULL DEFAULT -1 COMMENT '部门id',
  7. PRIMARY KEY (id)
  8. )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'user';
  9. alter table user modify id int auto_increment;

注意设置表格为自动增长,不然在后文中插入数据可能会报错。

1.3 创建部门类

  1. @Data
  2. public class Dept {
  3. /**
  4. * 主键id
  5. */
  6. private Long id;
  7. /**
  8. * 部门名称
  9. */
  10. private String deptName;
  11. /**
  12. * 父类id
  13. */
  14. private String parentId;
  15. /**
  16. * 排序码
  17. */
  18. private String orderCode;
  19. /**
  20. * 是否开启使用(0否,1是)
  21. */
  22. private Integer isStart;
  23. /**
  24. * 说明
  25. */
  26. private String description;
  27. /**
  28. * 地址
  29. */
  30. private String address;
  31. /**
  32. * 人员数据
  33. */
  34. private List<User> userList;
  35. /**
  36. * 人员名字集合
  37. */
  38. private List<String> userNameList;
  39. }

1.4 创建部门表

  1. CREATE TABLE dept(
  2. id BIGINT (15) NOT NULL AUTO_INCREMENT COMMENT '主键id',
  3. dept_name VARCHAR (50) NOT NULL DEFAULT '' COMMENT '部门名称',
  4. parent_id VARCHAR (50) NOT NULL DEFAULT '' COMMENT '父类id',
  5. order_code VARCHAR (50) NOT NULL DEFAULT '' COMMENT '排序码',
  6. is_start INT (11) NOT NULL DEFAULT 0 COMMENT '是否开启使用(0否,1是)',
  7. description VARCHAR (50) NOT NULL DEFAULT '' COMMENT '说明',
  8. address VARCHAR (50) NOT NULL DEFAULT '' COMMENT '地址',
  9. PRIMARY KEY (id)
  10. )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'dept';

至于表格数据就不在此处说明,用户可以用自己的数据。

二、代码详情

1. mybatis一对一映射

1.1 Dao接口

  1. @Mapper
  2. public interface UserDao {
  3. /***
  4. * description: 查询用户具体信息,mybatis中一对一样例
  5. * version: 1.0 ->
  6. * date: 2021/12/29 10:32
  7. * author: xiaYZ
  8. * iteration: 迭代说明
  9. * @param userId 用户id
  10. * @return java.lang.String
  11. */
  12. User findUserData(Long userId);
  13. }

1.2 Mapper文件类(重点)

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.example.springbootmybatis.dao.UserDao">
  4. <resultMap id="userMap" type="com.example.springbootmybatis.entity.User">
  5. <id property="id" column="id"/>
  6. <result property="deptId" column="dept_id"/>
  7. <result property="phoneNumber" column="phone_number"/>
  8. <result property="idCardNumber" column="idCardNumber"/>
  9. <result property="userName" column="user_name"/>
  10. <result property="deptName" column="dept_name"/>
  11. <!--mybatis一对一映射-->
  12. <association property="dept" javaType="com.example.springbootmybatis.entity.Dept">
  13. <id property="id" column="dept_id"/>
  14. <result property="deptName" column="dept_name"/>
  15. <result property="address" column="address"/>
  16. <result property="parentId" column="parent_id"/>
  17. <result property="description" column="description"/>
  18. <result property="isStart" column="is_start"/>
  19. <result property="orderCode" column="order_code"/>
  20. </association>
  21. </resultMap>
  22. <select id="findUserData" resultMap="userMap">
  23. select u.id, user_name, idCardNumber, phone_number, dept_id,
  24. dept_name, parent_id, order_code, is_start, description, address
  25. from user u
  26. left join dept d on u.dept_id = d.id
  27. where u.id = #{userId}
  28. </select>

1.3 Controller和Service层代码

  1. @RestController
  2. @RequestMapping("userController")
  3. public class UserController {
  4. @Resource
  5. UserService userService;
  6. /***
  7. * description: 查询用户具体信息,mybatis中一对一样例
  8. * version: 1.0 ->
  9. * date: 2021/12/29 10:32
  10. * author: xiaYZ
  11. * iteration: 迭代说明
  12. * @param userId
  13. * @return java.lang.String
  14. */
  15. @GetMapping("findUserData")
  16. public String findUserData(Long userId){
  17. User user = new User();
  18. try{
  19. user = userService.findUserData(userId);
  20. }catch (Exception e){
  21. e.printStackTrace();
  22. return "查询用户详细数据";
  23. }
  24. return user.toString();
  25. }
  1. @Service
  2. public class UserService {
  3. @Resource
  4. UserDao userDao;
  5. public User findUserData(Long userId){
  6. return userDao.findUserData(userId);
  7. }
  8. }

1.4 运行截图

springBoot学习笔记(2.1)—— 整合mybatis之一对一,一对多查询 - 图1

2. mybatis一对多映射

2.1 Dao接口

  1. @Mapper
  2. public interface DeptDao {
  3. /**
  4. * description: 通过部门id查询部门数据
  5. * version: 1.0
  6. * date: 2021/12/30 15:10
  7. * author: xiaYZ
  8. * iteration: 迭代说明
  9. * @param deptId 部门id
  10. * @return
  11. */
  12. Dept findDeptById(Long deptId);
  13. }

2.1 Mapper文件类(重点)

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.example.springbootmybatis.dao.DeptDao">
  4. <resultMap id="deptMap" type="com.example.springbootmybatis.entity.Dept">
  5. <id column="id" property="id"/>
  6. <result column="dept_name" property="deptName"/>
  7. <result column="parent_id" property="parentId"/>
  8. <result column="order_code" property="orderCode"/>
  9. <result column="is_start" property="isStart"/>
  10. <result column="description" property="description"/>
  11. <result column="address" property="address"/>
  12. <!-- mybatis中一对多映射-->
  13. <collection property="userList" ofType="com.example.springbootmybatis.entity.User">
  14. <id column="userId" property="id"/>
  15. <result column="user_name" property="userName"/>
  16. <result column="idCardNumber" property="idCardNumber"/>
  17. <result column="phone_number" property="phoneNumber"/>
  18. <result column="dept_id" property="deptId"/>
  19. </collection>
  20. <!--一对多时List<String>映射方法--->
  21. <collection property="userNameList" ofType="java.lang.String">
  22. <constructor>
  23. <arg column="user_name"/>
  24. </constructor>
  25. </collection>
  26. </resultMap>
  27. <select id="findDeptById" resultMap="deptMap">
  28. select d.id, dept_name, parent_id,order_code, is_start, description, address,
  29. u.id userId, user_name, idCardNumber, phone_number, dept_id
  30. from dept d
  31. left join user u on u.dept_id = d.id
  32. where d.id = #{deptId}
  33. </select>
  34. </mapper>

2.3 Controller和Service类

  1. @RestController
  2. @RequestMapping("deptController")
  3. public class DeptController {
  4. @Resource
  5. DeptService deptService;
  6. /***
  7. * description: 通过部门id查询部门数据
  8. * version: 1.0 ->
  9. * date: 2021/12/30 15:24
  10. * author: xiaYZ
  11. * iteration: 迭代说明
  12. * @param deptId 部门id
  13. * @return java.lang.String
  14. */
  15. @GetMapping("findDeptById")
  16. public String findDeptById(Long deptId){
  17. Dept dept = new Dept();
  18. try{
  19. dept = deptService.findDeptById(deptId);
  20. return dept.toString();
  21. }catch (Exception e){
  22. e.printStackTrace();
  23. return "查询部门数据错误";
  24. }
  25. }
  26. }
  1. @Service
  2. public class DeptService {
  3. @Resource
  4. DeptDao deptDao;
  5. public Dept findDeptById(Long deptId){
  6. return deptDao.findDeptById(deptId);
  7. }
  8. }

2.4运行截图

springBoot学习笔记(2.1)—— 整合mybatis之一对一,一对多查询 - 图2

3 mybatis新增修改删除操作

3.1 Dao接口类

  1. /**
  2. * description: 通过id删除用户操作
  3. * version: 1.0
  4. * date: 2021/12/29 15:54
  5. * author: xiaYZ
  6. * iteration: 迭代说明
  7. * @param userId
  8. * @return
  9. */
  10. int deleteUserById(Long userId);
  11. /**
  12. * description: 新增用户操作
  13. * version: 1.0
  14. * date: 2021/12/29 15:56
  15. * author: xiaYZ
  16. * iteration: 迭代说明
  17. * @param user
  18. * @return
  19. */
  20. int insertUser(User user);
  21. /**
  22. * description: 修改用户操作
  23. * version: 1.0
  24. * date: 2021/12/29 15:57
  25. * author: xiaYZ
  26. * iteration: 迭代说明
  27. * @param user
  28. * @return
  29. */
  30. int updateUser(User user);

3.2 Mapper文件类

  1. <delete id="deleteUserById">
  2. delete
  3. from user
  4. where id = #{userId}
  5. </delete>
  6. <insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
  7. insert into user(id, user_name, idCardNumber, phone_number, dept_id)
  8. values (#{id}, #{userName}, #{idCardNumber}, #{phoneNumber}, #{deptId,jdbcType=BIGINT})
  9. </insert>
  10. <update id="updateUser">
  11. update user
  12. <set>
  13. <if test="userName != null and userName != ''">
  14. user_name = #{userName},
  15. </if>
  16. <if test="idCardNumber != null and idCardNumber != ''">
  17. idCardNumber = #{idCardNumber},
  18. </if>
  19. <if test="phoneNumber != null and phoneNumber != ''">
  20. phone_number = #{phoneNumber},
  21. </if>
  22. <if test="deptId != null">
  23. dept_id = #{deptId}
  24. </if>
  25. </set>
  26. where id = #{id}
  27. </update>

3.3 Service服务层类

  1. public int deleteUserById(Long userId){
  2. return userDao.deleteUserById(userId);
  3. }
  4. public int insertUser(User user){
  5. return userDao.insertUser(user);
  6. }
  7. public int updateUser(User user){
  8. return userDao.updateUser(user);
  9. }

3.4 Controller控制层类

  1. public int deleteUserById(Long userId){
  2. return userDao.deleteUserById(userId);
  3. }
  4. public int insertUser(User user){
  5. return userDao.insertUser(user);
  6. }
  7. public int updateUser(User user){
  8. return userDao.updateUser(user);
  9. }

项目截图就不一一在此展示了


总结

  1. mybatis中一对一映射使用association标签。
    2. mybatis中一对多映射使用collection标签。
    注意:List 的映射方式
    3. 数据库中新增操作时可以使用insert标签中useGenerateKeys=”true” keyProperty=”id”来获取新增数据的id

项目源码