基础案例
==极其精简化了dao层的书写==
==先引入mp起步依赖==
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency>
|
==导入实体类和表==
==定义数据接口继承BaseMapper<泛型>即可==
@Mapper public interface UserDao extends BaseMapper<User> { }
|
CURD和分页
功能 |
自定义接口 |
MP接口 |
新增 |
boolean save(T t) |
int insert(T t) |
删除 |
boolean delete(int id) |
int deleteById(Serializable id) |
修改 |
boolean update(T t) |
int updateById(T t) |
根据id查询 |
T getById(int id) |
T selectById (Serializable id) |
查询全部 |
List getAll() |
List selectList() |
分页查询 |
PageInfo getAll(int page, int size) |
IPage selectPage(IPage page) |
条件查询 |
List getAll(Condition condition) |
IPage selectPage(wrapper querywrapper) |
==自动动态SQL==
样例:
@SpringBootTest class MybatisplusApplicationTests {
@Autowired private UserDao userDao;
@Test void testSave() { User user = new User(); user.setName("Wang"); user.setPassword("123456"); user.setAge(9); user.setTel("116483477"); userDao.insert(user); }
@Test void testDelete() { userDao.deleteById(1L); }
@Test void testUpdate() { User user = new User(); user.setId(1L); user.setName("Wang-nine"); userDao.updateById(user); }
@Test void testGetById() { User user = userDao.selectById(1L); System.out.println(user); }
@Test void testGetAll() { List<User> users = userDao.selectList(null); System.out.println(users); }
@Test void testGetByPage() { IPage page = new Page(1, 2); userDao.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()); }
}
|
==对于分页查询,开启拦截器和输出日志==
拦截器
@Configuration public class MpConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return mybatisPlusInterceptor; } }
|
配置日志输出
mybatis: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
==条件查询==
@Test void testGetByCondition() { QueryWrapper qw = new QueryWrapper(); qw.lt("age", 18); List<User> users = userDao.selectList(qw); System.out.println(users);
QueryWrapper<User> qw1 = new QueryWrapper(); qw1.lambda().lt(User::getAge, 18); List<User> users1 = userDao.selectList(qw); System.out.println(users1);
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>(); lqw.lt(User::getAge, 18); List<User> users2 = userDao.selectList(lqw); System.out.println(users2);
LambdaQueryWrapper<User> lqw1 = new LambdaQueryWrapper<>(); lqw1.lt(User::getAge, 30).or().gt(User::getAge, 10); UserQuery uq = new UserQuery(); uq.setAge(10); uq.setAge2(30); lqw.lt(null != uq.getAge2(), User::getAge, uq.getAge2()); lqw.gt(null != uq.getAge(), User::getAge, uq.getAge()); ...查询语句... }
|
==查询投影,非常麻烦,不如自己写==
==对于其余的复杂查询,例如> = between like null in group order等,强烈建议自己写mysql语句,不要再去学习复杂的API了==
==实在想学,可以查看它的官方文档==
==字段名称映射(均为熟悉注解)==
@TableField(value = "数据库字段名")
|
@TableField(exist = false)
|
@TableField(select = false)
|
全局配置表名前缀
mybatis-plus: global-config: db-config: table-prefix: tbl_
|
==id生成策略(均为属性注解)==
@TableId(value = 数据库主键名称, type = 策略)
|
策略:IdType.AUTO 自增 NONE无策略 INPUT自己输入 ASSIGN_ID雪花算法 ASSIGN_UUIDuuid
也可在yaml中全局配置
mybatis-plus: global-config: db-config: id-type: assign_id
|
==多数据操作==
样例
List<Long> ids = Arrays.asList(new Long[]{2, 3}); userDao.deleteBatchIds(ids); userDao.selectBatchIds(ids);
|
==逻辑删除==
通用配置
mybatis-plus: global-config: db-config: logic-delete-field: deleted logic-not-delete-value: 0 logic-delete-value: 1
|
==乐观锁:比如秒杀(可以解决2000个一下的并发问题)==
新增一个字段并加上下述注解
属性注解
@Configuration public class MpConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); return mybatisPlusInterceptor; } }
|