目录
2.第一个用法生成mapper和service以及实体类模板
MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为 简化开发、提高效率而生。
1.无侵入:只做增强不做改变,引入它不会对现有工程产生影响,损耗小:
2.启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作 强大的 CRUD 操作:
3.内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,
4.有强大的条件构造器,满足各类使用需求 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,
5.支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由 配置,完美解决主键问题 支持
6.ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强 大的 CRUD 操作
7. 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,
8.内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等 同于普通 List 查询 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、 Postgre、SQLServer 等多种数据库
9.内置性能分析插件:可输出 SQL 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出 慢查询
10.内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防 误操作
官方地址: http://mp.baomidou.com
代码发布地址: Github: https://github.com/baomidou/mybatis-plus Gitee: https://gitee.com/baomidou/mybatis-plus
文档发布地址: https://baomidou.com/pages/24112f
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-boot-starterartifactId>
- <version>3.5.1version>
- dependency>
- # 应用名称
- spring.application.name=mybatisplus
- #默认的数据源
- spring.datasource.type=com.zaxxer.hikari.HikariDataSource
- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- spring.datasource.url= jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=UTC
- spring.datasource.username=root
- spring.datasource.password=123456
- #显示Sql语句
- mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
- #mybatis-plus.mapper-locations=classpath*:mapper/**/*.xml 默认的配置
- #设置实体类所对应表的同一前缀
- #mybatis-plus.global-config.db-config.table-prefix=t_
- #mybatis-plus.global-config.db-config.table-underline=
- #统一设置主键生成策略
- #mybatis-plus.global-config.db-config.id-type=auto
- #别名
- mybatis-plus.type-aliases-package=top.remained.pojo
-
-
-
注:
mybatisPlus 自动配置了驼峰命名 user_name =userName
mybatisPlus 默认id就是主键
你主键设置的是递增,如果不在配置文件中主键生成策略,默认仍是雪花算法生成的id
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class User {
-
- private Long id;
- private String name;
- private Integer age;
- private String email;
- public User(String name, Integer age, String email) {
- this.name = name;
- this.age = age;
- this.email = email;
- }
- }
- @Mapper
- public interface UserMapper extends BaseMapper
{ -
- }
Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
- /*
- * Copyright (c) 2011-2022, baomidou (jobob@qq.com).
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.baomidou.mybatisplus.core.mapper;
-
- import com.baomidou.mybatisplus.core.conditions.Wrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
- import com.baomidou.mybatisplus.core.toolkit.Constants;
- import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
- import org.apache.ibatis.annotations.Param;
-
- import java.io.Serializable;
- import java.util.Collection;
- import java.util.List;
- import java.util.Map;
-
- /*
- :`
- .:,
- :::,,.
- :: `::::::
- ::` `,:,` .:`
- `:: `::::::::.:` `:';,`
- ::::, .:::` `@++++++++:
- `` :::` @+++++++++++#
- :::, #++++++++++++++`
- ,: `::::::;'##++++++++++
- .@#@;` ::::::::::::::::::::;
- #@####@, :::::::::::::::+#;::.
- @@######+@:::::::::::::. #@:;
- , @@########':::::::::::: .#''':`
- ;##@@@+:##########@::::::::::: @#;.,:.
- #@@@######++++#####'::::::::: .##+,:#`
- @@@@@#####+++++'#####+::::::::` ,`::@#:`
- `@@@@#####++++++'#####+#':::::::::::@.
- @@@@######+++++''#######+##';::::;':,`
- @@@@#####+++++'''#######++++++++++`
- #@@#####++++++''########++++++++'
- `#@######+++++''+########+++++++;
- `@@#####+++++''##########++++++,
- @@######+++++'##########+++++#`
- @@@@#####+++++############++++;
- ;#@@@@@####++++##############+++,
- @@@@@@@@@@@###@###############++'
- @#@@@@@@@@@@@@###################+:
- `@#@@@@@@@@@@@@@@###################'`
- :@#@@@@@@@@@@@@@@@@@##################,
- ,@@@@@@@@@@@@@@@@@@@@################;
- ,#@@@@@@@@@@@@@@@@@@@##############+`
- .#@@@@@@@@@@@@@@@@@@#############@,
- @@@@@@@@@@@@@@@@@@@###########@,
- :#@@@@@@@@@@@@@@@@##########@,
- `##@@@@@@@@@@@@@@@########+,
- `+@@@@@@@@@@@@@@@#####@:`
- `:@@@@@@@@@@@@@@##@;.
- `,'@@@@##@@@+;,`
- ``...``
- _ _ /_ _ _/_. ____ / _
- / / //_//_//_|/ /_\ /_///_/_\ Talk is cheap. Show me the code.
- _/ /
- */
-
- /**
- * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
- *
这个 Mapper 支持 id 泛型
- *
- * @author hubin
- * @since 2016-01-23
- */
- public interface BaseMapper
extends Mapper { -
- /**
- * 插入一条记录
- *
- * @param entity 实体对象
- */
- int insert(T entity);
-
- /**
- * 根据 ID 删除
- *
- * @param id 主键ID
- */
- int deleteById(Serializable id);
-
- /**
- * 根据实体(ID)删除
- *
- * @param entity 实体对象
- * @since 3.4.4
- */
- int deleteById(T entity);
-
- /**
- * 根据 columnMap 条件,删除记录
- *
- * @param columnMap 表字段 map 对象
- */
- int deleteByMap(@Param(Constants.COLUMN_MAP) Map
columnMap) ; -
- /**
- * 根据 entity 条件,删除记录
- *
- * @param queryWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
- */
- int delete(@Param(Constants.WRAPPER) Wrapper
queryWrapper) ; -
- /**
- * 删除(根据ID或实体 批量删除)
- *
- * @param idList 主键ID列表或实体列表(不能为 null 以及 empty)
- */
- int deleteBatchIds(@Param(Constants.COLLECTION) Collection> idList);
-
- /**
- * 根据 ID 修改
- *
- * @param entity 实体对象
- */
- int updateById(@Param(Constants.ENTITY) T entity);
-
- /**
- * 根据 whereEntity 条件,更新记录
- *
- * @param entity 实体对象 (set 条件值,可以为 null)
- * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
- */
- int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper
updateWrapper) ; -
- /**
- * 根据 ID 查询
- *
- * @param id 主键ID
- */
- T selectById(Serializable id);
-
- /**
- * 查询(根据ID 批量查询)
- *
- * @param idList 主键ID列表(不能为 null 以及 empty)
- */
- List
selectBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList); -
- /**
- * 查询(根据 columnMap 条件)
- *
- * @param columnMap 表字段 map 对象
- */
- List
selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap) ; -
- /**
- * 根据 entity 条件,查询一条记录
- *
查询一条记录,例如 qw.last("limit 1") 限制取一条记录, 注意:多条数据会报异常
- *
- * @param queryWrapper 实体对象封装操作类(可以为 null)
- */
- default T selectOne(@Param(Constants.WRAPPER) Wrapper
queryWrapper) { - List
ts = this.selectList(queryWrapper); - if (CollectionUtils.isNotEmpty(ts)) {
- if (ts.size() != 1) {
- throw ExceptionUtils.mpe("One record is expected, but the query result is multiple records");
- }
- return ts.get(0);
- }
- return null;
- }
-
- /**
- * 根据 Wrapper 条件,判断是否存在记录
- *
- * @param queryWrapper 实体对象封装操作类
- * @return
- */
- default boolean exists(Wrapper
queryWrapper) { - Long count = this.selectCount(queryWrapper);
- return null != count && count > 0;
- }
-
- /**
- * 根据 Wrapper 条件,查询总记录数
- *
- * @param queryWrapper 实体对象封装操作类(可以为 null)
- */
- Long selectCount(@Param(Constants.WRAPPER) Wrapper
queryWrapper) ; -
- /**
- * 根据 entity 条件,查询全部记录
- *
- * @param queryWrapper 实体对象封装操作类(可以为 null)
- */
- List
selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper) ; -
- /**
- * 根据 Wrapper 条件,查询全部记录
- *
- * @param queryWrapper 实体对象封装操作类(可以为 null)
- */
- List
-
- /**
- * 根据 Wrapper 条件,查询全部记录
- *
注意: 只返回第一个字段的值
- *
- * @param queryWrapper 实体对象封装操作类(可以为 null)
- */
- List
-
- /**
- * 根据 entity 条件,查询全部记录(并翻页)
- *
- * @param page 分页查询条件(可以为 RowBounds.DEFAULT)
- * @param queryWrapper 实体对象封装操作类(可以为 null)
- */
-
extends IPage> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper queryWrapper) ;
-
- /**
- * 根据 Wrapper 条件,查询全部记录(并翻页)
- *
- * @param page 分页查询条件
- * @param queryWrapper 实体对象封装操作类
- */
-
extends IPage
- }
- //可以对ioc容器自动装配
- @SpringBootTest
- public class MybatisPlusTest {
- @Autowired
- private UserMapper userMapper;
-
- @Test
- public void insert1(){
- // INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
- User user = new User("zz", 20, "123.com");
- int result = userMapper.insert(user);
- System.out.println(result);
- // 虽然是自增id,但是使用的是雪花算法的id
- // 1564895601187151874id
- System.out.println(user.getId()+"id");
-
- }
-
- }
注:这里写1L是因为定义的为long型
- @Test
- public void del(){
- // 通过id删除 DELETE FROM user WHERE id=?
- // int i = userMapper.deleteById(20);
- 1
- // System.out.println(i);
- // Map
map = new HashMap (); - map里面存放的是条件 DELETE FROM user WHERE age = ?
- // map.put("age",20);
- 因为年龄为20的 2个
- // int i1 = userMapper.deleteByMap(map);
- // System.out.println(i1);
- // DELETE FROM user WHERE id IN ( ? , ? )
- List
list = Arrays.asList(1L, 2L); - // 批量删除
- userMapper.deleteBatchIds(list);
-
- }
- @Test
- public void update(){
- // UPDATE user SET name=?, age=?, email=? WHERE id=?
- // 必须对象里存放Id 要不然白给 因为没Id 所以不更改
- User user =new User("zz",18,"456.com");
- userMapper.updateById(user);
- User user1 = new User();
- user1.setAge(16);
- user1.setId(3L);
- // UPDATE user SET age=? WHERE id=? 只改对象里有的数据,没有的字段原值不变
- userMapper.updateById(user1);
- }
- @Test
- public void sel(){
- // 通过id查询 SELECT id,name,age,email FROM user WHERE id=?
- userMapper.selectById(3L);
- // 通过map集合中的条件查询 SELECT id,name,age,email FROM user WHERE age = ?
- Map
map = new HashMap (); - // 条件为 age=16
- map.put("age",16);
- //
- userMapper.selectByMap(map);
- // 批量查询 SELECT id,name,age,email FROM user WHERE id IN ( ? , ? , ? )
- List
list = Arrays.asList(3L,4L,5L); - userMapper.selectBatchIds(list);
- // 通过条件查询一个list集合,若没有条件 则可以设置为null 即查询所有
- userMapper.selectList(null);
- // 调用自己写的 select * from user where id=?
- userMapper.selectMapById(3L);
- }
顶级 Service
- /*
- * Copyright (c) 2011-2022, baomidou (jobob@qq.com).
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.baomidou.mybatisplus.extension.service;
-
- import com.baomidou.mybatisplus.core.conditions.Wrapper;
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.core.toolkit.Assert;
- import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.baomidou.mybatisplus.extension.conditions.query.ChainQuery;
- import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
- import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
- import com.baomidou.mybatisplus.extension.conditions.update.ChainUpdate;
- import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
- import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
- import com.baomidou.mybatisplus.extension.kotlin.KtQueryChainWrapper;
- import com.baomidou.mybatisplus.extension.kotlin.KtUpdateChainWrapper;
- import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
- import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
- import org.springframework.transaction.annotation.Transactional;
-
- import java.io.Serializable;
- import java.util.Collection;
- import java.util.List;
- import java.util.Map;
- import java.util.Objects;
- import java.util.function.Function;
- import java.util.stream.Collectors;
-
- /**
- * 顶级 Service
- *
- * @author hubin
- * @since 2018-06-23
- */
- public interface IService
{ -
- /**
- * 默认批次提交数量
- */
- int DEFAULT_BATCH_SIZE = 1000;
-
- /**
- * 插入一条记录(选择字段,策略插入)
- *
- * @param entity 实体对象
- */
- default boolean save(T entity) {
- return SqlHelper.retBool(getBaseMapper().insert(entity));
- }
-
- /**
- * 插入(批量)
- *
- * @param entityList 实体对象集合
- */
- @Transactional(rollbackFor = Exception.class)
- default boolean saveBatch(Collection
entityList) { - return saveBatch(entityList, DEFAULT_BATCH_SIZE);
- }
-
- /**
- * 插入(批量)
- *
- * @param entityList 实体对象集合
- * @param batchSize 插入批次数量
- */
- boolean saveBatch(Collection
entityList, int batchSize) ; -
- /**
- * 批量修改插入
- *
- * @param entityList 实体对象集合
- */
- @Transactional(rollbackFor = Exception.class)
- default boolean saveOrUpdateBatch(Collection
entityList) { - return saveOrUpdateBatch(entityList, DEFAULT_BATCH_SIZE);
- }
-
- /**
- * 批量修改插入
- *
- * @param entityList 实体对象集合
- * @param batchSize 每次的数量
- */
- boolean saveOrUpdateBatch(Collection
entityList, int batchSize) ; -
- /**
- * 根据 ID 删除
- *
- * @param id 主键ID
- */
- default boolean removeById(Serializable id) {
- return SqlHelper.retBool(getBaseMapper().deleteById(id));
- }
-
- /**
- * 根据 ID 删除
- *
- * @param id 主键(类型必须与实体类型字段保持一致)
- * @param useFill 是否启用填充(为true的情况,会将入参转换实体进行delete删除)
- * @return 删除结果
- * @since 3.5.0
- */
- default boolean removeById(Serializable id, boolean useFill) {
- throw new UnsupportedOperationException("不支持的方法!");
- }
-
- /**
- * 根据实体(ID)删除
- *
- * @param entity 实体
- * @since 3.4.4
- */
- default boolean removeById(T entity) {
- return SqlHelper.retBool(getBaseMapper().deleteById(entity));
- }
-
- /**
- * 根据 columnMap 条件,删除记录
- *
- * @param columnMap 表字段 map 对象
- */
- default boolean removeByMap(Map
columnMap) { - Assert.notEmpty(columnMap, "error: columnMap must not be empty");
- return SqlHelper.retBool(getBaseMapper().deleteByMap(columnMap));
- }
-
- /**
- * 根据 entity 条件,删除记录
- *
- * @param queryWrapper 实体包装类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default boolean remove(Wrapper
queryWrapper) { - return SqlHelper.retBool(getBaseMapper().delete(queryWrapper));
- }
-
- /**
- * 删除(根据ID 批量删除)
- *
- * @param list 主键ID或实体列表
- */
- default boolean removeByIds(Collection> list) {
- if (CollectionUtils.isEmpty(list)) {
- return false;
- }
- return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list));
- }
-
- /**
- * 批量删除
- *
- * @param list 主键ID或实体列表
- * @param useFill 是否填充(为true的情况,会将入参转换实体进行delete删除)
- * @return 删除结果
- * @since 3.5.0
- */
- @Transactional(rollbackFor = Exception.class)
- default boolean removeByIds(Collection> list, boolean useFill) {
- if (CollectionUtils.isEmpty(list)) {
- return false;
- }
- if (useFill) {
- return removeBatchByIds(list, true);
- }
- return SqlHelper.retBool(getBaseMapper().deleteBatchIds(list));
- }
-
- /**
- * 批量删除(jdbc批量提交)
- *
- * @param list 主键ID或实体列表(主键ID类型必须与实体类型字段保持一致)
- * @return 删除结果
- * @since 3.5.0
- */
- @Transactional(rollbackFor = Exception.class)
- default boolean removeBatchByIds(Collection> list) {
- return removeBatchByIds(list, DEFAULT_BATCH_SIZE);
- }
-
- /**
- * 批量删除(jdbc批量提交)
- *
- * @param list 主键ID或实体列表(主键ID类型必须与实体类型字段保持一致)
- * @param useFill 是否启用填充(为true的情况,会将入参转换实体进行delete删除)
- * @return 删除结果
- * @since 3.5.0
- */
- @Transactional(rollbackFor = Exception.class)
- default boolean removeBatchByIds(Collection> list, boolean useFill) {
- return removeBatchByIds(list, DEFAULT_BATCH_SIZE, useFill);
- }
-
- /**
- * 批量删除(jdbc批量提交)
- *
- * @param list 主键ID或实体列表
- * @param batchSize 批次大小
- * @return 删除结果
- * @since 3.5.0
- */
- default boolean removeBatchByIds(Collection> list, int batchSize) {
- throw new UnsupportedOperationException("不支持的方法!");
- }
-
- /**
- * 批量删除(jdbc批量提交)
- *
- * @param list 主键ID或实体列表
- * @param batchSize 批次大小
- * @param useFill 是否启用填充(为true的情况,会将入参转换实体进行delete删除)
- * @return 删除结果
- * @since 3.5.0
- */
- default boolean removeBatchByIds(Collection> list, int batchSize, boolean useFill) {
- throw new UnsupportedOperationException("不支持的方法!");
- }
-
- /**
- * 根据 ID 选择修改
- *
- * @param entity 实体对象
- */
- default boolean updateById(T entity) {
- return SqlHelper.retBool(getBaseMapper().updateById(entity));
- }
-
- /**
- * 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
- *
- * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
- */
- default boolean update(Wrapper
updateWrapper) { - return update(null, updateWrapper);
- }
-
- /**
- * 根据 whereEntity 条件,更新记录
- *
- * @param entity 实体对象
- * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
- */
- default boolean update(T entity, Wrapper
updateWrapper) { - return SqlHelper.retBool(getBaseMapper().update(entity, updateWrapper));
- }
-
- /**
- * 根据ID 批量更新
- *
- * @param entityList 实体对象集合
- */
- @Transactional(rollbackFor = Exception.class)
- default boolean updateBatchById(Collection
entityList) { - return updateBatchById(entityList, DEFAULT_BATCH_SIZE);
- }
-
- /**
- * 根据ID 批量更新
- *
- * @param entityList 实体对象集合
- * @param batchSize 更新批次数量
- */
- boolean updateBatchById(Collection
entityList, int batchSize) ; -
- /**
- * TableId 注解存在更新记录,否插入一条记录
- *
- * @param entity 实体对象
- */
- boolean saveOrUpdate(T entity);
-
- /**
- * 根据 ID 查询
- *
- * @param id 主键ID
- */
- default T getById(Serializable id) {
- return getBaseMapper().selectById(id);
- }
-
- /**
- * 查询(根据ID 批量查询)
- *
- * @param idList 主键ID列表
- */
- default List
listByIds(Collection extends Serializable> idList) { - return getBaseMapper().selectBatchIds(idList);
- }
-
- /**
- * 查询(根据 columnMap 条件)
- *
- * @param columnMap 表字段 map 对象
- */
- default List
listByMap(Map columnMap) { - return getBaseMapper().selectByMap(columnMap);
- }
-
- /**
- * 根据 Wrapper,查询一条记录
- *
结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last("LIMIT 1")
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default T getOne(Wrapper
queryWrapper) { - return getOne(queryWrapper, true);
- }
-
- /**
- * 根据 Wrapper,查询一条记录
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- * @param throwEx 有多个 result 是否抛出异常
- */
- T getOne(Wrapper
queryWrapper, boolean throwEx) ; -
- /**
- * 根据 Wrapper,查询一条记录
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- Map
getMap(Wrapper queryWrapper) ; -
- /**
- * 根据 Wrapper,查询一条记录
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- * @param mapper 转换函数
- */
-
V getObj(Wrapper queryWrapper, Function super Object, V> mapper) ; -
- /**
- * 查询总记录数
- *
- * @see Wrappers#emptyWrapper()
- */
- default long count() {
- return count(Wrappers.emptyWrapper());
- }
-
- /**
- * 根据 Wrapper 条件,查询总记录数
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default long count(Wrapper
queryWrapper) { - return SqlHelper.retCount(getBaseMapper().selectCount(queryWrapper));
- }
-
- /**
- * 查询列表
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default List
list(Wrapper queryWrapper) { - return getBaseMapper().selectList(queryWrapper);
- }
-
- /**
- * 查询所有
- *
- * @see Wrappers#emptyWrapper()
- */
- default List
list() { - return list(Wrappers.emptyWrapper());
- }
-
- /**
- * 翻页查询
- *
- * @param page 翻页对象
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default
extends IPage> E page(E page, Wrapper queryWrapper) { - return getBaseMapper().selectPage(page, queryWrapper);
- }
-
- /**
- * 无条件翻页查询
- *
- * @param page 翻页对象
- * @see Wrappers#emptyWrapper()
- */
- default
extends IPage> E page(E page) { - return page(page, Wrappers.emptyWrapper());
- }
-
- /**
- * 查询列表
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default List
- return getBaseMapper().selectMaps(queryWrapper);
- }
-
- /**
- * 查询所有列表
- *
- * @see Wrappers#emptyWrapper()
- */
- default List
- return listMaps(Wrappers.emptyWrapper());
- }
-
- /**
- * 查询全部记录
- */
- default List
- return listObjs(Function.identity());
- }
-
- /**
- * 查询全部记录
- *
- * @param mapper 转换函数
- */
- default
List listObjs(Function super Object, V> mapper) { - return listObjs(Wrappers.emptyWrapper(), mapper);
- }
-
- /**
- * 根据 Wrapper 条件,查询全部记录
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default List
- return listObjs(queryWrapper, Function.identity());
- }
-
- /**
- * 根据 Wrapper 条件,查询全部记录
- *
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- * @param mapper 转换函数
- */
- default
List listObjs(Wrapper queryWrapper, Function super Object, V> mapper) { - return getBaseMapper().selectObjs(queryWrapper).stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toList());
- }
-
- /**
- * 翻页查询
- *
- * @param page 翻页对象
- * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
- */
- default
extends IPage - return getBaseMapper().selectMapsPage(page, queryWrapper);
- }
-
- /**
- * 无条件翻页查询
- *
- * @param page 翻页对象
- * @see Wrappers#emptyWrapper()
- */
- default
extends IPage - return pageMaps(page, Wrappers.emptyWrapper());
- }
-
- /**
- * 获取对应 entity 的 BaseMapper
- *
- * @return BaseMapper
- */
- BaseMapper
getBaseMapper(); -
- /**
- * 获取 entity 的 class
- *
- * @return {@link Class
} - */
- Class
getEntityClass(); -
- /**
- * 以下的方法使用介绍:
- *
- * 一. 名称介绍
- * 1. 方法名带有 query 的为对数据的查询操作, 方法名带有 update 的为对数据的修改操作
- * 2. 方法名带有 lambda 的为内部方法入参 column 支持函数式的
- * 二. 支持介绍
- *
- * 1. 方法名带有 query 的支持以 {@link ChainQuery} 内部的方法名结尾进行数据查询操作
- * 2. 方法名带有 update 的支持以 {@link ChainUpdate} 内部的方法名为结尾进行数据修改操作
- *
- * 三. 使用示例,只用不带 lambda 的方法各展示一个例子,其他类推
- * 1. 根据条件获取一条数据: `query().eq("column", value).one()`
- * 2. 根据条件删除一条数据: `update().eq("column", value).remove()`
- *
- */
-
- /**
- * 链式查询 普通
- *
- * @return QueryWrapper 的包装类
- */
- default QueryChainWrapper
query() { - return ChainWrappers.queryChain(getBaseMapper());
- }
-
- /**
- * 链式查询 lambda 式
- *
注意:不支持 Kotlin
- *
- * @return LambdaQueryWrapper 的包装类
- */
- default LambdaQueryChainWrapper
lambdaQuery() { - return ChainWrappers.lambdaQueryChain(getBaseMapper());
- }
-
- /**
- * 链式查询 lambda 式
- * kotlin 使用
- *
- * @return KtQueryWrapper 的包装类
- */
- default KtQueryChainWrapper
ktQuery() { - return ChainWrappers.ktQueryChain(getBaseMapper(), getEntityClass());
- }
-
- /**
- * 链式查询 lambda 式
- * kotlin 使用
- *
- * @return KtQueryWrapper 的包装类
- */
- default KtUpdateChainWrapper
ktUpdate() { - return ChainWrappers.ktUpdateChain(getBaseMapper(), getEntityClass());
- }
-
- /**
- * 链式更改 普通
- *
- * @return UpdateWrapper 的包装类
- */
- default UpdateChainWrapper
update() { - return ChainWrappers.updateChain(getBaseMapper());
- }
-
- /**
- * 链式更改 lambda 式
- *
注意:不支持 Kotlin
- *
- * @return LambdaUpdateWrapper 的包装类
- */
- default LambdaUpdateChainWrapper
lambdaUpdate() { - return ChainWrappers.lambdaUpdateChain(getBaseMapper());
- }
-
- /**
- *
- * 根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法
- * 此次修改主要是减少了此项业务代码的代码量(存在性验证之后的saveOrUpdate操作)
- *
- *
- * @param entity 实体对象
- */
- default boolean saveOrUpdate(T entity, Wrapper
updateWrapper) { - return update(entity, updateWrapper) || saveOrUpdate(entity);
- }
- }
自己的接口应该定义为
- public interface UserService extends IService
{ - }
其实现类写成
- @Service
- public class UserServiceImpl extends ServiceImpl
implements UserService { - //
第一个要操作的Mapper接口 第二个要操作的类型 - }
- @SpringBootTest
- public class MybatisPlusServiceTest {
- @Autowired
- UserService userService;
- @Test
- public void count(){
- // 总记录数版本3.3 SELECT COUNT( 1 ) FROM user
- // 版本3.5SELECT COUNT( * ) FROM user
- long count = userService.count();
- System.out.println(count);
- }
- @Test
- public void insert(){
- // mapper里面并没有批量添加 原因:sql语句长
- // save是保存 saveOrUpdate既有修改又有添加
- // 区分:看实体类中有无id有id就是修改 没id就是添加
- List
list = new ArrayList<>(); - for (int i = 0; i <10 ; i++) {
- User user = new User();
- user.setAge(16+i);
- user.setName("gg"+i);
- list.add(user);
- }
- // INSERT INTO user ( id, name, age ) VALUES ( ?, ?, ? )
- // 为什么不是批量添加sql 它是基于BaseMapper实现的 它是基于单个循环添加
- boolean b = userService.saveBatch(list);
-
- }
- }
insert变为save
saveOrUpdate 既有修改又有添加
区分:看实体类中有无id有id就是修改 没id就是添加

