基于grpc从零开始搭建一个准生产分布式应用(10) - Dao逻辑实现

开始前必读:​​基于grpc从零开始搭建一个准生产分布式应用(0) - quickStart​​ 

整个系列工程部分最后一个拼图了,集成mybatis插件实现数据库写入操作,本章代码任务:1、熟悉mybatis的集成和基础使用;2、打通service和dao层逻辑;

一、工程结构

本章内容需要修改内容比较多,涉及到以下三个工程模块的修改:

  • base-grpc-framework-core:主要改动点是在service中集成dao接口
  • base-grpc-framework-dao:数据库存储实现;
  • base-grpc-framework-application:主要改动点是添加数据库配置;

主要的改动文件如下图红框内所示,本章我们因为DAO是个全新的模块,所以本章会按工程修改的多少来罗列,先app,再core,最后dao:

基于grpc从零开始搭建一个准生产分布式应用(10) - Dao逻辑实现_第1张图片

二、修改启动配置,集成mysql

修改【base-grpc-framework-application】模块的application-dev.yml文件,完整的内容如下,新增了第11到14行:

# http配置
server:
  compression:
    enabled: true
    mime-types: application/json,application/octet-stream

# spring配置
spring:
  application:
    name: GrpcFramework-Server-APP
  aop:
    auto: true
    proxy-target-class: true
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/badCase?characterEncoding=utf8&useSSL=false&serverTimeznotallow=Asia/Shanghai
    username: root
    password: 12345678
    druid:
      initial-size: 20
      min-idle: 20
      max-active: 400
      max-wait: 60000
      validation-query: SELECT 1 FROM DUAL
      max-pool-prepared-statement-per-connection-size: 20
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 100000
      filters: stat

# grpc Server配置
grpc:
  server:
    port: 9898   #发布远程访问地址
    in-process-name: native  #发布本地访问地址
  client:
    inProcess:
      address: in-process:native #配置内部访问服务名称

# mybatis plug 配置
mybatis-plus:
  mapper-locations: classpath:/mybatis/*Mapper.xml
  configuration:
    map-underscore-to-camel-case: true # 开启驼峰命名规则映射
    default-statement-timeout: 10 #超时查询
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql日志

# logging日志配置
logging:
  config: classpath:log4j2.xml
  level:
    root: INFO
    org.springframework.web: ERROR

三、创建必要的实体类

3.1、创建实体类

修改【base-grpc-framework-dao】模块,新创建以下两个实体类:

3.2、完善对象转抽象工具类

修改【base-grpc-framework-core】模块,完整代码如下:

/**
 * @Title: com.zd.baseframework.core.core.systemlog.translator.SystemLogModelTranslator
 * @Description service与dao层数据对象转换工具类
 * @author liudong
 * @date 2022/6/15 9:02 PM
 */
@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED,
        nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
        imports = {MapStructMapperUtil.class},
        uses = BaseMapStructMapper.class)
public interface SystemLogModelTranslator {
    SystemLogModelTranslator INSTANCE = Mappers.getMapper(SystemLogModelTranslator.class);

    SystemLogEntity toEntry(SystemLogBo var);

    /*查询请求参数转换*/
    SystemLogQuery toQueryEntry(SystemLogQueryBo var);

    /*返回结果转换*/
    SystemLogBo toQueryResultBo(SystemLogEntity var);
    List toQueryResultBos(List var);
}

四、core集成dao,实现dao调用

本小节的代码会报错,不过没关系。因为依赖的dao代码会在第5章完善,先红着放着。

4.1、修改pom.xml文件

4.1.1、增加以下数据库依赖

        
            mysql
            mysql-connector-java
        
        
            com.baomidou
            mybatis-plus-boot-starter
        
        
            com.alibaba
            druid-spring-boot-starter
        
4.1.2、添加配置

        
            
                src/main/resources
            
            
                ../base-grpc-framework-dao/src/main/resources/mybatis
                ./mybatis
            
        
4.1.3、完整结构如下


    
        base-grpc-framework-parent
        com.zd
        1.0-SNAPSHOT
    
    4.0.0

    base-grpc-framework-core

    
        0.1.0
    

    
        
        
            net.devh
            grpc-server-spring-boot-starter
        
        
            com.google.protobuf
            protobuf-java-util
        
        
            com.google.protobuf
            protobuf-java
        

        
        
            net.devh
            grpc-client-spring-boot-starter
        

        
        
            com.zd
            base-grpc-framework-common
            ${project.parent.version}
            
                
                    slf4j-log4j12
                    org.slf4j
                
            
        
        
            com.zd
            base-grpc-framework-api
            ${project.parent.version}
        
        
            com.zd
            base-grpc-framework-dao
            ${project.parent.version}
        

        
        
            com.alibaba.boot
            nacos-config-spring-boot-starter
        

        
        
            mysql
            mysql-connector-java
        
        
            com.baomidou
            mybatis-plus-boot-starter
        
        
            com.alibaba
            druid-spring-boot-starter
        

        
        
        
            org.mapstruct
            mapstruct
        
        
            org.mapstruct
            mapstruct-processor
            compile
        

        
        
            org.projectlombok
            lombok
            compile
        

        
        
            cn.hutool
            hutool-all
        

        
        
            org.springframework.boot
            spring-boot-starter-log4j2
        
    

    
        
        
            
                src/main/resources
            
            
                ../base-grpc-framework-dao/src/main/resources/mybatis
                ./mybatis
            
        

        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    ${maven.compiler.source}
                    ${maven.compiler.target}
                    
                        
                            org.projectlombok
                            lombok
                            ${lombok.version}
                        
                        
                            org.mapstruct
                            mapstruct-processor
                            ${mapstruct.version}
                        

                        
                        
                            org.projectlombok
                            lombok-mapstruct-binding
                            ${lombok.mapstruct.binding.version}
                        
                    
                
            
        
    

