快速入门
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.3.1</version>
- </dependency>
- package com.example.springboot_learn.mapper;
-
- import com.baomidou.mybatisplus.mapper.BaseMapper;
- import com.example.springboot_learn.Pojo.User;
- import org.apache.ibatis.annotations.Mapper;
-
- @Mapper
- public interface usermapper extends BaseMapper<User> {
-
- }
- #mybatis的日志
- mybatis-plus:
- //日志显示
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- //关掉图标
- global-config:
- banner: false
- @Test
- void testum(){
-
- //新增数据
- User user=new User(6,"zzl",55,"test6baomidou.com");
- um.insert(user);
-
-
- //查询全部
- List
userslist = um.selectList(null); - System.out.println(userslist);
-
- //参数ID查询
- User user1 = um.selectById(2);
- System.out.println("user1 = " + user1);
-
- //修改数据,这里有个注意点:只会修改你赋的值,未初始化的值不做修改
- User user2=new User();
- user2.setId(5);
- user2.setName("zzlyyds");
- um.updateById(user2);
-
- //删除数据
- um.deleteById(6);
- }
- @Test
- void tset02(){
-
- //分页查询,要使用拦截器实现该功能
- //1:显示第1页 2:一页多少条数据
- IPage page = new Page(1,2);
- um.selectPage(page,null);
- //显示第几页
- System.out.println(page.getCurrent());
- //显示每页大小
- System.out.println(page.getSize());
- //显示一共有多少页
- System.out.println(page.getPages());
- //显示一共用多少条数据
- System.out.println(page.getTotal());
- //显示该页的数据
- System.out.println(page.getRecords());
-
- }
分页查询的拦截器配置:
- package com.example.springboot_learn;
-
- import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
- import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- @Configuration
- public class mpConfig {
-
- @Bean
- public MybatisPlusInterceptor mybatisPlusInterceptor() {
-
- //定义Mybatisplus
- MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
-
- //添加具体的分页查询的拦截器
- interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
-
-
- return interceptor;
- }
- }
- @Test
- void tset03(){
-
- //方式一
- //按条件查询,由QueryWrapper对象来实现
- QueryWrapper wrapper = new QueryWrapper();
- wrapper.lt("age",20);
- List
userslist = um.selectList(wrapper); - System.out.println(userslist);
-
-
- //方式二
- QueryWrapper
wrapper1 = new QueryWrapper(); - wrapper1.lambda().gt(User::getAge,20);
- List
userslist1 = um.selectList(wrapper1); - System.out.println(userslist1);
-
-
-
- //方式三,主用!!!!
- LambdaQueryWrapper
lambdaQueryWrapper = new LambdaQueryWrapper(); - lambdaQueryWrapper.gt(User::getAge,25);
- List
userslist2 = um.selectList(lambdaQueryWrapper); - System.out.println(userslist2);
-
-
- //链式编程写多条件查询
- LambdaQueryWrapper
lambdaQueryWrapper1 = new LambdaQueryWrapper(); -
- //and操作
- lambdaQueryWrapper1.lt(User::getAge,25).gt(User::getAge,20);
- //or操作
- lambdaQueryWrapper1.lt(User::getAge,25).or().gt(User::getAge,20);
-
- userslist2 = um.selectList(lambdaQueryWrapper1);
- System.out.println(userslist2);
- }
- //字段筛选
- wrapper.select("age");
- //分组
- wrapper.groupBy("age");
- //排序
- wrapper.orderByAsc("age");
- wrapper.orderByDesc("age");
- //等值
- wrapper.eq("age",18);
- //范围查询gt ge lt le eq between
- wrapper.gt("age",18);
- //模糊匹配
- wrapper.likeRight("name","h");
- wrapper.likeLeft("name","j");
表名: @TableName("user")
字段匹配以及不做字段投影:@TableField(value = 'pwd' ,select = false)
添加属性:@TableField(exist = false)
- @TableId(type = IdType.AUTO)
- @TableId(type = IdType.ASSIGN_ID)
- @TableId(type = IdType.INPUT)
- @TableId(type = IdType.NONE)
- @TableId(type = IdType.UUID)
- private Integer id;
雪花ID的组成
表名前缀以及雪花算法的全局配置
- #mybatis的配置
- mybatis-plus:
- # configuration:
- # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- global-config:
- banner: false
- db-config:
- id-type: assign_id
- table-prefix: tbl_
- List<Long> list = new ArrayList<>();
- //多数据删除
- um.deleteBatchIds(list);
- //多数据查询
- um.selectBatchIds(list);
添加数据库字段deleted
全局配置
- #mybatis的配置
- mybatis-plus:
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- global-config:
- banner: false
- db-config:
- id-type: assign_id
- //逻辑删除
- logic-not-delete-value: 0
- logic-delete-value: 1
步骤一:添加数据库字段version
步骤二:添加字段的@version注解
步骤三:添加拦截器
步骤四:实现
Mybatisplus
特点: