mybatis-plus
一、导包
1.1 mybatis-plus
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.4</version>
</dependency>
1.2 lombok
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
二、配置
2.1 数据库配置
spring:
datasource:
username: root
password: haorui1996
url: jdbc:mysql://localhost:3306/eat-what?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
2.2 mapper继承
public interface UserMapper extends BaseMapper<User>
2.3 在 Spring Boot 启动类中添加 @MapperScan
注解,扫描 Mapper 文件夹:
@SpringBootApplication
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2.4 测试数据库是否连接成功
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
System.out.println(dataSource.getConnection());
}
三、mybatis-plus使用
/**
* 获取列表
* @return map
*/
public List<Map<String,Object>> data(int pageNum){
List<Map<String,Object>> res = new ArrayList<>();
//查表
QueryWrapper<Menu> wrapper = new QueryWrapper();
wrapper.eq("verify",false);
//分页
Page<Menu> page = new Page<>(pageNum,10);
menu.selectPage(page,wrapper);
var menus = page.getRecords();
//单个对象赋值
for (int i = 0; i < menus.size(); i++) {
Map<String,Object> map = new HashMap();
map.put("id",menus.get(i).getId());
map.put("name",menus.get(i).getName());
map.put("eatdesc",menus.get(i).getDescription());
//对应菜单
QueryWrapper<Dish> wrapper1 = new QueryWrapper();
wrapper1.eq("menuid",menus.get(i).getId());
List<Dish> desc = dish.selectList(wrapper1);
map.put("desc",desc);
res.add(map);
}
3.1 wrapper
- wrapper对象为查询条件
-
- QueryWrapper
- 每次查询出的返回参数为一张数据库表
- 通过wrapper设置对应的查询条件(wrapper.eq(“verify”,false);)
- 接口需要一个wrapper参数作为查询条件
3.2 page(分页)
-
配置分页拦截器
//Spring boot方式 @Configuration @MapperScan("com.baomidou.cloud.service.*.mapper*") public class MybatisPlusConfig { // 旧版 @Bean public PaginationInterceptor paginationInterceptor() { PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false // paginationInterceptor.setOverflow(false); // 设置最大单页限制数量,默认 500 条,-1 不受限制 // paginationInterceptor.setLimit(500); // 开启 count 的 join 优化,只针对部分 left join paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true)); return paginationInterceptor; } // 最新版 @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; } }
- Page
- 使用分页时必须使用**selectPage(page,wrapper)**进行查询,返回的对象依旧是Page(相当于做了set)
- **page.getRecords()**返回一个List集合
四、查询
4.1 模糊查询
方法 | 解释 |
---|---|
wrapper.notLike(column,val) | 查询column字段中没有val的行 |
wrapper.likeRight(column,val) | t% |
wrapper.likeLight(column,val) | %t |
wrapper.like(column,val) | %t% |
五、注解
5.1 @TableId
-
type:
值 描述 AUTO 数据库自增 NONE MP set主键,雪花算法实现 INPUT 手动赋值 ASSIGN_ID MP 分配ID,Long、Integer、String ASSIGN_UUID 分配UUID,String