04_通用mapper

张创琦 2022.03.11

避免重复书写CRUD映射的框架有两个

  • 通用mybatis(tk mybatis)

  • mybatis plus,通能更加强大,后面实战项目中讲解

1 tk mybatis

  1. pom.xml 配置文件
  1. <!-- 通用mapper -->
  2. <dependency>
  3. <groupId>tk.mybatis</groupId>
  4. <artifactId>mapper-spring-boot-starter</artifactId>
  5. <version>2.0.2</version>
  6. </dependency>
  1. 实体类配置
  1. @Table(name = "tb_user") // 表名相关注解
  2. @Column(name="fieldName") // 属性名和字段名的相关注解
  1. package com.lxs.domain;
  2. import javax.persistence.GeneratedValue;
  3. import javax.persistence.GenerationType;
  4. import javax.persistence.Id;
  5. import javax.persistence.Table;
  6. import java.io.Serializable;
  7. import java.util.Date;
  8. @Table(name = "tb_user") // 如果不声明则默认一样的表名
  9. // 支持驼峰转下划线的命名
  10. public class User implements Serializable {
  11. // private static final long serialVersionUID = 1L;
  12. // 主键的映射一定要进行
  13. @Id
  14. @GeneratedValue(strategy = GenerationType.IDENTITY) // 逐渐策略是自增
  15. private Long id;
  16. // 用户名
  17. //自动转换下换线到驼峰命名 user_name -> userName
  18. private String userName;
  19. // 密码
  20. private String password;
  21. // 姓名
  22. private String name;
  23. // 年龄
  24. private Integer age;
  25. // 性别,1男性,2女性
  26. private Integer sex;
  27. // 出生日期
  28. private Date birthday;
  29. // 创建时间
  30. private Date created;
  31. // 更新时间
  32. private Date updated;
  33. // 备注
  34. private String note;
  35. @Override
  36. public String toString() {
  37. return "User{" +
  38. "id=" + id +
  39. ", userName='" + userName + '\'' +
  40. ", password='" + password + '\'' +
  41. ", name='" + name + '\'' +
  42. ", age=" + age +
  43. ", sex=" + sex +
  44. ", birthday=" + birthday +
  45. ", created=" + created +
  46. ", updated=" + updated +
  47. ", note='" + note + '\'' +
  48. '}';
  49. }
  50. // 省略 getter 和 setter 方法
  51. }
  1. UserMapper

一旦继承了Mapper,继承的Mapper就拥有了Mapper所有的通用方法。

具体方法见 pdf 文档

  1. package com.lxs.dao;
  2. import com.lxs.domain.User;
  3. import tk.mybatis.mapper.common.Mapper;
  4. import java.util.List;
  5. // import org.apache.ibatis.annotations.Mapper;
  6. // 注意:在mapper导包时,导入的是tk.mybatis的
  7. public interface UserMapper extends Mapper<User> {
  8. public List<User> findByUser(User user);
  9. }

注意以下的写法:

  1. package com.lxs.dao;
  2. import com.lxs.domain.User;
  3. //import tk.mybatis.mapper.common.Mapper;
  4. import java.util.List;
  5. import org.apache.ibatis.annotations.Mapper;
  6. @Mapper
  7. public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User> {
  8. public List<User> findByUser(User user);
  9. }
  1. 映射复杂文件:

resources/mappers/UserMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.lxs.dao.UserMapper">
  5. <select id="findByUser" resultType="user">
  6. SELECT * FROM tb_user
  7. <where>
  8. <if test="name != null"> name like '%${name}%' </if>
  9. <if test="note != null"> and note like '%${note}%' </if>
  10. </where>
  11. </select>
  12. </mapper>
  1. 注意Application的导包问题

一个是 mybatis 的,一个是 tk mybatis 的

  1. // import org.mybatis.spring.annotation.MapperScan; // myBatis的
  2. import org.springframework.boot.SpringApplication; // tk myBatis的
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  5. import tk.mybatis.spring.annotation.MapperScan;
  6. @SpringBootApplication
  7. @EnableConfigurationProperties
  8. @MapperScan("com.lxs.dao")
  9. public class Application {
  1. 测试
  1. package com.lxs.dao;
  2. import com.lxs.domain.User;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.test.context.junit4.SpringRunner;
  8. import tk.mybatis.mapper.entity.Example;
  9. import java.util.Date;
  10. import java.util.List;
  11. import static org.junit.Assert.*;
  12. @RunWith(SpringRunner.class)
  13. @SpringBootTest
  14. public class UserMapperTest {
  15. @Autowired
  16. private UserMapper userMapper;
  17. @Autowired
  18. private UserDao userDao;
  19. /* chenggong */
  20. @Test
  21. public void testFindByUser() {
  22. User condition = new User();
  23. condition.setName("a");
  24. List<User> list = userMapper.findByUser(condition);
  25. for (User user : list) {
  26. System.out.println(user);
  27. }
  28. }
  29. /* chenggong
  30. @Test
  31. public void testFindAll() {
  32. userMapper.selectAll().forEach(user -> {
  33. System.out.println(user);
  34. });
  35. }*/
  36. /* 查询不到userName
  37. @Test
  38. public void testFindAll1() {
  39. List<User> list = userDao.findAll();
  40. for (User user : list) {
  41. System.out.println(user);
  42. }
  43. }*/
  44. /* 未测试
  45. @Test
  46. public void testFindById() {
  47. User user = userDao.selectByPrimaryKey(4);
  48. System.out.println(user);
  49. }
  50. */
  51. /* 测试成功 */
  52. @Test
  53. public void testFindByExample() {
  54. Example example = new Example(User.class);
  55. example.createCriteria().andLike("name", "%a%");
  56. userMapper.selectByExample(example).forEach(user -> {
  57. System.out.println(user);
  58. });
  59. }
  60. /* 未测试
  61. @Test
  62. public void testInsert() {
  63. User user = new User();
  64. user.setAge(18);
  65. user.setBirthday(new Date());
  66. user.setCreated(new Date());
  67. user.setName("周星驰");
  68. userDao.insert(user);
  69. }
  70. */
  71. }