实体类所对应的数据库表名 "user"
- /*
- * Copyright (c) 2011-2022, baomidou (jobob@qq.com).
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.baomidou.mybatisplus.annotation;
-
- import java.lang.annotation.*;
-
- /**
- * 数据库表相关
- *
- * @author hubin, hanchunlin
- * @since 2016-01-23
- */
- @Documented
- @Retention(RetentionPolicy.RUNTIME)
- @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
- public @interface TableName {
-
- /**
- * 实体对应的表名
- */
- String value() default "";
-
- /**
- * schema
- *
- * 配置此值将覆盖全局配置的 schema
- *
- * @since 3.1.1
- */
- String schema() default "";
-
- /**
- * 是否保持使用全局的 tablePrefix 的值
- *
只生效于 既设置了全局的 tablePrefix 也设置了上面 {@link #value()} 的值
- *
- 如果是 false , 全局的 tablePrefix 不生效
- *
- * @since 3.1.1
- */
- boolean keepGlobalPrefix() default false;
-
- /**
- * 实体映射结果集,
- * 只生效于 mp 自动注入的 method
- */
- String resultMap() default "";
-
- /**
- * 是否自动构建 resultMap 并使用,
- * 只生效于 mp 自动注入的 method,
- * 如果设置 resultMap 则不会进行 resultMap 的自动构建并注入,
- * 只适合个别字段 设置了 typeHandler 或 jdbcType 的情况
- *
- * @since 3.1.2
- */
- boolean autoResultMap() default false;
-
- /**
- * 需要排除的属性名
- *
- * @since 3.3.1
- */
- String[] excludeProperty() default {};
- }
注:如果每个表有前缀可以在配置文件中 统一配置
#设置实体类所对应表的同一前缀 #mybatis-plus.global-config.db-config.table-prefix=t_
将属性所对应的字段指定为主键 value=数据库中的字段
不指定type默认id生成是雪花算法 并不是递增
如果自己主动设置id 主动设置的优先
- public @interface TableId {
-
- /**
- * 字段名(该值可无)
- */
- String value() default "";
-
- /**
- * 主键类型
- * {@link IdType}
- */
- IdType type() default IdType.NONE;
- }
类型可以为
- import lombok.Getter;
-
- /**
- * 生成ID类型枚举类
- *
- * @author hubin
- * @since 2015-11-10
- */
- @Getter
- public enum IdType {
- /**
- * 数据库ID自增
- *
该类型请确保数据库设置了 ID自增 否则无效
- */
- AUTO(0),
- /**
- * 该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
- */
- NONE(1),
- /**
- * 用户输入ID
- *
该类型可以通过自己注册自动填充插件进行填充
- */
- INPUT(2),
-
- /* 以下3种类型、只有当插入对象ID 为空,才自动填充。 */
- /**
- * 分配ID (主键类型为number或string),
- * 默认实现类 {@link com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator}(雪花算法)
- *
- * @since 3.3.0
- */
- ASSIGN_ID(3),
- /**
- * 分配UUID (主键类型为 string)
- * 默认实现类 {@link com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator}(UUID.replace("-",""))
- */
- ASSIGN_UUID(4);
-
- private final int key;
-
- IdType(int key) {
- this.key = key;
- }
- }
雪花算法(粗略)
不可能只有一张表 储存的数量太多 因此进行表的拆分 核心优点 保证拆分时不同表的主键的不重复性 主键有序性(后添加的id>之前添加的ID)
也可以在全局配置中统一配置
- #统一设置主键生成策略
- mybatis-plus.global-config.db-config.id-type=auto
注:mybatisplus自动配置了驼峰
普通字段对应的数据中的列名
- public @interface TableField {
-
- /**
- * 数据库字段值
- *
- * 不需要配置该值的情况:
- *
- 当 {@link com.baomidou.mybatisplus.core.MybatisConfiguration#mapUnderscoreToCamelCase} 为 true 时,
- * (mp下默认是true,mybatis默认是false), 数据库字段值.replace("_","").toUpperCase() == 实体属性名.toUpperCase()
- *
- 当 {@link com.baomidou.mybatisplus.core.MybatisConfiguration#mapUnderscoreToCamelCase} 为 false 时,
- * 数据库字段值.toUpperCase() == 实体属性名.toUpperCase()
- */
- String value() default "";
-
- /**
- * 是否为数据库表字段
- *
- * 默认 true 存在,false 不存在
- */
- boolean exist() default true;
-
- /**
- * 字段 where 实体查询比较条件
- *
- * 默认 {@link SqlCondition#EQUAL}
- */
- String condition() default "";
-
- /**
- * 字段 update set 部分注入, 该注解优于 el 注解使用
- *
- * 例1:@TableField(.. , update="%s+1") 其中 %s 会填充为字段
- * 输出 SQL 为:update 表 set 字段=字段+1 where ...
- *
- * 例2:@TableField(.. , update="now()") 使用数据库时间
- * 输出 SQL 为:update 表 set 字段=now() where ...
- */
- String update() default "";
-
- /**
- * 字段验证策略之 insert: 当insert操作时,该字段拼接insert语句时的策略
- *
- * IGNORED: 直接拼接 insert into table_a(column) values (#{columnProperty});
- * NOT_NULL: insert into table_a(
column ) values (#{columnProperty} ) - * NOT_EMPTY: insert into table_a(
column ) values (#{columnProperty} ) - * NOT_EMPTY 如果针对的是非 CharSequence 类型的字段则效果等于 NOT_NULL
- *
- * @since 3.1.2
- */
- FieldStrategy insertStrategy() default FieldStrategy.DEFAULT;
-
- /**
- * 字段验证策略之 update: 当更新操作时,该字段拼接set语句时的策略
- *
- * IGNORED: 直接拼接 update table_a set column=#{columnProperty}, 属性为null/空string都会被set进去
- * NOT_NULL: update table_a set
column=#{columnProperty} - * NOT_EMPTY: update table_a set
column=#{columnProperty} - * NOT_EMPTY 如果针对的是非 CharSequence 类型的字段则效果等于 NOT_NULL
- *
- * @since 3.1.2
- */
- FieldStrategy updateStrategy() default FieldStrategy.DEFAULT;
-
- /**
- * 字段验证策略之 where: 表示该字段在拼接where条件时的策略
- *
- * IGNORED: 直接拼接 column=#{columnProperty}
- * NOT_NULL:
column=#{columnProperty} - * NOT_EMPTY:
column=#{columnProperty} - * NOT_EMPTY 如果针对的是非 CharSequence 类型的字段则效果等于 NOT_NULL
- *
- * @since 3.1.2
- */
- FieldStrategy whereStrategy() default FieldStrategy.DEFAULT;
-
- /**
- * 字段自动填充策略
- *
- * 在对应模式下将会忽略 insertStrategy 或 updateStrategy 的配置,等于断言该字段必有值
- */
- FieldFill fill() default FieldFill.DEFAULT;
-
- /**
- * 是否进行 select 查询
- *
- * 大字段可设置为 false 不加入 select 查询范围
- */
- boolean select() default true;
-
- /**
- * 是否保持使用全局的 columnFormat 的值
- *
- * 只生效于 既设置了全局的 columnFormat 也设置了上面 {@link #value()} 的值
- * 如果是 false , 全局的 columnFormat 不生效
- *
- * @since 3.1.1
- */
- boolean keepGlobalFormat() default false;
-
- /**
- * {@link ResultMapping#property} and {@link ParameterMapping#property}
- *
- * @since 3.4.4
- */
- String property() default "";
-
- /**
- * JDBC类型 (该默认值不代表会按照该值生效),
- * 只生效于 mp 自动注入的 method,
- * 建议配合 {@link TableName#autoResultMap()} 一起使用
- *
- * {@link ResultMapping#jdbcType} and {@link ParameterMapping#jdbcType}
- *
- * @since 3.1.2
- */
- JdbcType jdbcType() default JdbcType.UNDEFINED;
-
- /**
- * 类型处理器 (该默认值不代表会按照该值生效),
- * 只生效于 mp 自动注入的 method,
- * 建议配合 {@link TableName#autoResultMap()} 一起使用
- *
- * {@link ResultMapping#typeHandler} and {@link ParameterMapping#typeHandler}
- *
- * @since 3.1.2
- */
- Class extends TypeHandler> typeHandler() default UnknownTypeHandler.class;
-
- /**
- * 只在使用了 {@link #typeHandler()} 时判断是否辅助追加 javaType
- *
- * 一般情况下不推荐使用
- * {@link ParameterMapping#javaType}
- *
- * @since 3.4.0 @2020-07-23
- */
- boolean javaType() default false;
-
- /**
- * 指定小数点后保留的位数,
- * 只生效于 mp 自动注入的 method,
- * 建议配合 {@link TableName#autoResultMap()} 一起使用
- *
- * {@link ParameterMapping#numericScale}
- *
- * @since 3.1.2
- */
- String numericScale() default "";
- }

