开始前必读:基于grpc从零开始搭建一个准生产分布式应用(0) - quickStart
整个系列工程部分最后一个拼图了,集成mybatis插件实现数据库写入操作,本章代码任务:1、熟悉mybatis的集成和基础使用;2、打通service和dao层逻辑;
本章内容需要修改内容比较多,涉及到以下三个工程模块的修改:
主要的改动文件如下图红框内所示,本章我们因为DAO是个全新的模块,所以本章会按工程修改的多少来罗列,先app,再core,最后dao:
修改【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
修改【base-grpc-framework-dao】模块,新创建以下两个实体类:
修改【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);
}
本小节的代码会报错,不过没关系。因为依赖的dao代码会在第5章完善,先红着放着。
mysql
mysql-connector-java
com.baomidou
mybatis-plus-boot-starter
com.alibaba
druid-spring-boot-starter
src/main/resources
../base-grpc-framework-dao/src/main/resources/mybatis
./mybatis
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.target}
org.projectlombok
lombok
${lombok.version}
org.mapstruct
mapstruct-processor
${mapstruct.version}
org.projectlombok
lombok-mapstruct-binding
${lombok.mapstruct.binding.version}
完整代码如下:
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;
}
}
}
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
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;
}
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);
}
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;
}
}
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);
}
文件路径:src/main/resources/mybatis/SystemLogMapper.xml。
mybatis的详细使用可查看后续章节