MyBatisPlus的增删改查--SpringBoot方式

1、POM文件添加引用

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>MyBatisBoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>MyBatisBoot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

2、配置文件(application.yml)

mybatis-plus:
  #  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
  type-aliases-package: com.yas.entity
  configuration:
    lazy-loading-enabled: true
    aggressive-lazy-loading: false
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: false

 

3、配置类(解决分页问题)

 1 package com.yas.config;
 2 
 3 import com.baomidou.mybatisplus.annotation.DbType;
 4 import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
 5 import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
 6 import org.mybatis.spring.annotation.MapperScan;
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.context.annotation.Configuration;
 9 
10 @Configuration
11 @MapperScan("com.yas.dao")
12 public class MybatisPlusConfig {
13 
14     //配置mp分页插件
15     @Bean
16     public MybatisPlusInterceptor mybatisPlusInterceptor() {
17         MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
18         interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
19         return interceptor;
20     }
21 }

 

4、实体类(POJO)

 1 package com.yas.entity;
 2 
 3 import com.baomidou.mybatisplus.annotation.IdType;
 4 import com.baomidou.mybatisplus.annotation.TableField;
 5 import com.baomidou.mybatisplus.annotation.TableId;
 6 import lombok.AllArgsConstructor;
 7 import lombok.Data;
 8 import lombok.NoArgsConstructor;
 9 import lombok.ToString;
10 
11 import java.util.Date;
12 
13 @Data
14 @NoArgsConstructor
15 @AllArgsConstructor
16 @ToString
17 public class SysUser {
18     //指定主键生成方式为数据库自增
19     @TableId(type= IdType.AUTO)
20     private Integer id;
21     private String account;
22     private String username;
23 
24     //查询时不反悔该字段的值
25     @TableField(select = false)
26     private String pwd;
27 
28     private Date gmt_create;
29 
30     //假如属性名与数据库表中的列名不同,可以在这里设置列名
31     @TableField(value = "gmt_modified")
32     private Date gmt_modified;
33 
34     //这个属性不会进行映射
35     @TableField(exist = false)
36     private String otherProperty;
37 }

 

5、测试方法:

  1 package com.yas;
  2 
  3 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  5 import com.baomidou.mybatisplus.core.metadata.IPage;
  6 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7 import com.yas.dao.SysUserPlusDAO;
  8 import com.yas.entity.SysUser;
  9 import org.junit.jupiter.api.Test;
 10 import org.springframework.beans.factory.annotation.Autowired;
 11 import org.springframework.boot.test.context.SpringBootTest;
 12 
 13 import java.sql.Timestamp;
 14 import java.util.*;
 15 
 16 @SpringBootTest
 17 public class MyBatisPlusTests {
 18     @Autowired
 19     SysUserPlusDAO sysUserPlusDAO;
 20 
 21     //插入
 22     @Test
 23     void testInsert() {
 24         SysUser user = new SysUser(null, "manager", "王羲之", "000000", new Date(), new Date(), null);
 25         int result = sysUserPlusDAO.insert(user);
 26         System.out.println(result);
 27         System.out.println("主键回填:" + user.getId());
 28     }
 29 
 30     //根据id查询
 31     @Test
 32     void testSelectById() {
 33         SysUser sysUser = sysUserPlusDAO.selectById(8);
 34         System.out.println(sysUser);
 35     }
 36 
 37     //根据id更新
 38     @Test
 39     void testUpdateById() {
 40         SysUser user = new SysUser();
 41         user.setId(8);
 42         user.setUsername("王献之");
 43         int result = sysUserPlusDAO.updateById(user);
 44         System.out.println(result);
 45     }
 46 
 47     //根据条件更新
 48     @Test
 49     void testUpdate() {
 50         SysUser user = new SysUser();
 51         user.setPwd("888888");
 52 
 53         QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
 54         wrapper.eq("account", "manager");
 55         int result = sysUserPlusDAO.update(user, wrapper);
 56         System.out.println(result);
 57     }
 58 
 59 
 60     //根据条件更新2
 61     @Test
 62     void testUpdate2() {
 63         UpdateWrapper<SysUser> wrapper = new UpdateWrapper<>();
 64         wrapper.set("pwd", "666666").eq("account", "manager");
 65         int result = sysUserPlusDAO.update(null, wrapper);
 66         System.out.println(result);
 67     }
 68 
 69     //根据条件删除
 70     @Test
 71     void testDeleteById() {
 72         int result = sysUserPlusDAO.deleteById(8);
 73         System.out.println(result);
 74     }
 75 
 76     //根据map条件删除,多条件之间是and关系
 77     @Test
 78     void testDeleteByMap() {
 79         Map<String, Object> map = new HashMap<>();
 80         map.put("username", "王维");
 81         map.put("pwd", "666666");
 82         //根据map删除,多条件之间是 and 关系
 83         int result = sysUserPlusDAO.deleteByMap(map);
 84         System.out.println(result);
 85     }
 86 
 87     //使用条件删除
 88     @Test
 89     void testDeleteByWrapper() {
 90         //用法一:
 91 //        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
 92 //        wrapper.eq("username","杜甫").eq("pwd","666666");
 93 
 94         //用法二:(推荐)
 95         SysUser user = new SysUser();
 96         user.setUsername("李白");
 97         user.setPwd("666666");
 98         QueryWrapper<SysUser> wrapper = new QueryWrapper<>(user);
 99 
100         int result = sysUserPlusDAO.delete(wrapper);
101         System.out.println(result);
102     }
103 
104     //根据id批量删除数据
105     @Test
106     void testDeleteByBatchIds() {
107         int result = sysUserPlusDAO.deleteBatchIds(Arrays.asList(9, 10));
108         System.out.println(result);
109     }
110 
111     //查询 条件in
112     @Test
113     void testSelectBatchIds() {
114         List<SysUser> users = sysUserPlusDAO.selectBatchIds(Arrays.asList(1, 2, 3, 4, 5));
115         for (SysUser user : users) {
116             System.out.println(user);
117         }
118     }
119 
120     //查询单条,返回结果最多只能有1条
121     @Test
122     void testSelectOne() {
123         SysUser user = new SysUser();
124         user.setId(1);
125         user.setAccount("admin");
126         user.setUsername("管理员");
127         QueryWrapper<SysUser> wrapper = new QueryWrapper<>(user);
128 
129         SysUser user2 = sysUserPlusDAO.selectOne(wrapper);
130         System.out.println(user2);
131     }
132 
133     @Test
134     void testSelectCount() {
135         QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
136         wrapper.isNotNull("username");
137         int result = sysUserPlusDAO.selectCount(wrapper);
138         System.out.println(result);
139     }
140 
141     @Test
142     void testSelectList() {
143         QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
144         wrapper.isNotNull("username");
145         List<SysUser> users = sysUserPlusDAO.selectList(wrapper);
146         for (SysUser user : users) {
147             System.out.println(user);
148         }
149     }
150 
151     @Test
152     void testSelectPage() {
153         Page<SysUser> page = new Page<>(1, 1);
154 
155         QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
156         wrapper.like("username", "C");
157         IPage<SysUser> iPage = sysUserPlusDAO.selectPage(page, wrapper);
158         System.out.println("数据总条数:" + iPage.getTotal());
159         System.out.println("总页数:" + iPage.getPages());
160         System.out.println("当前是第" + iPage.getCurrent() + "页");
161 
162         List<SysUser> records = iPage.getRecords();
163         for (SysUser record : records) {
164             System.out.println(record);
165         }
166     }
167 }

 

上一篇:leetcode-98-验证二叉搜索树


下一篇:一些常见的git 命令