xinchan _ 考勤 _ 数据层

一、条件构造器

1、网:Wrapper.java层次结构图

2、网:Wrapper包含的方法

3、网:Wrapper相关语法

查询方式	                                          说明
setSqlSelect	                            设置 SELECT 查询字段
where	                                    WHERE 语句,拼接 + WHERE 条件
and											AND语句,拼接 + AND 字段=值
andNew										AND 语句,拼接 + AND (字段=值)
or											OR 语句,拼接 + OR 字段=值
orNew										OR 语句,拼接 + OR (字段=值)
eq											等于=
allEq										基于 map 内容等于=
ne											不等于<>
gt											大于>
ge											大于等于>=
lt											小于<
le											小于等于<=
like										模糊查询 LIKE
notLike										模糊查询 NOT LIKE
in											IN 查询
notIn										NOT IN 查询
isNull										NULL 值查询
isNotNull									IS NOT NULL
groupBy										分组 GROUP BY
having										HAVING 关键词
orderBy										排序 ORDER BY
orderAsc									ASC 排序 ORDER BY
orderDesc									DESC 排序 ORDER BY
exists										EXISTS 条件语句
notExists									NOT EXISTS 条件语句
between										BETWEEN 条件语句
notBetween									NOT BETWEEN 条件语句
addFilter									自由拼接 SQL
last										拼接在最后,例如:last("LIMIT 1")
 注意! xxNew 都是另起 ( ... ) 括号包裹。

二、MyBatis-Plus:封装了CRUD

1、网:BaseMapper与ServiceImpl的关系

         先看一下继承结构

        这样看,是不是很神奇,我们继承的ServiceImpl依旧实现了BaseMapper接口和Iservice接口,这就感觉有点啰嗦了,明明我们单独写了RestDeptMapper,并且继承了BaseMapper,现在ServiceImpl还是实现了BaseMapper,那我直接一个Service用下来不就行了,创建两套类,功能相似,还容易混乱,代码结构冗余。

        本着“存在即合理”的理念,我们对比一下两个接口的方法。

果然,Service简直是BaseMapper的大扩充,不但包含了所有基本方法,还加入了很多批处理功能,我们可以看一下官网对这两种接口的说明:

综合来看,比较合理的解释是:

  1. 从分层角度来解释,BaseMapper是DAO层的CRUD封装,而IService是业务业务逻辑层的CRUD封装,所以多了批量增、删、改的操作封装,这也比较符合官方指南中的阐述;
  2. IService是对BaseMapper的扩展,从BaseMapper、IService、ServiceImpl三者的类关系以及源码可以看出;
  3. 此外,个人认为应该还有一个原因,就是IService和BaseMapper提供的是两种实现方式:如果继承BaseMapper,则不需要去实现其内部方法,依靠mybatis的动态代理即可实现CRUD操作;而如果自定义IBaseService去继承IService,则需要去实现IService中的方法;

        最后本文还是比较水的,只是简单的看了一下结构而已,没有太多的深入,总结一下,以我平时粘贴复制的经验来看,Service虽然加入了数据库的操作,但还是以业务功能为主,而更加复杂的SQL查询,还是要靠Mapper对应的XML文件里去编写SQL语句

参考:

        关于mybatis-plus中Service和Mapper的分析 - rain1024 - 博客园 (cnblogs.com)

        Mybatis-plus中BaseMapper和IService功能相似重复,为什么要提供两个接口? 

        有关Mybatis-Plus中Service和Mapper的分析 

        Mybatis Plus自定义IService与BaseMapper (nicethemes.cn)

        MyBatis-Plus BaseMapper和IService使用手册 - seer- - 博客园 (cnblogs.com)

2、网:BaseMapper与ServiceImpl最佳实践的工作流程(图)

在这里插入图片描述

如上图所示,先演示一下基本开发中的继承关系,手动创建的Service继承于ServiceImpl,并加载自己创建的Mapper:

@Service
public class RestDeptService extends ServiceImpl<RestDeptMapper, RestDept> {

    @Resource
    private RestDeptMapper restDeptMapper;
    
 }
 
 public interface RestDeptMapper extends BaseMapper<RestDept> {
 
 }

如上,就是一般开发的基本模版代码,足以满足各种需求功能,但点开源码时,便进入新世界的大门。

