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