- public @interface TableLogic {
-
- /**
- * 默认逻辑未删除值(该值可无、会自动获取全局配置)
- */
- String value() default "";
-
- /**
- * 默认逻辑删除值(该值可无、会自动获取全局配置)
- */
- String delval() default "";
- }
逻辑删除的字段 数据库中也要添加 删除变为修改,只不过你查不出来 因为默认查询时会有where id_del=0

Wrapper : 条件构造抽象类,最顶端父类
AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
QueryWrapper : 查询条件封装
UpdateWrapper : Update 条件封装
AbstractLambdaWrapper : 使用Lambda 语法
LambdaQueryWrapper :用于Lambda语法使用的查询Wrapper
LambdaUpdateWrapper : Lambda 更新封装Wrappe
1.对象封装操作类
2.用于组装查询条件
3.调每一个方法返回值都是用的QueryWrapper
4.删除和查询都是用QueryWrapper
5. 默认条件之间拼接and
6.lambda条件表达式优先执行
- @Autowired
- UserMapper userMapper;
-
- @Test
- public void t1(){
-
- QueryWrapper
wrapper = new QueryWrapper<>(); - // ge>= le<=
- // 第一个是字段名 调每一个方法返回值都是用的QueryWrapper
- // 所以是链式结构
- // 用户名包含a,年龄在20-30之间,邮箱不为空
- QueryWrapper
w = wrapper.like("name", "a") - .between("age", 20, 30)
- .isNotNull("email");
- //SELECT id,name,age,email FROM user
- //WHERE (name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL)
- // %a%(String), 20(Integer), 30(Integer)
- userMapper.selectList(w);
-
- }
- @Test
- public void t2(){
-
- QueryWrapper
wrapper = new QueryWrapper<>(); - // 查询用户信息 按照年龄降序排, 年龄相同id升序
- // SELECT id,name,age,email FROM user ORDER BY age DESC,id ASC
- wrapper.orderByDesc("age")
- .orderByAsc("id");
- userMapper.selectList(wrapper);
- }
- // 删除和查询都是用QueryWrapper
- @Test
- public void del(){
- QueryWrapper
wrapper = new QueryWrapper<>(); - // DELETE FROM user WHERE (age IS NULL AND email IS NULL)
- QueryWrapper
w = wrapper.isNull("age").isNull("email"); - userMapper.delete(w);
- }
- @Test
- public void update(){
- // 2个参数 第一个为修改为的实体类 第二个是修改条件
- QueryWrapper
wrapper = new QueryWrapper<>(); - // UPDATE user SET name=?, age=?, email=? WHERE (name LIKE ? OR age BETWEEN ? AND ?)
- User user = new User("mm",21,"789.com");
- wrapper.like("name", "a").or().between("age", 20, 30);
- userMapper.update(user,wrapper);
- // 1个参数 即第一个设置为null 第二个即是条件也是要修改的内容
- // UPDATE user SET name=? WHERE (name LIKE ?)
- UpdateWrapper
wrapper1 = new UpdateWrapper<>(); - wrapper1.set("name","hh").like("name","a");
- userMapper.update(null,wrapper1);
-
-
- }
- @Test
- public void grade(){
- // 默认条件之间拼接and
- // 用户名包含a 且(年龄大于20或邮箱为null)
- // lambda条件表达式优先执行
- UpdateWrapper
wrapper = new UpdateWrapper<>(); - // UPDATE user SET name=?, age=?, email=?
- // WHERE (name LIKE ? AND (age > ? OR email IS NULL))
- //
- wrapper.like("name","a")
- .and(i->i.gt("age",20).or().isNull("email"));
- userMapper.update(new User("mm",23,"147.com"),wrapper);
-
- }
- @Test
- public void partColumn(){
- // 查询部分字段
- QueryWrapper
wrapper = new QueryWrapper<>(); - // SELECT name,age FROM user
- wrapper.select("name","age");
- userMapper.selectList(wrapper);
- }
- @Test
- public void sonSelect(){
- // 子查询 id <=100
- QueryWrapper
wrapper = new QueryWrapper<>(); - // 第一个参数 字段名 第二个 参数 条件
- // SELECT id,name,age,email FROM user
- // WHERE (age IN (select id from user where id<=100))
- wrapper.inSql("age","select id from user where id<=100");
- userMapper.selectList(wrapper);
-
- }
- @Test
- public void judge(){
- // 动态sql
-
- String name="";
- Integer ageBegin = 20;
- Integer ageEnd = 40;
- QueryWrapper
wrapper = new QueryWrapper<>(); - // SELECT id,name,age,email FROM user WHERE (age >= ? AND age <= ?)
- if (StringUtils.isNotBlank(name)){
- // isNotBlank 判断是否不为空 不为null 不为空白符
- wrapper.like("name",name);
- }
- if (ageBegin !=null){
- wrapper.ge("age",20);
- }
- if (ageEnd !=null){
- wrapper.le("age",ageEnd);
- }
- userMapper.selectList(wrapper);
- }
通过条件判断的condition
- @Test
- public void simple(){
- // 通过条件判断的condition condition 为true执行 反之不执行该条件
- String name="";
- Integer ageBegin = null;
- Integer ageEnd = 40;
- QueryWrapper
wrapper = new QueryWrapper<>(); - // SELECT id,name,age,email FROM user WHERE (age <= ?)
- // StringUtils 封装了很多关于String的判断方法
- wrapper.like(StringUtils.isNotBlank(name),"name",name)
- .ge(ageBegin != null,"age",ageBegin)
- .le(ageEnd != null,"age",ageEnd);
- userMapper.selectList(wrapper);
- }
- @Test
- public void lambda(){
- String name="";
- Integer ageBegin = 20;
- Integer ageEnd = 40;
- LambdaQueryWrapper
lambdaQueryWrapper = new LambdaQueryWrapper<>(); - // 好处 本来是数据库中的字段 现在通过lambda表达式变为实体类中的属性 防止字段写错
- lambdaQueryWrapper.like(StringUtils.isNotBlank(name),User::getName,name)
- .ge(ageBegin != null,User::getAge,ageBegin)
- .le(ageEnd != null,User::getAge,ageEnd);
- }
MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能
注:实现原理就是先查询所有数据然后拦截第一页的数据进行显示
- @Configuration
- public class MybatisPlusConfig {
- // 配置分页插件
- @Bean
- public MybatisPlusInterceptor mybatisPlusInterceptor(){
- MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
- // 数据库之间的分页不相同 DbType.MYSQL
- interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
-
- return interceptor;
- }
- }
- @Autowired
- private UserMapper userMapper;
- @Autowired
- private ProductMapper productMapper;
- @Test
- public void t1(){
- Page
page = new Page<>(1,3); - // SELECT id,name,age,email FROM user LIMIT ?
- // 只有一个参数,因为是第一页
- userMapper.selectPage(page,null);
- System.out.println(page);
- }
虽然Sql语句不需要Page,但是如果想用MybatisPlus提供的分页 1.参数中就必须有Pagepage, 且放在第一个参数 2.返回值必须是page
1.在Mapper接口里
- @Mapper
- public interface UserMapper extends BaseMapper
{ -
- // 自定义分页插件 通过年龄查出用户信息并分页
- // 虽然Sql语句不需要Page,但是如果想用MybatisPlus提供的分页
- // 1.参数中就必须有Page
page, 且放在第一个参数 - // 2.返回值必须是page
- Page
mySelectPage(@Param("page") Page page, - @Param("age") Integer age);
- }
2.在xml文件中直接sql的筛选条件
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="top.remained.mapper.UserMapper">
-
- <select id="mySelectPage" resultType="User">
- select id,name,age,email from user where age > #{age}
- select>
-
- mapper>
3.使用
- @Test
- public void t2(){
- // SELECT COUNT(*) AS total FROM user WHERE age > ?
- // select id,name,age,email from user where age > ? LIMIT ?,?
- Page
page = new Page(2,3); - userMapper.mySelectPage(page,18);
-
- }
乐观锁:
每次修改之后会核对一下之前的版本号,如果相同修改之后保存,反之进行回滚。
- @Test
- public void te1(){
- // 不用锁 出现问题
- // 查询价格
- Product product = productMapper.selectById(1);
- System.out.println(product.getPrice() + ":小李查询");
- // 100:小李查询
- Product product1 = productMapper.selectById(1);
- // 100:小王查询
- System.out.println(product1.getPrice() + ":小王查询");
- // 小李增加50
- product.setPrice(product.getPrice()+50);
- // 150
- productMapper.updateById(product);
- // 小王-30
- product1.setPrice(product1.getPrice() -30);
- // 70
- productMapper.updateById(product1);
- // 查询改变后的
- System.out.println(productMapper.selectById(1).getPrice()+":最后的");
- // 70
-
- }
1.标识乐观锁字段号

