MyBatis-plus是mybatis的增强工具,在MyBatis 上只做增强,不做改变,为简化开发,提高效率而生
MyBatis-plus和mybatis就像是魂斗罗中的两个兄弟
安装MyBatisX插件
创建项目
略
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
自动配置的内容
MyBatis PlusAutoConfiguration配置类,MyBatisPlusProperties配置项前缀 mybatis-plus: ***就是对mybatis-plus的参数的设置
SQLSessionFactory已经配置好
mapperlocations 自动配置好的,默认值是classpath*:/mapper/**/*.xml 意为任意包路径下所有的mapper包下的xml文件
@Mapper建议替换成MapperScan
配置mybatisplus
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: root
druid:
initial-size: 5
min-idle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,slf4j
connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
web-stat-filter:
enabled: true
url-pattern: "/*"
exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*"
stat-view-servlet:
url-pattern: "/druid/*"
allow: 127.0.0.1,192.168.8.109
deny: 192.168.1.188
reset-enable: false
login-username: admin
login-password: 123456
mybatis-plus:
type-aliases-package: com.msb.pojo
实体类
@AllArgsConstructor
@NoArgsConstructor
@Data
@TableName("dept")
public class Dept implements Serializable {
/*@TableField(exist = false)
private String aaa;*/
@TableField("deptno")
private Integer deptno;
private String dname;
private String loc;
}
mapper
public interface DeptMapper extends BaseMapper<Dept> {
}
service
接口
public interface DeptService extends IService<Dept> {
}
实现类
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {
}
测试代码CURD
package com.msb;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.msb.mapper.DeptMapper;
import com.msb.pojo.Dept;
import com.msb.service.DeptService;
import lombok.AllArgsConstructor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SpringbootMybatisplusApplicationTests {
@Autowired
DeptService deptService;
@Test
public void testFindAll(){
List<Dept> list = deptService.list();
for (Dept dept : list) {
System.out.println(dept);
}
}
// 查询集合
@Test
public void testQueryWrapper(){
// 部门号》=20
// QueryWrapper 作用就是在原本的SQL语句后面拼接where条件
// selec * from where delete from dept where update dept set ... where ....
QueryWrapper<Dept> queryWrapper=new QueryWrapper<>();
//queryWrapper.ge("deptno", 20).eq("dname", "ACCOUNTING").likeRight("dname", "A");
//queryWrapper.likeRight("dname", "A");
List<Dept> list = deptService.list(queryWrapper);
for (Dept dept : list) {
System.out.println(dept);
}
}
// 查询单个
@Test
public void testQueryWrapper2(){
QueryWrapper<Dept> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("deptno", 20);
Dept dept = deptService.getOne(queryWrapper);
System.out.println(dept);
}
// 增加
@Test
public void testAdd(){
boolean save = deptService.save(new Dept(null, "aaa", "bbb"));
System.out.println(save);
}
// 修改
@Test
public void testUpdate(){
// 要更新的数据
Dept dept =new Dept();
dept.setDname("xxx");
dept.setLoc("yyy");
// 更新的条件
QueryWrapper<Dept> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("deptno", 41);
boolean update = deptService.update(dept, queryWrapper);
System.out.println(update);
}
// 删除
@Test
public void testRemove(){
QueryWrapper<Dept> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("deptno", 41);
boolean remove = deptService.remove(queryWrapper);
System.out.println(remove);
}
}
分页插件的使用
配置分页插件
package com.msb.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author: Ma HaiYang
* @Description: MircoMessage:Mark_7001
*/
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor =new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor =new PaginationInnerInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
//paginationInnerInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
//paginationInnerInterceptor.setMaxLimit(500L);
// 设置数据库类型
paginationInnerInterceptor.setDbType(DbType.MYSQL);
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
return mybatisPlusInterceptor;
}
}
测试分页插件
@Test
public void testPage(){
// 当前页 页大小
QueryWrapper<Dept> queryWrapper=new QueryWrapper<>();
//queryWrapper.likeRight("dname", "A");
Page<Dept> page = deptService.page(new Page<>(1, 2), queryWrapper);
// 当前页数据 总页数 总记录数 当前页 页大小 ... ..
List<Dept> list = page.getRecords();
list.forEach(System.out::println);
System.out.println("总页数:"+page.getPages());
System.out.println("总记录数:"+page.getTotal());
System.out.println("当前页:"+page.getCurrent());
System.out.println("页大小:"+page.getSize());
}