BeetlSql&ProtoBuffer实践Demo
前言
由于开发架构问题,公司使用的基于SpringBoot+BeetlSql+ProtoBuffer的架子来处理业务,由于BeetlSQL和Protobuffer有些小众,特记录一下。
项目架构介绍
- 本文采用SpringBoot+BeetlSQL+ProtoBuffer进行快速搭建开发
- PDMAN表快速设计与维护
项目准备工作
SQL语句
员工信息表
1 |
|
员工岗位表
1 |
|
相关环境
- jdk 1.8+
- mysql 5.7
- Lombok
项目案例
项目层级
1 |
|
相关依赖
引入springboot父项目依赖
1 |
|
设置相关版本
1 |
|
引入相关依赖
1 |
|
引入ProtoBuffer和Maven编译工具
1 |
|
相关配置
配置application.yml文件
1 |
|
配置dev【application-dev.yml】环境
1 |
|
配置日志
配置logback文件
创建log4jdbc.log4j2.properties文件
配置如下属性:
1
2# If you use SLF4J. First, you need to tell log4jdbc-log4j2 that you want to use the SLF4J logger
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
配置logback
创建logback-dev.xml
配置如下属性:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<contextName>beetlSqlDemo</contextName>
<property name="LOG_HOME" value="D:/logs/beetSqlDemo/dev"/>
<property name="LOG_NAME" value="beetlSqlDemo"/>
<!--<springProperty scope="context" name="LOG_HOME" source="sys.log.output.path"/>-->
<!--输出到控制台-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%black(%contextName-) %red(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{36}) - %gray(%msg%n)</pattern>
<charset>utf-8</charset>
</encoder>
</appender>
<appender name="FILE_LOG_OUT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/${LOG_NAME}.log</file>
<append>true</append>
<encoder>
<pattern>%contextName- %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %class{36} %L %M - %msg%xEx%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}${file.separator}${LOG_NAME}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>180</maxHistory>
<maxFileSize>100MB</maxFileSize>
</rollingPolicy>
</appender>
<!--普通日志输出到控制台-->
<root level="info">
<appender-ref ref="console" />
</root>
<logger name="com.wabestway" level="INFO">
<appender-ref ref="FILE_LOG_OUT"/>
</logger>
<logger name="com.idasound.isb" level="INFO">
<appender-ref ref="FILE_LOG_OUT"/>
</logger>
<!--监控sql日志输出 -->
<logger name="jdbc.sqlonly" level="INFO" additivity="false">
<appender-ref ref="console" />
<appender-ref ref="FILE_LOG_OUT"/>
</logger>
<logger name="jdbc.resultset" level="ERROR" additivity="false">
<appender-ref ref="console" />
<appender-ref ref="FILE_LOG_OUT"/>
</logger>
<logger name="jdbc.resultsettable" level="OFF" additivity="false">
<appender-ref ref="console" />
</logger>
<logger name="jdbc.connection" level="OFF" additivity="false">
<appender-ref ref="console" />
</logger>
<logger name="jdbc.sqltiming" level="OFF" additivity="false">
<appender-ref ref="console" />
</logger>
<logger name="jdbc.audit" level="OFF" additivity="false">
<appender-ref ref="console" />
</logger>
</configuration>
Demo实现
Model
创建员工信息
1 |
|
创建岗位表
1 |
|
SQL文件
**BeetlSql的SQL文件扫描默认路径为resources/sql,所以我们为了简便开发使用Beetlsql的默认配置。
在resources下创建一个名字为sql文件夹
增加文件,实现相关业务
interEmployeeExtra.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69listCols
===
t.id id, t.name name, t.email email, t.sex sex, t.dept_id deptId, t.org_id orgId, t.status status
, t.created_by createdBy, t.created_time createdTime, t.updated_by updatedBy, t.updated_time updatedTime
queryByCondition
===
select #use("listCols")#
from inter_employee t
where #use("condition")#
queryByCondition$count
===
select count(1) total,count(t.id) empNums
from inter_employee t
where #use("condition")#
condition
===
1 = 1
@if(!isEmpty(id)){
and t.id = #id#
@}
@if(!isEmpty(name)){
and t.name = #name#
@}
@if(!isEmpty(email)){
and t.email = #email#
@}
@if(!isEmpty(sex)){
and t.sex = #sex#
@}
@if(!isEmpty(deptId)){
and t.dept_id = #deptId#
@}
@if(!isEmpty(orgId)){
and t.org_id = #orgId#
@}
@if(!isEmpty(status)){
and t.status = #status#
@}
@if(!isEmpty(createdBy)){
and t.created_by = #createdBy#
@}
@if(!isEmpty(createdTime)){
and t.created_time = #createdTime#
@}
@if(!isEmpty(updatedBy)){
and t.updated_by = #updatedBy#
@}
@if(!isEmpty(updatedTime)){
and t.updated_time = #updatedTime#
@}
@if(!isEmpty(jobDuties)){
and EXISTS (SELECT 1 FROM inter_employee_job_duty jd WHERE jd.emp_id = t.id AND jd.emp_job IN (#join(jobDuties)#))
@}
delInterEmployeeById
===
* 逻辑删除
update inter_employee t set t.status = '0', t.updated_by = #updatedBy#, t.updated_time = now() where t.id = #id#
batchDelInterEmployeeByIds
===
* 批量逻辑删除
update inter_employee t set t.status = '0', t.updated_by = #updatedBy#, t.updated_time = now() where t.id in( #join(ids)#)interEmployeeJobDutyExtra.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44listCols
===
t.id id, t.emp_id empId, t.emp_job empJob, t.job_name jobName
queryByCondition
===
select #use("listCols")#
from inter_employee_job_duty t
where #use("condition")#
queryByCondition$count
===
select count(1)
from inter_employee_job_duty t
where #use("condition")#
condition
===
1 = 1
@if(!isEmpty(id)){
and t.id = #id#
@}
@if(!isEmpty(empId)){
and t.emp_id = #empId#
@}
@if(!isEmpty(empJob)){
and t.emp_job = #empJob#
@}
@if(!isEmpty(jobName)){
and t.job_name = #jobName#
@}
delInterEmployeeJobDutyById
===
* 逻辑删除
update inter_employee_job_duty t set t.status = '0', t.updated_by = #updatedBy#, t.updated_time = now() where t.id = #id#
batchDelInterEmployeeJobDutyByIds
===
* 批量逻辑删除
update inter_employee_job_duty t set t.status = '0', t.updated_by = #updatedBy#, t.updated_time = now() where t.id in( #join(ids)#)
DAO
创建repository文件夹
- InterEmployeeDao
1 |
|
- InterEmployeeJobDutyDao
1 |
|
Service
接口定义
- InterEmployeeService
1 |
|
- InterEmployeeJobDutyService
1 |
|
接口实现
在service下创建impl文件夹进行service的接口实现。
- InterEmployeeServiceImpl
1 |
|
- InterEmployeeJobDutyServiceImpl
1 |
|
ProtoBuffer
创建一个protobuf的文件夹,与resources、java平级
创建protobuf文件夹
创建protobuf文件
- interEmployee.proto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39syntax = "proto3";
package beetsqlDemo;
option java_package = "com.bossyang.beetsqlDemo.proto.api";
option java_outer_classname = "InterEmployeeProto";
message InterEmployeeDto {
string id = 1; //id
string name = 2; //姓名
string email = 3; //邮箱
string sex = 4; //性别 0-男;1-女;2-未知
string deptId = 5; //所属部门id
string orgId = 6; //所属机构id
string status = 7; //状态 0-无效;1-有效
string createdBy = 8; //创建人
string createdTime = 9; //创建时间
string updatedBy = 10; //更新人
string updatedTime = 11; //更新时间
}
message InterEmployeeQueryReq {
int64 page = 1; //页码
int64 size = 2; //每页数量
string id = 3; //id
string name = 4; //姓名
string email = 5; //邮箱
string sex = 6; //性别 0-男;1-女;2-未知
string deptId = 7; //所属部门id
string orgId = 8; //所属机构id
string status = 9; //状态 0-无效;1-有效
string createdBy = 10; //创建人
string createdTime = 11; //创建时间
string updatedBy = 12; //更新人
string updatedTime = 13; //更新时间
repeated string jobDuties = 14;//员工岗位集合
}
message InterEmployeeQueryRes {
int64 total = 1; //总数
int64 size = 2; //当前列表大小
repeated InterEmployeeDto list=3;//列表
int64 empNums = 4;//员工数量
}- interEmployeeJobDuty.proto
1 |
|
业务实现
创建api文件夹进行业务实现以及前端交互
业务Service
业务接口定义
在api下面创建service文件夹,进行业务的接口定义
- InterEmployeeApiService
1 |
|
业务接口实现
在service下面创建impl文件夹,进行业务接口的实现
- InterEmployeeApiServiceImpl
1 |
|
Controller
在api文件夹下面创建controller文件夹
- InterEmployeeController
1 |
|
运行
启动运行application
测试
本案例采用DocwayAPI接口管理平台存储和定义接口,具体接口如下:
- 运行查询接口进行测试
响应结果:
后台运行结果:
BeetlSql&ProtoBuffer实践Demo
https://github.com/yangxiangnanwill/yangxiangnanwill.github.io/2024/01/03/好好码代码吖/JAVA/Beetl&BeetlSql/BeetlSql&ProtoBuffer实践Demo/