2. 配置乐观锁插件
- @Configuration
- public class MybatisPlusConfig {
- // 配置分页插件
- @Bean
- public MybatisPlusInterceptor mybatisPlusInterceptor(){
- MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
- // 数据库之间的分页不相同
- interceptor.addInnerInterceptor
- (new PaginationInnerInterceptor(DbType.MYSQL));
- // 添加乐观锁机制
- interceptor.addInnerInterceptor
- (new OptimisticLockerInnerInterceptor());
- return interceptor;
- }
- }
- @Test
- public void te2(){
- Product product = productMapper.selectById(1);
- System.out.println(product.getPrice() + ":小李查询");
- // 100:小李查询
- Product product1 = productMapper.selectById(1);
- // 100:小王查询
- System.out.println(product1.getPrice() + ":小王查询");
- // 小李增加50
- product.setPrice(product.getPrice()+50);
- // 150
- productMapper.updateById(product);
- // UPDATE t_product SET name=?, price=?,
- // version=? WHERE id=? AND version=?
- // 小王-30
- product1.setPrice(product1.getPrice() -30);
- // 70
- productMapper.updateById(product1);
- // 查询改变后的
- System.out.println(productMapper.selectById(1)
- .getPrice()+":最后的");
- // 150
- // 最后version为1
- }
小王之所以不成功 版本号不一致
解决:如果获取更行后的返回值=0 重新查询 重新更改
- @Test
- public void te3(){
- Product product = productMapper.selectById(1);
- Product product1 = productMapper.selectById(1);
- product.setPrice(product.getPrice()+50);
- productMapper.updateById(product);
- product1.setPrice(product1.getPrice() -30);
- int i = productMapper.updateById(product1);
- if (i == 0) {
- product1 = productMapper.selectById(1);
- product1.setPrice(product1.getPrice() -30);
- productMapper.updateById(product1);
-
- }
- System.out.println(productMapper.selectById(1).
- getPrice()+":最后的");
- // 120 版本号改变2次
- }
https://baomidou.com/pages/ba5b24/
环境配置
idea连接上数据库

1.选中生成的表

2.生成实体类的配置

3.生成service和mapper的配置

4.


https://baomidou.com/pages/ba5b24/