4.2、service集成dao

完整代码如下:

package com.zd.baseframework.core.core.systemlog.service.impl;


@Service
@Slf4j
@Primary
public class SystemLogServcieImpl implements ISystemLogServcie {

    @Autowired
    private ISystemLogDao iSystemLogDao;
    @Override
    public boolean createSystemLog(SystemLogBo systemLogBo) {
        String trackLog = LogGenerator.trackLog();
        log.info(trackLog
                + "createSystemLog=" + JSONUtil.toJsonStr(systemLogBo)
        );
        try{
            SystemLogEntity entity = SystemLogModelTranslator.INSTANCE.toEntry(systemLogBo);
            return iSystemLogDao.save(entity);

        }catch(Exception e){
            throw e;
        }
    }

    @Override
    public List listByCondition(SystemLogQueryBo systemLogQueryBo) {
        String trackLog = LogGenerator.trackLog();
        try{
            SystemLogQuery entity = SystemLogModelTranslator.INSTANCE.toQueryEntry(systemLogQueryBo);
            //下面这两个DAO接口返回同样的数据,只是为了说明下不同的写法,详细参考dao的实现
            List resut1 = iSystemLogDao.listByConditionV1(entity);
            List resut2 = iSystemLogDao.listByConditionV2(entity);

            log.info(trackLog
                    + "queryParam=" + JSONUtil.toJsonStr(systemLogQueryBo)
                    + "resut1=" + JSONUtil.toJsonStr(resut1)
                    + "resut2=" + JSONUtil.toJsonStr(resut2)
            );

            return SystemLogModelTranslator.INSTANCE.toQueryResultBos(resut1);

        }catch(Exception e){
            throw e;
        }
    }
}

五、实现daon层逻辑

5.0、修改pom.xml文件



    
        base-grpc-framework-parent
        com.zd
        1.0-SNAPSHOT
    
    4.0.0

    base-grpc-framework-dao

    

        
            com.zd
            base-grpc-framework-common
            ${project.parent.version}
            provided
        

    

5.1、定义实体类

package com.zd.baseframework.core.dao.systemlog.entry;

import com.baomidou.mybatisplus.annotation.TableName;
import com.zd.baseframework.common.entity.dao.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName("t_sys_record")
public class SystemLogEntity extends BaseEntity {

    private String bizId;

    private Long userId;

    private String trackUid;

    private String code;

    private String customCode;

    private Integer state;

}
package com.zd.baseframework.core.dao.systemlog.entry;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

@Data
@EqualsAndHashCode
@Accessors(chain = true)
public class SystemLogQuery {
    private String bizId;

    private Long userId;

    private String code;
}

5.2、定义接口和接口实现

5.2.1、定义DAO接口
package com.zd.baseframework.core.dao.systemlog;

import com.baomidou.mybatisplus.extension.service.IService;
import com.zd.baseframework.core.dao.systemlog.entry.SystemLogEntity;
import com.zd.baseframework.core.dao.systemlog.entry.SystemLogQuery;

import java.util.List;

public interface ISystemLogDao  extends IService {

    List listByConditionV1(SystemLogQuery query);

    List listByConditionV2(SystemLogQuery query);

}
5.2.2、DAO接口实现
package com.zd.baseframework.core.dao.systemlog;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zd.baseframework.core.dao.systemlog.entry.SystemLogEntity;
import com.zd.baseframework.core.dao.systemlog.entry.SystemLogQuery;
import com.zd.baseframework.core.dao.systemlog.mapper.SystemLogMapper;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Title: com.zd.baseframework.core.dao.systemlog.SystemLogDaoImpl
 * @Description 这个是最底层服务了,不建议用trycatch,多少可以优化点性能
 * @author liudong
 * @date 2022/6/15 9:05 PM
 */
@Repository
@Primary
public class SystemLogDaoImpl extends ServiceImpl implements ISystemLogDao {
    @Override
    public List listByConditionV1(SystemLogQuery query) {
        LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery();
        if(StrUtil.isNotEmpty(query.getCode())){
            queryWrapper.eq(SystemLogEntity::getCode, query.getCode());
        }
        if(StrUtil.isNotEmpty(query.getBizId())){
            queryWrapper.eq(SystemLogEntity::getBizId, query.getBizId());
        }
        if(query.getUserId()!=null){
            queryWrapper.eq(SystemLogEntity::getUserId, query.getUserId());
        }
        return list(queryWrapper);
    }

    @Override
    public List listByConditionV2(SystemLogQuery query) {
        List entityList = this.getBaseMapper().listByCondition(query);
        return entityList;
    }
}

5.3、实现mybatis逻辑

5.3.1、定义mapper
package com.zd.baseframework.core.dao.systemlog.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zd.baseframework.core.dao.systemlog.entry.SystemLogEntity;
import com.zd.baseframework.core.dao.systemlog.entry.SystemLogQuery;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface SystemLogMapper  extends BaseMapper {

    List listByCondition(@Param("query") SystemLogQuery systemLogQuery);
}
5.3.2、定义mapper.xml

文件路径:src/main/resources/mybatis/SystemLogMapper.xml。





    

mybatis的详细使用可查看后续章节

你可能感兴趣的:(java,grpc,springboot)