MyBatis-Plus (简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
@TableName(“表名”) 表名注解
@TableId(“主键字段名”) 主键注解
@TableField(“字段注解”) 字段注解(非主键)
@Version 乐观锁注解
@EnumValue 通枚举类注解(注解在枚举字段上)
@TableLogic 表字段逻辑处理注解(逻辑删除)
allEq:全部eq(或个别isNull]
allEq(Map<R, V> params)
allEq(Map<R, V> params, boolean null2IsNull)
allEq(boolean condition, Map<R, V> params, boolean null2IsNull)
eq:等于 =
eq(R column, Object val)
eq(boolean condition, R column, Object val)
ne:不等于 <>
ne(R column, Object val)
ne(boolean condition, R column, Object val)
gt:大于 >
gt(R column, Object val)
gt(boolean condition, R column, Object val)
ge: 大于等于 >=
ge(R column, Object val)
ge(boolean condition, R column, Object val)
lt: 小于 <
lt(R column, Object val)
lt(boolean condition, R column, Object val)
le:小于等于 <=
le(R column, Object val)
le(boolean condition, R column, Object val)
between:BETWEEN 值1 AND 值2
between(R column, Object val1, Object val2)
between(boolean condition, R column, Object val1, Object val2)
notBetween:NOT BETWEEN 值1 AND 值2
notBetween(R column, Object val1, Object val2)
notBetween(boolean condition, R column, Object val1, Object val2)
like:LIKE ‘%值%’
like(R column, Object val)
like(boolean condition, R column, Object val)
notLike:NOT LIKE ‘%值%’
notLike(R column, Object val)
notLike(boolean condition, R column, Object val)
isNull:字段为空
isNull(R column)
isNull(boolean condition, R column)
isNotNull:字段非空
isNotNull(R column)
isNotNull(boolean condition, R column)
获取 LambdaWrapper
在QueryWrapper
中是获取LambdaQueryWrapper
在UpdateWrapper
中是获取LambdaUpdateWrapper
LambdaQueryWrapper<T> lambda = new QueryWrapper<User>().lambda();
LambdaQueryWrapper<T> lambdaQueryWrapper = new LambdaQueryWrapper<T>();
LambdaQueryWrapper<T> lambdaQuery = Wrapper.<T>lambdaQuery();
使用Wrapper自定义SQL,在MP3.0.7版本后实现了可自定义SQL
方案一:注解方式Mapper.java
@Select("select * from mysql_data ${ew.customSqlSegment}")
List<MysqlData> getAll(@Param(Constants.WRAPPER) Wrapper wrapper);
方案二:XML形式Mapper.xml
<select id="getAll" resultType="MysqlData">
SELECT * FROM mysql_data ${ew.customSqlSegment}
select>
<property name="plugins">
<array>
<bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
<property name="sqlParser" ref="自定义解析类、可以没有"/>
<property name="dialectClazz" value="自定义方言类、可以没有"/>
<property name="countSqlParser" ref="countSqlParser"/>
bean>
array>
property>
<bean id="countSqlParser" class="com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize">
<property name="optimizeJoin" value="true"/>
bean>
//Spring boot方式
@EnableTransactionManagement
@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;
}
}
//UserMapper.java 方式查询
public interface UserMapper {//可以继承或者不继承BaseMapper
/**
*
* 查询 : 根据state状态查询用户列表,分页显示
*
*
* @param page 分页对象,xml中可以从里面进行取值,传递参数 Page 即自动分页,必须放在第一位(你可以继承Page实现自己的分页对象)
* @param state 状态
* @return 分页对象
*/
IPage<User> selectPageVo(Page<?> page, Integer state);
}
<-- Usermapper.xml 等同于编写一个普通list查询,MP自动替你分页 -->
<select id="selectPageVo" resultType="com.baomidou.cloud.entity.UserVo">
SELECT id,name FROM user WHERE state=#{state}
select>
//UserServiceImpl.java 调用分页方法
public IPage<User> selectUserPage(Page<User> page, Integer state) {
// 不进行 count sql 优化,解决 MP 无法自动优化 SQL 问题,这时候你需要自己查询 count 部分
// page.setOptimizeCountSql(false);
// 当 total 为小于 0 或者设置 setSearchCount(false) 分页插件不会进行 count 查询
// 要点!! 分页返回的对象与传入的对象是同一个
return userMapper.selectPageVo(page, state);
}
//更新
@Test
public void updateById(){
User user = new User();
user.setId(1l);
user.setAge(26);
user.setEmail("[email protected]");
int rows = userMapper.updateById(user);
}
@Test
public void updateByWrapper(){
UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>();
updateWrapper.eq("name","李").eq("age",28);
User user = new User();
user.setEmail("[email protected]");
user.setAge(29);
userMapper.update(user,updateWrapper);
}
//更新少量字段时
@Test
public void updateByWrapper(){
UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>();
updateWrapper.eq("name","李").eq("age",28).set("age",30);
userMapper.update(user,updateWrapper);
}
@Test
public void updateByWrapperLambda(){
LambdaUpdateWrapper<User> lambdaUpdate = Wrappers.<User>lambdaUpdate();
lambdaUpdate.eq(User::getName,"李").eq(User::getAge,29).set(User::Age,30);
}
//删除
@Test
public void deleteByMap(){
Map<String,Object> columnMap = new HashMap<>();
columnMap.put("name","李");
columnMap.put("age",32);
}
//批量删除
@Test
public void deleteBatchids(){
UserMapper userMapper = new UserMapper();
userMapper.deleteBatchids(Arrays.asList(1l,2l,3l));
}
@Test
public void deleteByWrapper(){
LambdaQuertyWrapper<User> lambdaQuery = Wrapper.<User>lLambdaQuery();
lambdaQuery.eq(User::getAge,27).or().gt(User::getAGe,40);
userMapper.delete(lambdaQuery);
}