MyBatis-Plus 基本使用

╰半夏微凉° 2022-12-12 13:57 215阅读 0赞

推荐:MyBatis Plus汇总

MyBatis-Plus 基本使用

首先我们需要创建一个数据库表,用于演示MyBatis-Plus的基本用法。

  1. CREATE TABLE `user` (
  2. `id` varchar(32) NOT NULL,
  3. `username` varchar(32) DEFAULT '',
  4. `password` varchar(32) DEFAULT '',
  5. PRIMARY KEY (`id`)
  6. );

然后创建一个Spring Boot项目,pom.xml和配置如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>org.kaven</groupId>
  5. <artifactId>mybatis-plus</artifactId>
  6. <version>1.0-SNAPSHOT</version>
  7. <parent>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-parent</artifactId>
  10. <version>2.3.4.RELEASE</version>
  11. <relativePath/>
  12. </parent>
  13. <properties>
  14. <java.version>1.8</java.version>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-test</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-webflux</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>com.baomidou</groupId>
  31. <artifactId>mybatis-plus-boot-starter</artifactId>
  32. <version>3.4.0</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>mysql</groupId>
  36. <artifactId>mysql-connector-java</artifactId>
  37. <version>5.1.49</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.projectlombok</groupId>
  41. <artifactId>lombok</artifactId>
  42. </dependency>
  43. </dependencies>
  44. <build>
  45. <plugins>
  46. <plugin>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-maven-plugin</artifactId>
  49. </plugin>
  50. </plugins>
  51. </build>
  52. </project>
  53. spring:
  54. application:
  55. name: mybatis-plus
  56. datasource:
  57. driver-class-name: com.mysql.jdbc.Driver
  58. username: root
  59. password: 123456
  60. url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=false
  61. server:
  62. port: 8085
  63. logging:
  64. level:
  65. root: warn
  66. com.kaven.mybatisplus.dao: trace
  67. pattern:
  68. console: '%p%m%n'

实体类User:

  1. package com.kaven.mybatisplus.entity;
  2. import com.baomidou.mybatisplus.annotation.TableField;
  3. import com.baomidou.mybatisplus.annotation.TableId;
  4. import com.baomidou.mybatisplus.annotation.TableName;
  5. import lombok.Data;
  6. @TableName("user")
  7. @Data
  8. public class User {
  9. @TableId
  10. private String id;
  11. @TableField("username")
  12. private String username;
  13. @TableField("password")
  14. private String password;
  15. /** * 使用 @TableField(exist = false) ,表示该字段在数据库中不存在 ,所以不会插入到数据库中 * 使用 transient 、 static 修饰的属性也不会插入数据库中 */
  16. @TableField(exist = false)
  17. private String phone;
  18. }

Mapper接口UserMapper:

  1. package com.kaven.mybatisplus.dao;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.kaven.mybatisplus.entity.User;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. public interface UserMapper extends BaseMapper<User> {
  7. }

UserMapper需要继承MyBatis-Plus的BaseMapper接口。

BaseMapper接口源码如下,其实就是定义了一些数据库表的CRUD方法。

  1. package com.baomidou.mybatisplus.core.mapper;
  2. import com.baomidou.mybatisplus.core.conditions.Wrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import java.io.Serializable;
  5. import java.util.Collection;
  6. import java.util.List;
  7. import java.util.Map;
  8. import org.apache.ibatis.annotations.Param;
  9. public interface BaseMapper<T> extends Mapper<T> {
  10. int insert(T entity);
  11. int deleteById(Serializable id);
  12. int deleteByMap(@Param("cm") Map<String, Object> columnMap);
  13. int delete(@Param("ew") Wrapper<T> wrapper);
  14. int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
  15. int updateById(@Param("et") T entity);
  16. int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
  17. T selectById(Serializable id);
  18. List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
  19. List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
  20. T selectOne(@Param("ew") Wrapper<T> queryWrapper);
  21. Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);
  22. List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
  23. List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
  24. List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
  25. <E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);
  26. <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param("ew") Wrapper<T> queryWrapper);
  27. }

启动类:

  1. package com.kaven.mybatisplus;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. @MapperScan(basePackages = "com.kaven.mybatisplus.dao")
  7. public class AppRun {
  8. public static void main(String[] args) {
  9. SpringApplication.run(AppRun.class , args);
  10. }
  11. }

@MapperScan(basePackages = "com.kaven.mybatisplus.dao")这个一定要加上。
这样就构建好了项目,使用MyBatis-Plus可以不用写Mapper.xml配置文件,是不是贼方便。

我们先在数据库中添加几行数据,方便演示。

在这里插入图片描述