3、xc:案例1:ServiceImpl.getOne(Wrapper<T> queryWrapper, boolean throwEx)

//  类与接口的层次结构

public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {}

public interface IDeviceService extends IService<Device> {}

public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {}

@Api(tags = {"通行开放接口的控制器"})
public class RecordController {
    @PostMapping("/wg_visit/push")
    @ApiOperation(value = "通行记录-无感考勤推送记录", notes = "通行记录-无感考勤推送记录")
    public Result<Object> savePassWgRecord(@RequestBody WgRecordDTO record) throws IOException, ParseException {
        //  从Wrappers.java中获取LambdaQueryWrapper
        LambdaQueryWrapper<Device> deviceLambdaQueryWrapper = Wrappers.lambdaQuery();
        //  设置查询条件
        deviceLambdaQueryWrapper.eq(Device::getName, record.getAlias()); 
        //  通过IService.getOne进行查询
        Device device = deviceService.getOne(deviceLambdaQueryWrapper);
    }
}

4、xc:案例2:Mapper.selectOne(Wrapper<T> queryWrapper)

//  类、接口、xml文档层次结构

XML文档:RuleMapper.xml
public interface RuleMapper extends BaseMapper<Rule> {}

@Api(tags = {"通行开放接口的控制器"})
public class RecordController {
    //  注入XxxMapper
    private final RuleMapper ruleMapper;
    
    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void updateEmployee(EmployeeDTO dto) {
        //  调用selectOne()   
          ruleList = ruleMapper.selectOne(new QueryWrapper<Rule>().lambda()
                         .eq(Rule::getAreaId, employee.getAreaId())
                         .eq(Rule::getCompanyId, employee.getCompanyId())
                         .eq(Rule::getClassCode, employee.getClassCode())
                         .eq(Rule::getClassPermissionId, "1".equals(employee.getRuleTypes()) ? "1" : "3")
                         .eq(Rule::getStatus, 1));
    }
}

5、xc:案例3:mybatis动态代理,调用方式1

//  类、接口、xml文档层次结构

XML文档:ExtendCscpRolesMapper.xml
工具类:public class SpringUtil implements ApplicationContextAware {} 
public interface ExtendCscpRolesMapper extends BaseMapper<CscpRoles> {}

//  Controller中调用
@Api(tags = {"通行开放接口的控制器"})
public class RecordController {
        public void clockIn(Employee employee, ClockRecordDTO record, String type) {
            //  获取XxxMapper对象
            ExtendCscpRolesMapper     extendCscpRolesMapper=SpringUtil.getBean(ExtendCscpRolesMapper.class);
            //  调用对象的方法
            List<CscpRoles> roleList = CscpRolesMapper.findRolesByUserId(employee.getUserId());
    }
}

有ExtendCscpRolesMapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ctsi.ssdc.admin.mapper.ExtendCscpRolesMapper">

    <select id="findRolesByUserId" resultType="com.ctsi.ssdc.admin.domain.CscpRoles">
        select DISTINCT r.* from cscp_user_role ur,cscp_roles r where ur.role_id=r.id and user_id=#{userId }
    </select>
</mapper>

6、xc:案例4:mybatis动态代理,调用方式2

XML文档:BusinessTemperatureRecordMapper.xml

public class RecordController {    
    //  注入Mapper
    private final BusinessTemperatureRecordMapper businessTemperatureRecordMapper;
    @PostMapping("/employee_gps_list/find")
    @ApiOperation(value = "获取员工打卡位置列表", notes = "获取员工打卡位置列表")
    public Result findEmployeeGpsListById() throws IOException, ParseException {
        //  调用自定义方法
        BusinessTemperatureRecord temperatureFirst = businessTemperatureRecordMapper.getTemperatureRecordFirst(dto.setUserId(employee.getId()));
    }
}

三、MyBatis-Plus精彩博文(大量图片)

        MyBatis-plus 源码解析_谈谈1974的博客

        (157条消息) MyBatis-plus 条件构造器的实现原理_谈谈1974的博客-CSDN博客 

        MyBatis-plus 转化处理 SQL 语句的源码分析_谈谈1974的博客 

        MyBatis-plus 自定义通用方法及其实现原理_谈谈1974的博客

        MyBatis-plus 批量插入的通用方法使用_谈谈1974的博客 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值