MyBatisPlus
Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
MyBatis-Plus可以自动为我们生成Dao层的简单CRUD操作,同时还内置了基于MyBatis的物理分页功能。
注意:MyBatisPlus的2.X版本和3.X版本有不兼容的现象,因为3.X改动了部分包名,还有分页插件也进行了较大的改动。
本篇博客以MyBatisPlus-3.0.5版本为环境基础。
搭建开发环境
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
|
application.yml配置
1 2 3 4 5 6 7 8 9 10 11 12
| spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/myshop username: root password: admin jackson: default-property-inclusion: ALWAYS time-zone: GMT+8 date-format: yyyy-MM-dd mybatis-plus: type-aliases-package: com.hrp.domain
|
MyBatisPlus配置
1 2 3 4 5 6 7 8 9 10 11 12
| @Configuration @MapperScan("com.hrp.dao") public class MyBatisPlusConfig {
@Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
|
实体类的编写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Data @TableName("user") public class User { @TableId(type = IdType.AUTO) private Long id; private String username; private String password; private String name; private Integer gender; private Date birthday; private String phone; private String picture; private String address1; private String address2; private String address3; private Integer power; }
|
Dao层编写
1 2
| public interface UserDao extends BaseMapper<User> { }
|
测试类的编写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| @SpringBootTest @RunWith(SpringRunner.class) public class UserDaoTest {
@Autowired private UserDao userDao;
@Test public void test01(){ User user = userDao.selectById(1); System.out.println(user); }
@Test public void test02(){ List<User> list = userDao.selectList(null); for (User user : list) { System.out.println(user); } }
@Test public void test03(){ Map<String,Object> map = new HashMap<>(); map.put("username","zero"); map.put("name","零"); List<User> list = userDao.selectByMap(map); for (User user : list) { System.out.println(user); } }
@Test public void test04(){ IPage<User> page = new Page<>(2,3); IPage<User> UserPage = userDao.selectPage(page, null); for (User user : UserPage.getRecords()) { System.out.println(user); } }
@Test public void test05(){ User user = new User(); user.setUsername("阿部多瑞"); user.setPassword("123"); user.setName("宣墨"); user.setGender(0); user.setBirthday(new Date()); user.setPhone("154845129875"); user.setPower(2); userDao.insert(user); } }
|