package
com.chaim.mybatis.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
import com.chaim.mybatis.converter.SysUserConverter;
import com.chaim.mybatis.dto.SysUserDTO;
import com.chaim.mybatis.entitys.SysUser;
import com.chaim.mybatis.mappers.SysUserMapper;
import com.chaim.mybatis.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
/**
* @author chaim
* @description: 用户服务接口实现
* @date 2022/7/2 23:05
*/
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl implements SysUserService {
private final SysUserMapper sysUserMapper;
private final SysUserConverter sysUserConverter;
/**
* ------------------ select ------------------------
* BaseMapper: 偏向一些简单的查询操作, 通常配合LambdaQueryWrapper一起使用 (推荐)
* LambdaQueryWrapper: 更灵活, 适用一些判断操作, 复杂的逻辑
* ChainWrappers: 适用于一些简单的查询操作, 可以直接链式调用数据执行操作的方法
*/
@Override
public Object selectById(Long id) {
return sysUserMapper.selectById(id);
}
@Override
public Object selectOne(Long id) {
return ChainWrappers.lambdaQueryChain(sysUserMapper)
.eq(SysUser::getId, id)
.select(SysUser::getUsername, SysUser::getPhone)
.last("limit 1")
.one();
}
@Override
public Object selectPage(Integer page, Integer size) {
return ChainWrappers.lambdaQueryChain(sysUserMapper)
.select(SysUser::getUsername, SysUser::getPhone)
.page(new Page<>(page, size));
}
@Override
public Object selectList(String phone) {
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
if (StringUtils.isNotEmpty(phone)) {
queryWrapper.like(SysUser::getPhone, phone);
}
queryWrapper.select(SysUser::getUsername, SysUser::getPhone);
return sysUserMapper.selectList(queryWrapper);
}
/**
* ------------------ insert ------------------------
*/
@Override
public Object insert(SysUserDTO.InsertSysUserDTO insertSysUserDTO) {
SysUser sysUser = sysUserConverter.insertUserDTOToSysUser(insertSysUserDTO);
sysUser.setPassword("123456");
sysUser.setSex(1);
sysUser.setState(0);
sysUser.setSalt(1234);
return sysUserMapper.insert(sysUser);
}
/**
* ------------------ update ------------------------
* BaseMapper: 适用于更新多列, 以及一些复杂的逻辑
* ChainWrappers: 适用于更新少量内容, 列如状态变更
*/
@Override
public Object updateById(SysUserDTO.UpdateSysUserDTO updateSysUserDTO) {
SysUser sysUser = sysUserConverter.updateUserDTOToSysUser(updateSysUserDTO);
return sysUserMapper.updateById(sysUser);
}
@Override
public Object update(String phone) {
return ChainWrappers.lambdaUpdateChain(sysUserMapper)
.set(SysUser::getPassword, "9878654")
.eq(SysUser::getPhone, phone)
.update();
}
/**
* ------------------ delete ------------------------
* BaseMapper: 适用于ID删除
* Wrappers: 适用于一些复杂的逻辑删除, 指定条件删除等
*/
@Override
public Object deleteById(Long id) {
return sysUserMapper.deleteById(id);
}
@Override
public Object delete(String phone) {
return sysUserMapper.delete(Wrappers.lambdaQuery().eq(SysUser::getPhone, phone));
}
}
package com.chaim.mybatis.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ClassUtils;
import java.time.LocalDateTime;
/**
* @author Chaim
* @date 2022/7/2 18:04
* @Description
*/
@Configuration
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
/**
* 模拟用户ID
*/
private static final Long userId = 1543166049341100034L;
@Override
public void insertFill(MetaObject metaObject) {
log.debug("mybatis plus start insert fill ....");
LocalDateTime now = LocalDateTime.now();
fillValIfNullByName("createTime", now, metaObject, false);
fillValIfNullByName("updateTime", now, metaObject, false);
fillValIfNullByName("createBy", userId, metaObject, false);
fillValIfNullByName("updateBy", userId, metaObject, false);
}
@Override
public void updateFill(MetaObject metaObject) {
log.debug("mybatis plus start update fill ....");
fillValIfNullByName("updateTime", LocalDateTime.now(), metaObject, true);
fillValIfNullByName("updateBy", userId, metaObject, true);
}
/**
* 填充值,先判断是否有手动设置,优先手动设置的值,例如:job必须手动设置
*
* @param fieldName 属性名
* @param fieldVal 属性值
* @param metaObject MetaObject
* @param isCover 是否覆盖原有值,避免更新操作手动入参
*/
private static void fillValIfNullByName(String fieldName, Object fieldVal, MetaObject metaObject, boolean isCover) {
// 1. 没有 get 方法
if (!metaObject.hasSetter(fieldName)) {
return;
}
// 2. 如果用户有手动设置的值
Object userSetValue = metaObject.getValue(fieldName);
if (userSetValue != null && !isCover) {
return;
}
// 3. field 类型相同时设置
Class> getterType = metaObject.getGetterType(fieldName);
if (ClassUtils.isAssignableValue(getterType, fieldVal)) {
metaObject.setValue(fieldName, fieldVal);
}
}
}
com.baomidou.mybatisplus.core.mapper.BaseMapper
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
com.baomidou.mybatisplus.extension.toolkit.ChainWrappers
com.baomidou.mybatisplus.core.toolkit.Wrappers
我在做一些比如根据ID查询删除, 添加, 等会直接使用BaseMapper
当存在一些复杂的逻辑, 比如需要判断条件是否带入SQL时, 会使用LambdaQueryWrapper + BaseMapper
当更新部分数据, 且不复杂会使用ChainWrappers
当使用BaseMapper的方法, 需要传入一些简单的条件, 会使用Wrappers
我个人不推荐使用这种方法:
sysUserMapper.update(null, Wrappers.lambdaUpdate().set(SysUser::getPassword, “9878654”).eq(SysUser::getPhone, phone));
示列代码: https://gitee.com/xmaxm/test-code/tree/master/chaim-mybatis-plus