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

发布时间:2024年01月02日

开始前必读:??基于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:

二、修改启动配置,集成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<SystemLogBo> toQueryResultBos(List<SystemLogEntity> var);
}

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

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

4.1、修改pom.xml文件

4.1.1、增加以下数据库依赖
<!--mybatis相关依赖,mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
4.1.2、添加配置
<!--resources配置解决mybatis 的mapperXml配置在java路径不被扫描的问题 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>../base-grpc-framework-dao/src/main/resources/mybatis</directory>
                <targetPath>./mybatis</targetPath>
            </resource>
        </resources>
4.1.3、完整结构如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>base-grpc-framework-parent</artifactId>
        <groupId>com.zd</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>base-grpc-framework-core</artifactId>

    <properties>
        <lombok.mapstruct.binding.version>0.1.0</lombok.mapstruct.binding.version>
    </properties>

    <dependencies>
        <!--grpc依赖-->
        <dependency>
            <groupId>net.devh</groupId>
            <artifactId>grpc-server-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java-util</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
        </dependency>

        <!--gprc依赖实现本地grpc调用-->
        <dependency>
            <groupId>net.devh</groupId>
            <artifactId>grpc-client-spring-boot-starter</artifactId>
        </dependency>

        <!--项目依赖-->
        <dependency>
            <groupId>com.zd</groupId>
            <artifactId>base-grpc-framework-common</artifactId>
            <version>${project.parent.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.zd</groupId>
            <artifactId>base-grpc-framework-api</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <dependency>
            <groupId>com.zd</groupId>
            <artifactId>base-grpc-framework-dao</artifactId>
            <version>${project.parent.version}</version>
        </dependency>

        <!--nacos相关-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
        </dependency>

        <!--mybatis相关依赖,mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>

        <!--===========以下全是工具类和工具框架=========================-->
        <!--mapStruct依赖,代码拷贝-->
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <scope>compile</scope>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>compile</scope>
        </dependency>

        <!--hutool工具类-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
        </dependency>

        <!-- 使用log4j2,性能更高 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
    </dependencies>

    <build>
        <!--resources配置解决mybatis 的mapperXml配置在java路径不被扫描的问题 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>../base-grpc-framework-dao/src/main/resources/mybatis</directory>
                <targetPath>./mybatis</targetPath>
            </resource>
        </resources>

        <plugins>
            <!--处理lombok和mapstruft 加载顺序的问题 防止在生成的实体没有属性-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>

                        <!-- additional annotation processor required as of Lombok 1.18.16 -->
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-mapstruct-binding</artifactId>
                            <version>${lombok.mapstruct.binding.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

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<SystemLogBo> listByCondition(SystemLogQueryBo systemLogQueryBo) {
        String trackLog = LogGenerator.trackLog();
        try{
            SystemLogQuery entity = SystemLogModelTranslator.INSTANCE.toQueryEntry(systemLogQueryBo);
            //下面这两个DAO接口返回同样的数据,只是为了说明下不同的写法,详细参考dao的实现
            List<SystemLogEntity> resut1 = iSystemLogDao.listByConditionV1(entity);
            List<SystemLogEntity> 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文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>base-grpc-framework-parent</artifactId>
        <groupId>com.zd</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>base-grpc-framework-dao</artifactId>

    <dependencies>

        <dependency>
            <groupId>com.zd</groupId>
            <artifactId>base-grpc-framework-common</artifactId>
            <version>${project.parent.version}</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

</project>

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<SystemLogEntity> {

    List<SystemLogEntity> listByConditionV1(SystemLogQuery query);

    List<SystemLogEntity> 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<SystemLogMapper, SystemLogEntity> implements ISystemLogDao {
    @Override
    public List<SystemLogEntity> listByConditionV1(SystemLogQuery query) {
        LambdaQueryWrapper<SystemLogEntity> 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<SystemLogEntity> listByConditionV2(SystemLogQuery query) {
        List<SystemLogEntity> 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<SystemLogEntity> {

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

文件路径:src/main/resources/mybatis/SystemLogMapper.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.zd.baseframework.core.dao.systemlog.mapper.SystemLogMapper">

    <select id="listByCondition" resultType="com.zd.baseframework.core.dao.systemlog.entry.SystemLogEntity">
        select t_sys_record.* from t_sys_record
        where 1=1
        <if test="query.bizId != null">
            and t_sys_record.biz_id = #{query.bizId}
        </if>
        <if test="query.userId != null">
            and t_sys_record.user_id = #{query.userId}
        </if>
        <if test="query.code != null">
            and t_sys_record.code = #{query.code}
        </if>
    </select>
</mapper>

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

文章来源:https://blog.csdn.net/liudonglovehemin/article/details/135341045
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。