我们来演示几个基本的查询方法。

  1. package com.kaven.mybatisplus.dao;
  2. import com.kaven.mybatisplus.entity.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 java.util.Arrays;
  9. import java.util.HashMap;
  10. import java.util.List;
  11. import java.util.Map;
  12. @RunWith(SpringRunner.class)
  13. @SpringBootTest
  14. public class UserMapperTest {
  15. @Autowired
  16. private UserMapper userMapper;
  17. @Test
  18. public void selectList(){
  19. // 条件设置为null , 就是没有条件,即查询所有数据
  20. List<User> userList = userMapper.selectList(null);
  21. userList.forEach(System.out::println);
  22. }
  23. @Test
  24. public void selectById(){
  25. // 根据Id查询
  26. User user = userMapper.selectById("1");
  27. System.out.println(user);
  28. }
  29. @Test
  30. public void selectBatchIds(){
  31. // 根据Id列表进行批查询
  32. List<String> idList = Arrays.asList("1" , "2" , "3");
  33. List<User> userList = userMapper.selectBatchIds(idList);
  34. userList.forEach(System.out::println);
  35. }
  36. @Test
  37. public void selectByMap(){
  38. // 根据<属性 , 值>来进行匹配查询 , 多个<属性 , 值>会通过and方式来查询
  39. Map<String , Object> map = new HashMap<>();
  40. // 这里是数据库的列名 , 而不是实体类的属性名
  41. map.put("username" , "kaven");
  42. map.put("password" , "kaven");
  43. List<User> userList = userMapper.selectByMap(map);
  44. userList.forEach(System.out::println);
  45. }
  46. }

运行结果:
在这里插入图片描述
再演示更新方法。

  1. package com.kaven.mybatisplus.dao;
  2. import com.kaven.mybatisplus.entity.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. @RunWith(SpringRunner.class)
  9. @SpringBootTest
  10. public class UserMapperUpdateTest {
  11. @Autowired
  12. private UserMapper userMapper;
  13. @Test
  14. public void updateById(){
  15. // 根据Id进行更新
  16. User user = userMapper.selectById("1");
  17. user.setPassword("itkaven");
  18. int rows = userMapper.updateById(user);
  19. System.out.println(userMapper.selectById(user.getId()));
  20. }
  21. }

在这里插入图片描述
其实还有一个update方法,但它需要一个条件,条件也可以设置为null,但这样会更新所有的数据,这里先不演示,之后的博客再进行演示说明,两个更新方法的定义如下。

  1. /** * 根据 ID 修改 * * @param entity 实体对象 */
  2. int updateById(@Param(Constants.ENTITY) T entity);
  3. /** * 根据 whereEntity 条件,更新记录 * * @param entity 实体对象 (set 条件值,可以为 null) * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) */
  4. int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);

再演示几个删除方法。

  1. package com.kaven.mybatisplus.dao;
  2. import org.junit.Assert;
  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 org.springframework.transaction.annotation.Transactional;
  9. import java.util.Arrays;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. @RunWith(SpringRunner.class)
  14. @SpringBootTest
  15. public class UserMapperDeleteTest {
  16. @Autowired
  17. private UserMapper userMapper;
  18. @Test
  19. @Transactional
  20. public void deleteById(){
  21. // 根据Id进行删除
  22. int rows = userMapper.deleteById("1");
  23. Assert.assertEquals(rows , 1);
  24. }
  25. @Test
  26. @Transactional
  27. public void deleteByMap(){
  28. // 根据<属性 , 值>进行匹配删除
  29. Map<String , Object> map = new HashMap<>();
  30. map.put("username" , "607");
  31. map.put("password" , "607");
  32. int rows = userMapper.deleteByMap(map);
  33. Assert.assertEquals(rows , 1);
  34. }
  35. @Test
  36. @Transactional
  37. public void deleteBatchIds(){
  38. // 根据Id列表进行批删除
  39. List<String> idList = Arrays.asList("1" , "2" , "3");
  40. int rows = userMapper.deleteBatchIds(idList);
  41. Assert.assertEquals(rows , 3);
  42. }
  43. }

结果如下:
在这里插入图片描述
这里也还有一个delete方法,也需要一个条件,所以也不进行演示了。

再演示插入方法。

  1. package com.kaven.mybatisplus.dao;
  2. import com.kaven.mybatisplus.entity.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. @RunWith(SpringRunner.class)
  9. @SpringBootTest
  10. public class UserMapperInsertTest {
  11. @Autowired
  12. private UserMapper userMapper;
  13. @Test
  14. public void insert(){
  15. // 直接实体插入
  16. User user = new User();
  17. user.setId("4");
  18. user.setUsername("hn");
  19. user.setPassword("hn");
  20. userMapper.insert(user);
  21. }
  22. }

结果如下:
在这里插入图片描述

这就是MyBatis-Plus的基本使用了。

写博客是博主记录自己的学习过程,如果有错误,请指正,谢谢!

发表评论

表情:
评论列表 (有 0 条评论,215人围观)

还没有评论,来说两句吧...

相关阅读