MyBatisPlus简介
- 无侵入:只做增强不做改变,不会对现有工程产生影响
- 强大的CRUD操作:内置通用的Mapper,少量配置即可实现CRUD操作
- 支持Lambda:编写查询条件无需担心字段写错
- 支持主键自动生成
- 内置分页插件
所需的依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
标准的CRUD操作
- 首先定义对应的实体类
- 创建对应的Mapper接口,让其继承于BaseMapper<>
- 调用内置的CRUD方法
功能 | MP接口 |
---|---|
新增 | int insert(T t) |
删除 | int deleteById(Serializable id) |
修改 | int updateById(T t) |
根据id查询 | T selectById(Serializable id) |
查询全部 | List |
分页查询 | IPage |
按条件查询 | IPage |
- 实体类的编写
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserInfo {
private int id;
private String name;
private int age;
public UserInfo(String name, int age) {
this.name = name;
this.age = age;
}
}
- Mapper接口的编写
@Mapper
public interface UserMapper extends BaseMapper<UserInfo> {
}
- 测试类的编写
@SpringBootTest
class Demo1ApplicationTests {
@Autowired
public UserMapper userMapper;
@Test
public void save(){
UserInfo user = new UserInfo("jack",20);
userMapper.insert(user);
}
@Test
public void delete(){
userMapper.deleteById(3);
}
@Test
public void update(){
UserInfo userInfo = new UserInfo();
userInfo.setName("wang");
userInfo.setId(1);
userMapper.updateById(userInfo);
}
@Test
public void getOne(){
System.out.println(userMapper.selectById(1));
}
@Test
public void getAll() {
System.out.println(userMapper.selectList(null));
}
}
MyBatisPlus的调试日志
spring:
datasource:
url: jdbc:mysql:///mybatis?serverTimeZone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
main:
banner-mode: off #关闭SpringBoot 的banner
server:
port: 8080 # 设置服务器的端口号
logging:
level:
root: error # 设置SpringBoot的日志为只有报错的时候打印日志
mybatis-plus:
global-config:
db-config:
table-prefix: # 添加表名的前缀
id-type: auto # 默认的id是采用雪花算法,auto后就是id自增了
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #标准日志输出
MybatisPlus分页操作
- 创建MybatisPlus配置类
@Configuration
public class MyBatisPlusConfigration {
//设置分页拦截器
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mybatisPlusInterceptor;
}
}
- 编写测试方法
@Test
void pageQuery(){
IPage page = new Page(1,3);
userMapper.selectPage(page,null);
System.out.println(page.getPages());//总页数
System.out.println(page.getCurrent());//当前页码
System.out.println(page.getSize());//当前页的数量
System.out.println(page.getTotal());//总数
System.out.println(page.getRecords());//返回的集合
}
MyBatisPlus条件查询
@Test
public void whereQuery(){
String name = "j";
LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper();
/*
* param 1: 判断字符是否为null值,可以省略,一但为null那么sql的模糊查询--> %null%
* param 2: 根据那个字段进行匹配
* parma 3: 模糊查询的内容
*/
queryWrapper.like(Strings.isNotEmpty(name),UserInfo::getName,name);
userMapper.selectList(queryWrapper);
}
标签:name,void,userMapper,page,Test,使用,mybatis,public,puls
From: https://www.cnblogs.com/-xyk/p/17590606.html