1.创建一个springboot工程并加入mybatis-plus的相关依赖
<!--引入mybatis-plus的依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
2.书写配置文件
3.书写实体类
4.创建一个接口并继承BaseMapper
5.在主启动类上添加mapper的扫描
@MapperScan(basePackages = {"com.ykq.mybatisplus.mapper"})
6.进行测试(在test中根据id查询相应的信息)
package com.yinqngmybatisplus.mybatisplus;
import com.yinqngmybatisplus.mybatisplus.entry.Test1;
import com.yinqngmybatisplus.mybatisplus.mapper.TestMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest
class MybatisPlusApplicationTests {
@Resource
private TestMapper testMapper;
@Test
void contextLoads() {
Test1 test1 = testMapper.selectById(1);
System.out.println(test1);
}
}
7.crud(增,删,改,查 <皆在test中进行>)
1)新增
运行:
@Test
public void insertTest(){
Test1 test1=new Test1(null,"埃斯","23",0);
int i=testMapper.insert(test1);
System.out.println(i);
}
2)删除(逻辑删除)
在数据库对应的表中增加一个逻辑字段 :
在实体中添加相属性及注解应:
运行:
@Test
public void deleteTest(){
int i= testMapper.deleteById(8);
System.out.println(i);
}
3)修改
修改要有添加时间和修改时间(阿里规定),所以在数据库对应的表中增加:
自动填充
1))在实体中添加相属性及注解:(注:要加其它属性的构造,否则报错)
2))创建一个自动配置类:
package com.mybatisplug.zengqiang.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Date;
@Configuration//表示该类为配置类
public class MybatisPlusConfig implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
//自动填充:修改时会自动填充修改时间,添加时会自动填充添加时间和修改时间
this.strictInsertFill(metaObject,"createTime",Date.class,new Date());
this.strictInsertFill(metaObject,"updateTime",Date.class,new Date());
}
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject,"updateTime",Date.class,new Date());
}
}
运行:
@Test
public void updateTest(){
Test1 test1=new Test1(12,"戚奇","24",1,0);
int i=testMapper.updateById(test1);
}
4)查询
1))全表查询:无条件
运行:
@Test
public void selectAll() {
List<Test1> test01=testMapper.selectList(null);
System.out.println(test01);
}
2))条件查询(以下是含有的多个条件,可一个或多个)
运行:
@Test
public void selectByCondication(){
QueryWrapper<Test1> wrapper=new QueryWrapper<>();
wrapper.between("sage",16,53);
wrapper.or();
wrapper.like("sname","张");
wrapper.orderByDesc("sage");
wrapper.select("count(*)");
List<Test1> test1s=testMapper.selectList(wrapper);
System.out.println(test1s);
}
3))分页查询
引入分页的插件 :
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor=new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
我放在了package com.mybatisplug.zengqiang.config;中
运行:
@Test
public void SelectPage(){
Page<Test1> page=new Page<>(1,4);// 1是当前页数,4是每页的条数
Page<Test1> page1=testMapper.selectPage(page,null);
System.out.println("总页码:"+page1.getPages());
System.out.println("总条数:"+page1.getTotal());
System.out.println("当前页的条数:"+page1.getRecords());
}