├── .gitignore
├── README.md
├── cheese-db-core
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── cheese
│ └── db
│ └── core
│ ├── DevBaseConfiguration.java
│ ├── DevBaseMapperRegistry.java
│ ├── condition
│ ├── AbstractAction.java
│ ├── AbstractTableAction.java
│ ├── Action.java
│ ├── Actions.java
│ ├── CommonSegmentProvider.java
│ ├── load
│ │ └── LoadAction.java
│ ├── manager
│ │ └── DevBaseActionManager.java
│ ├── page
│ │ ├── DevBasePage.java
│ │ ├── IPage.java
│ │ └── PageFactory.java
│ ├── query
│ │ ├── ComparatorKeyValue.java
│ │ ├── LikeKeyValue.java
│ │ └── RangeKeyValue.java
│ └── simple
│ │ ├── delete
│ │ └── DeleteTableAction.java
│ │ ├── insert
│ │ └── InsertTableAction.java
│ │ ├── query
│ │ ├── AbstractQueryAction.java
│ │ └── QueryTableAction.java
│ │ └── update
│ │ └── UpdateTableAction.java
│ ├── enums
│ ├── ActionType.java
│ ├── Comparator.java
│ ├── LikeType.java
│ └── RangeType.java
│ ├── exception
│ ├── DevBaseException.java
│ ├── ParamNotFountException.java
│ ├── ResultWrapperException.java
│ ├── StatementNotFoundException.java
│ └── UnknownActionTypeException.java
│ ├── mapper
│ └── DB.java
│ ├── props
│ ├── DataSourceConfig.java
│ └── MybatisConfig.java
│ ├── proxy
│ ├── DevBaseDBMapperMethod.java
│ ├── DevBaseDBMethodSignature.java
│ ├── DevBaseDBSqlCommand.java
│ ├── DevBaseMapperProxy.java
│ └── DevBaseMapperProxyFactory.java
│ ├── support
│ ├── ConfigurationSupport.java
│ └── DevBaseConstant.java
│ └── wrapper
│ ├── BeanWrapperResult.java
│ ├── MapWrapperResult.java
│ └── WrapperResult.java
├── cheese-db-rpc
└── pom.xml
├── cheese-db-sample
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── cheese
│ │ │ └── db
│ │ │ └── sample
│ │ │ ├── CheeseSampleApplication.java
│ │ │ └── service
│ │ │ ├── ICommonService.java
│ │ │ └── impl
│ │ │ └── CommonServiceImpl.java
│ └── resources
│ │ ├── application.properties
│ │ └── config
│ │ └── db.setting
│ └── test
│ └── java
│ └── com
│ └── cheese
│ └── db
│ └── sample
│ └── test
│ └── CheeseApplicationTest.java
├── cheese-db-spring-boot-starter
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── cheese
│ │ └── db
│ │ ├── autoconfigure
│ │ ├── DevBaseDBAutoConfiguration.java
│ │ ├── DevBaseDbAutoImportSelector.java
│ │ └── EnableDevBase.java
│ │ ├── enums
│ │ └── TransactionEnum.java
│ │ └── props
│ │ └── DevBaseDBProps.java
│ └── resources
│ └── META-INF
│ └── spring-configuration-metadata.json
├── cheese-db-spring
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── cheese
│ └── db
│ └── spring
│ ├── annotation
│ ├── DevBaseMapperRegistrar.java
│ ├── DevBaseMappers.java
│ └── DevBaseMultiDataSourceTransactional.java
│ ├── datasource
│ └── DatasourceContext.java
│ ├── exception
│ └── TransactionException.java
│ ├── injector
│ ├── DevBaseSqlInjector.java
│ ├── DevBaseSqlInjectorProvider.java
│ ├── DevBaseStatementFactory.java
│ ├── collector
│ │ ├── InjectMetaCollector.java
│ │ ├── SysSqlConfigInjectMetaCollector.java
│ │ ├── TableInjectMetaCollector.java
│ │ ├── dialect
│ │ │ ├── DialectType.java
│ │ │ └── MysqlDialectCollector.java
│ │ └── method
│ │ │ ├── AbstractMethod.java
│ │ │ ├── Delete.java
│ │ │ ├── Insert.java
│ │ │ ├── Select.java
│ │ │ ├── SqlMethod.java
│ │ │ └── Update.java
│ ├── event
│ │ ├── DevBaseSqlInjectListener.java
│ │ ├── InjectType.java
│ │ ├── SqlInjectEvent.java
│ │ └── runtime
│ │ │ └── DefaultEventSqlInjector.java
│ ├── metadata
│ │ ├── InjectMeta.java
│ │ ├── TableMeta.java
│ │ └── simple
│ │ │ ├── DefaultInjectMeta.java
│ │ │ └── MysqlTableMeta.java
│ └── simple
│ │ └── DefaultDevBaseSqlInjectorProvider.java
│ ├── mapper
│ ├── DevBaseClassPathMapperScanner.java
│ ├── DevBaseMapperFactoryBean.java
│ └── DevBaseMapperScannerConfigurer.java
│ ├── support
│ ├── DatasourceContextSupport.java
│ ├── DevBaseApplicationContextSupport.java
│ ├── DevBaseMapperRegistrySupport.java
│ ├── DevBaseSqlSessionDaoSupport.java
│ └── DevBaseTableMetaSupport.java
│ ├── transaction
│ ├── aop
│ │ ├── DevBaseAnnotationsTransactionMethodInterceptor.java
│ │ └── DevBaseMultiDatasourceTransactionAdvisor.java
│ └── aspectj
│ │ └── DevBaseMultiDataSourceTransactionalAspect.java
│ ├── utils
│ └── SqlScriptUtils.java
│ └── wrappers
│ ├── DevBaseDataSourceTransactionManagers.java
│ ├── DevBaseDataSources.java
│ ├── DevBaseSqlSessionFactories.java
│ ├── DevBaseSqlSessions.java
│ └── simple
│ ├── DefaultDevBaseDataSourceTransactionManagers.java
│ ├── DefaultDevBaseDataSources.java
│ ├── DefaultDevBaseSqlSessionFactories.java
│ └── DefaultDevBaseSqlSessions.java
└── doc
└── sql
├── cheese-repository-bus.sql
└── cheese-repository-sys.sql
/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 | !**/src/main/**
5 | !**/src/test/**
6 |
7 | ### STS ###
8 | .apt_generated
9 | .classpath
10 | .factorypath
11 | .project
12 | .settings
13 | .springBeans
14 | .sts4-cache
15 |
16 | ### IntelliJ IDEA ###
17 | .idea
18 | *.iws
19 | *.iml
20 | *.ipr
21 |
22 | ### NetBeans ###
23 | /nbproject/private/
24 | /nbbuild/
25 | /dist/
26 | /nbdist/
27 | /.nb-gradle/
28 | build/
29 |
30 | ### VS Code ###
31 | .vscode/
32 |
33 |
34 | ### Mac
35 | .DS_Store
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## cheese-repository -- 以mybatis为核心的多数据源的持久层操作框架
2 |
3 | ### 一、架构组件
4 | #### 单体服务
5 | - cheese-db-core:`完成与mybatis的整合,针对多数据源重新设计代理方法的执行策略;设计条件构建工具`
6 | - cheese-db-rpc: (需要拆分cheese-db-spring功能,提供feign、dubbo, 模仿dubbo写一个netty+zookeeper的rpc方式)
7 | - cheese-db-spring:
8 | - `cheese-db整合spring,完成持久层实例BeanDefinition的定义、初始化以及持久层代理的创建;`
9 | - `多数据源、事务管理器、会话工厂以及会话顶层设计及默认实现;`
10 | - `sql注册机以及相关功能设计;`
11 | - `多数据源事务处理`
12 | - cheese-db-spring-boot-starter:`cheese-db-spring接入springboot,提供可插拔式的组件使用方式`
13 |
14 | #### 微服务(服务提供者和消费者组成,支持多种RPC方式)
15 | - cheese-db-common: `基础common,为服务提供者和服务消费者提供顶层设计,如DevBaseService、DevBaseServiceProvider`
16 | - cheese-db-rpc: `提供参数序列化方式(json、hessian2),支持消费者多种rpc调用方式(feign、dubbo)`
17 | - cheese-db-server-core: `持久层服务提供组件,完成cheese-db持久层数据收集和注册,提供多种服务暴露方式,服务暴露方式要与服务调用方式对应`
18 | - cheese-db-client-core: `持久层服务消费组件,通过rpc组件支持多种服务调用方式,服务调用方式要与服务暴露方式对应`
19 | ### 二、架构图
20 | #### 单体服务架构图
21 |
22 |
23 | #### 微服务架构图
24 |
25 |
26 | ### 三、快速开始
27 | #### 单体服务
28 | 1. 引入maven依赖
29 | ```xml
30 |
31 | com.cheese.db
32 | cheese-db-spring-boot-starter
33 | 1.0.0
34 |
35 | ```
36 | 2. 运行工程下`doc/script/sql`中的`cheese-repository-sys.sql`和`cheese-repository-bus.sql`,目前sql脚本文件只提供mysql
37 | ```markdown
38 | cheese-repository-sys.sql 导入配置数据库
39 | cheese-repository-bus.sql 导入其他数据库(可以多个业务数据库,这里提供一个demo)
40 |
41 | ```
42 |
43 | 3. 搭建springboot工程,并在启动类上添加`@EnableDevBase`注解(请参考cheese-db-sample工程)
44 | ```java
45 | /*
46 | devbase功能可以兼容DataSourceAutoConfiguration的功能,进行相应的配置即可
47 | */
48 | @EnableDevBase
49 | @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
50 | public class CheeseApplication {
51 | public static void main(String[] args) {
52 | SpringApplication.run(CheeseApplication.class,args);
53 | }
54 |
55 | /**
56 | * mysql数据库的配置及数据库加载方式
57 | * 目前仅实现mysql的实现方式,后续会陆续增加pg oracle 等数据库的元数据加载实现
58 | *
59 | * @return
60 | */
61 | @Bean
62 | public MysqlDialectCollector mysqlDialectCollector() {
63 | return new MysqlDialectCollector();
64 | }
65 | }
66 | ```
67 | 3. `application.properties`中配置
68 | ```properties
69 | server.port=8081
70 | logging.level.com.cheese.db.spring=debug
71 | spring.main.allow-bean-definition-overriding=true
72 |
73 | # devbase 配置
74 | ## devbase-db开启开关,默认开启
75 | devbase-db.enabled=true
76 | ## devbase-db使用默认配置,默认为false
77 | devbase-db.use-default-config=true
78 |
79 | # 多数据源配置
80 | devbase-db.configuration.sys.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
81 | devbase-db.configuration.sys.insert-key-property=id
82 | devbase-db.configuration.bus.insert-key-property=id
83 |
84 | ## 配置数据源
85 | devbase-db.config-data-source=sys
86 | ## 加载数据库的元数据,为数据库下的所有表生成基础增删改的方法
87 | ## 元数据加载的key
88 | devbase-db.data-sources.sys.scheme-key=sys
89 | ## 元数据加载数据库的名称,与url中的数据库名一致
90 | devbase-db.data-sources.sys.scheme-name=xxx_sys
91 | devbase-db.data-sources.sys.datasource-type=com.alibaba.druid.pool.DruidDataSource
92 | devbase-db.data-sources.sys.url=jdbc:mysql://localhost:3306/xxx_sys?characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
93 | devbase-db.data-sources.sys.driver-class-name=com.mysql.cj.jdbc.Driver
94 | devbase-db.data-sources.sys.username=root
95 | devbase-db.data-sources.sys.password=root
96 |
97 | ## 其他数据源 可以配置多个
98 | devbase-db.data-sources.bus.scheme-key=bus
99 | devbase-db.data-sources.bus.scheme-name=xxx_bus
100 | devbase-db.data-sources.bus.datasource-type=com.alibaba.druid.pool.DruidDataSource
101 | devbase-db.data-sources.bus.url=jdbc:mysql://localhost:3306/xxx_bus?characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
102 | devbase-db.data-sources.bus.driver-class-name=com.mysql.cj.jdbc.Driver
103 | devbase-db.data-sources.bus.username=root
104 | devbase-db.data-sources.bus.password=root
105 |
106 |
107 | ```
108 |
109 |
110 | #### 微服务(这里提供基于ribbon的微服务调用方式)
111 |
112 | - 参考分支cheese-db-cloud中cheese-db-sample工程
113 | - 与单体服务不同的部分:
114 | - 服务提供者依赖使用
115 | ```xml
116 |
117 | com.cheese.db
118 | cheese-db-server-core
119 | 1.0.0
120 |
121 | ```
122 | - 服务提供者配置:
123 | ```properties
124 | # -------------------eureka--------------
125 | spring.application.name=server
126 | eureka.instance.instance-id=${spring.application.name}:@project.version@-${spring.cloud.client.ip-address}:${server.port}
127 | eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
128 | eureka.instance.lease-renewal-interval-in-seconds=5
129 | eureka.instance.lease-expiration-duration-in-seconds=10
130 | eureka.client.healthcheck.enabled=false
131 | eureka.instance.prefer-ip-address=true
132 | eureka.client.registry-fetch-interval-seconds=5
133 | ribbon.ServerListRefreshInterval=5000
134 | # -------------------cheese-db-server--------------
135 | logging.level.com.cheese.db.server=debug
136 | spring.main.allow-bean-definition-overriding=true
137 |
138 | # devbase 配置
139 | ## devbase-db开启开关,默认开启
140 | devbase-db.server.enabled=true
141 | ## devbase-db使用默认配置,默认为false
142 | devbase-db.server.use-default-config=true
143 | ## 事务配置
144 | devbase-db.server.transaction-type=advisor
145 | ## 服务暴露方式 默认为feign
146 | devbase-db.server.exposer-type=default
147 |
148 | # 多数据源配置
149 | devbase-db.server.configuration.sys.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
150 | devbase-db.server.configuration.sys.insert-key-property=id
151 | devbase-db.server.configuration.bus.insert-key-property=id
152 |
153 | ## 配置数据源
154 | devbase-db.server.config-data-source=sys
155 | ## 加载数据库的元数据,为数据库下的所有表生成基础增删改的方法
156 | ## 元数据加载的key
157 | devbase-db.server.data-sources.sys.scheme-key=sys
158 | ## 元数据加载数据库的名称,与url中的数据库名一致
159 | devbase-db.server.data-sources.sys.scheme-name=db_sys
160 | devbase-db.server.data-sources.sys.datasource-type=com.alibaba.druid.pool.DruidDataSource
161 | devbase-db.server.data-sources.sys.url=jdbc:mysql://localhost:3306/db_sys?characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
162 | devbase-db.server.data-sources.sys.driver-class-name=com.mysql.cj.jdbc.Driver
163 | devbase-db.server.data-sources.sys.username=root
164 | devbase-db.server.data-sources.sys.password=root
165 |
166 | ## 其他数据源 可以配置多个
167 | devbase-db.server.data-sources.bus.scheme-key=bus
168 | devbase-db.server.data-sources.bus.scheme-name=db_bus
169 | devbase-db.server.data-sources.bus.datasource-type=com.alibaba.druid.pool.DruidDataSource
170 | devbase-db.server.data-sources.bus.url=jdbc:mysql://localhost:3306/db_bus?characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8
171 | devbase-db.server.data-sources.bus.driver-class-name=com.mysql.cj.jdbc.Driver
172 | devbase-db.server.data-sources.bus.username=root
173 | devbase-db.server.data-sources.bus.password=root
174 | ```
175 | - 服务消费者依赖使用
176 | ```xml
177 |
178 | com.cheese.db
179 | cheese-db-client-core
180 | 1.0.0
181 |
182 | ```
183 | - 服务消费者配置:
184 | ```properties
185 | # -------------------eureka--------------
186 | spring.application.name=client
187 | eureka.instance.instance-id=${spring.application.name}:@project.version@-${spring.cloud.client.ip-address}:${server.port}
188 | eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
189 | eureka.instance.lease-renewal-interval-in-seconds=5
190 | eureka.instance.lease-expiration-duration-in-seconds=10
191 | eureka.client.healthcheck.enabled=false
192 | eureka.instance.prefer-ip-address=true
193 | eureka.client.registry-fetch-interval-seconds=5
194 | ribbon.ServerListRefreshInterval=5000
195 | # -------------------cheese-db-client--------------
196 | ## rpc调用的服务名称
197 | devbase-db.client.rpc-server=server
198 | ## rpc类型
199 | devbase-db.client.rpc-type=feign
200 | ```
--------------------------------------------------------------------------------
/cheese-db-core/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.cheese.db
8 | cheese-db-core
9 | 1.0.0
10 |
11 |
12 |
13 |
14 | org.slf4j
15 | slf4j-api
16 | 1.7.25
17 | compile
18 |
19 |
20 | org.mybatis
21 | mybatis
22 | 3.5.2
23 | compile
24 |
25 |
26 | com.github.pagehelper
27 | pagehelper
28 | 5.1.4
29 | compile
30 |
31 |
32 |
33 |
34 |
35 |
36 | org.apache.maven.plugins
37 | maven-source-plugin
38 |
39 | true
40 |
41 |
42 |
43 | attach-sources
44 | compile
45 |
46 | jar
47 |
48 |
49 |
50 |
51 |
52 | org.apache.maven.plugins
53 | maven-deploy-plugin
54 | 2.8.1
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/DevBaseConfiguration.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core;
2 |
3 | import com.cheese.db.core.props.MybatisConfig;
4 | import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
5 | import org.apache.ibatis.executor.keygen.KeyGenerator;
6 | import org.apache.ibatis.session.Configuration;
7 |
8 | import java.util.Objects;
9 |
10 | /**
11 | * 继承自mybatis中Configuration类
12 | *
13 | * @author sobann
14 | */
15 | public class DevBaseConfiguration extends Configuration {
16 |
17 | protected final DevBaseMapperRegistry mapperRegistry;
18 | protected Class extends KeyGenerator> keyGenerator;
19 | private String insertKeyProperty;
20 |
21 | private final String dbKey;
22 |
23 | public DevBaseConfiguration(String dbKey) {
24 | this.dbKey = dbKey;
25 | mapperRegistry = new DevBaseMapperRegistry(dbKey, this);
26 | }
27 |
28 | public boolean match(String dbKey) {
29 | return Objects.equals(this.dbKey, dbKey);
30 | }
31 |
32 | public boolean hasMapper(String dbKey, Class> type) {
33 | return mapperRegistry.hasMapper(dbKey, type);
34 | }
35 |
36 | public void setMybatisConfig(MybatisConfig mybatisConfig) {
37 | this.setLogImpl(mybatisConfig.getLogImpl());
38 | this.setMapUnderscoreToCamelCase(mybatisConfig.isMapUnderscoreToCamelCase());
39 | this.setCacheEnabled(mybatisConfig.isCacheEnabled());
40 | this.setUseGeneratedKeys(mybatisConfig.isUseGeneratedKeys());
41 | this.keyGenerator = mybatisConfig.getKeyGenerator() == null ? Jdbc3KeyGenerator.class : mybatisConfig.getKeyGenerator();
42 | this.insertKeyProperty = mybatisConfig.getInsertKeyProperty() == null ? "id" : mybatisConfig.getInsertKeyProperty();
43 | }
44 |
45 | public Class extends KeyGenerator> getKeyGenerator() {
46 | return keyGenerator;
47 | }
48 |
49 | public void setKeyGenerator(Class extends KeyGenerator> keyGenerator) {
50 | this.keyGenerator = keyGenerator;
51 | }
52 |
53 | public String getInsertKeyProperty() {
54 | return insertKeyProperty;
55 | }
56 |
57 | public void setInsertKeyProperty(String insertKeyProperty) {
58 | this.insertKeyProperty = insertKeyProperty;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/DevBaseMapperRegistry.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core;
2 |
3 | import org.apache.ibatis.binding.MapperProxyFactory;
4 | import org.apache.ibatis.binding.MapperRegistry;
5 | import org.apache.ibatis.session.Configuration;
6 |
7 | import java.util.HashMap;
8 | import java.util.Map;
9 |
10 | /**
11 | * 继承自mybatis中MapperRegistry类
12 | *
13 | * 暂未使用
14 | *
15 | * @author sobann
16 | */
17 | public class DevBaseMapperRegistry extends MapperRegistry {
18 |
19 | private final Configuration config;
20 | private final String dbKey;
21 | private final Map, MapperProxyFactory>> knownMappers = new HashMap<>();
22 |
23 | public DevBaseMapperRegistry(String dbKey, Configuration config) {
24 | super(config);
25 | this.dbKey = dbKey;
26 | this.config = config;
27 | }
28 |
29 | public boolean hasMapper(String dbKey, Class type) {
30 | return knownMappers.containsKey(type);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/AbstractAction.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition;
2 |
3 | /**
4 | * 抽象类
5 | * 如果有公用的一些方法以及属性可以抽提到此类中
6 | *
7 | *
8 | * tip:
9 | *
10 | * @author sobann
11 | */
12 | public abstract class AbstractAction implements Action {
13 |
14 | private final String dbKey;
15 | private final String code;
16 |
17 | public AbstractAction(String dbKey, String code) {
18 | this.dbKey = dbKey;
19 | this.code = code;
20 | }
21 |
22 | @Override
23 | public String getDbKey() {
24 | return dbKey;
25 | }
26 |
27 | @Override
28 | public String getCode() {
29 | return code;
30 | }
31 |
32 | public String getSqlSegment() {
33 | return null;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/AbstractTableAction.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition;
2 |
3 | /**
4 | * 关于直接对数据表操作的抽象action
5 | *
6 | * @author sobann
7 | */
8 | public abstract class AbstractTableAction extends AbstractAction{
9 |
10 | private final String tableName;
11 |
12 | public AbstractTableAction(String dbKey, String tableName) {
13 | super(dbKey, null);
14 | this.tableName = tableName;
15 | }
16 |
17 | public String getTableName() {
18 | return tableName;
19 | }
20 |
21 | @Override
22 | public String getCode() {
23 | // dbKey + tableName + ActionType 确认唯一Statement
24 | return this.getDbKey() + TOKEN_SEPARATOR + this.getTableName() + TOKEN_SEPARATOR + this.getActionType().name();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/Action.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition;
2 |
3 |
4 | import com.cheese.db.core.enums.ActionType;
5 | import com.cheese.db.core.support.DevBaseConstant;
6 |
7 | /**
8 | * 条件对象顶层接口
9 | *
10 | * @author sobann
11 | */
12 | public interface Action extends DevBaseConstant {
13 |
14 | /**
15 | * 数据库标识
16 | *
17 | * @return
18 | */
19 | String getDbKey();
20 |
21 | /**
22 | * 可以理解为唯一名称空间
23 | *
24 | * @return
25 | */
26 | String getCode();
27 |
28 | /**
29 | * 操作类型
30 | *
31 | * @return
32 | */
33 | ActionType getActionType();
34 | }
35 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/Actions.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition;
2 |
3 | import com.cheese.db.core.condition.load.LoadAction;
4 |
5 | /**
6 | * 构建Action实例的工厂
7 | *
8 | * @author sobann
9 | */
10 | public class Actions {
11 |
12 | public static LoadAction getLoad(String dbKey,String code){
13 | return new LoadAction(dbKey, code);
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/CommonSegmentProvider.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition;
2 |
3 | import com.cheese.db.core.condition.query.ComparatorKeyValue;
4 | import com.cheese.db.core.condition.query.LikeKeyValue;
5 | import com.cheese.db.core.condition.query.RangeKeyValue;
6 | import com.cheese.db.core.enums.Comparator;
7 | import com.cheese.db.core.enums.LikeType;
8 | import com.cheese.db.core.enums.RangeType;
9 |
10 | import java.util.ArrayList;
11 | import java.util.Arrays;
12 | import java.util.List;
13 | import java.util.stream.Collectors;
14 |
15 | /**
16 | * 对于查询、修改、删除通用的sql片段的包装
17 | *
18 | * @author sobann
19 | */
20 | public class CommonSegmentProvider {
21 |
22 | private static final String NO_SEGMENT = null;
23 |
24 | private final List rangeCdn;
25 | private final List likeCdn;
26 | private final List comparatorCdn;
27 |
28 | public CommonSegmentProvider() {
29 | this.rangeCdn = new ArrayList<>(8);
30 | this.likeCdn = new ArrayList<>(8);
31 | this.comparatorCdn = new ArrayList<>(8);
32 | }
33 |
34 | public void putLikeParam(String field, LikeType likeType, Object value) {
35 | likeCdn.add(new LikeKeyValue(field, value, likeType));
36 | }
37 |
38 | public void putRangeParam(String field, RangeType rangeType, Object... rangeValues) {
39 | rangeCdn.add(new RangeKeyValue(field, rangeType, rangeValues));
40 | }
41 |
42 | public void putComparatorParam(String field, Comparator comparator, Object value) {
43 | comparatorCdn.add(new ComparatorKeyValue(field, value, comparator));
44 | }
45 |
46 | public String supportSqlSegment() {
47 | if (comparatorCdn.isEmpty() && likeCdn.isEmpty() && rangeCdn.isEmpty()) {
48 | return NO_SEGMENT;
49 | }
50 | StringBuilder segmentBuilder = new StringBuilder();
51 | if (!rangeCdn.isEmpty()) {
52 | String inSegment = rangeCdn.stream().map(item -> String.format(" AND %s %s ", item.getKey(), String.format(item.getRangeType().getSegment(), Arrays.stream(item.getValues()).map(String::valueOf).sorted().collect(Collectors.joining(","))))).collect(Collectors.joining(" "));
53 | segmentBuilder.append(inSegment);
54 | }
55 | if (!likeCdn.isEmpty()) {
56 | String likeSegment = likeCdn.stream().map(item -> String.format(" AND %s LIKE '%s'", item.getKey(), String.format(item.getLikeType().getSegment(), item.getValue()))).collect(Collectors.joining(" "));
57 | segmentBuilder.append(likeSegment);
58 | }
59 | if (!comparatorCdn.isEmpty()) {
60 | String comparatorSegment = comparatorCdn.stream().map(item -> String.format(" AND %s %s '%s' ", item.getKey(), item.getRelation().getToken(), item.getValue())).collect(Collectors.joining(" "));
61 | segmentBuilder.append(comparatorSegment);
62 | }
63 | return segmentBuilder.toString();
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/load/LoadAction.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition.load;
2 |
3 | import com.cheese.db.core.condition.AbstractAction;
4 | import com.cheese.db.core.enums.ActionType;
5 |
6 | import java.util.HashMap;
7 | import java.util.Map;
8 |
9 | /**
10 | * load
11 | *
12 | * @author sobann
13 | */
14 | public class LoadAction extends AbstractAction {
15 |
16 | private Long id;
17 |
18 | /**
19 | * 此属性为修改和新增准备
20 | */
21 | private Map data;
22 | /**
23 | * 此属性作为所有sql的条件属性
24 | */
25 | private Map param;
26 |
27 | public LoadAction(String dbKey, String code){
28 | super(dbKey, code);
29 | this.data = new HashMap<>(8);
30 | this.param = new HashMap<>(8);
31 | }
32 |
33 | public void setData(Map data) {
34 | this.data = data;
35 | }
36 |
37 | public void setParam(Map param) {
38 | this.param = param;
39 | }
40 |
41 | public void putData(String field, Object val) {
42 | this.data.put(field, val);
43 | }
44 |
45 | public void putParam(String field, Object condition) {
46 | this.param.put(field, condition);
47 | }
48 |
49 | public Map getData() {
50 | return data;
51 | }
52 |
53 | public Map getParam() {
54 | return param;
55 | }
56 |
57 | public Long getId() {
58 | return id;
59 | }
60 |
61 | public void setId(Long id) {
62 | this.id = id;
63 | }
64 |
65 | @Override
66 | public ActionType getActionType() {
67 | return ActionType.LOAD;
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/manager/DevBaseActionManager.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition.manager;
2 |
3 | import com.cheese.db.core.condition.Action;
4 |
5 | /**
6 | * 默认的Action对象参数管理器
7 | * 通过对DevBaseActionManager的定义,可以完成:
8 | * 1.action的校验
9 | * 2.action增强 ===> 实现通用字段的注入等
10 | *
11 | * @author sobann
12 | */
13 | public interface DevBaseActionManager {
14 |
15 | void manager(Action action);
16 | }
17 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/page/DevBasePage.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition.page;
2 |
3 | import com.cheese.db.core.support.DevBaseConstant;
4 |
5 | import java.util.Collections;
6 | import java.util.List;
7 |
8 | /**
9 | * @author sobann
10 | */
11 | public class DevBasePage implements IPage, DevBaseConstant {
12 |
13 | private List records = Collections.emptyList();
14 | private long current;
15 | private long size;
16 | private long total;
17 |
18 | @Override
19 | public List getRecords() {
20 | return this.records;
21 | }
22 |
23 | @Override
24 | public void setRecords(List records) {
25 | this.records = records;
26 | }
27 |
28 | @Override
29 | public long getTotal() {
30 | return this.total;
31 | }
32 |
33 | @Override
34 | public void setTotal(long total) {
35 | this.total = total;
36 | }
37 |
38 | @Override
39 | public long getCurrent() {
40 | //current默认为0
41 | if (ZERO == this.current){
42 | return DEFAULT_CURRENT;
43 | }
44 | return this.current;
45 | }
46 |
47 | @Override
48 | public void setCurrent(long current) {
49 | this.current = current;
50 | }
51 |
52 | @Override
53 | public long getSize() {
54 | //size默认为10
55 | if (ZERO == this.size){
56 | return DEFAULT_SIZE;
57 | }
58 | return this.size;
59 | }
60 |
61 | @Override
62 | public void setSize(long size) {
63 | this.size = size;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/page/IPage.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition.page;
2 |
3 | import java.io.Serializable;
4 | import java.util.List;
5 |
6 | /**
7 | * @author sobann
8 | */
9 | public interface IPage extends Serializable {
10 |
11 | List getRecords();
12 |
13 | void setRecords(List records);
14 |
15 | long getTotal();
16 |
17 | void setTotal(long total);
18 |
19 | long getSize();
20 |
21 | void setSize(long size);
22 |
23 | long getCurrent();
24 |
25 | void setCurrent(long current);
26 | }
27 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/page/PageFactory.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition.page;
2 |
3 | /**
4 | * 分页构建工厂
5 | *
6 | * @author sobann
7 | */
8 | public class PageFactory {
9 |
10 | /**
11 | * 创建基本的分页对象
12 | *
13 | * @param current
14 | * @param size
15 | * @param
16 | * @return
17 | */
18 | public static IPage getPage(long current, long size) {
19 | DevBasePage devBasePage = new DevBasePage<>();
20 | devBasePage.setCurrent(current);
21 | devBasePage.setSize(size);
22 | return devBasePage;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/query/ComparatorKeyValue.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition.query;
2 |
3 | import com.cheese.db.core.enums.Comparator;
4 |
5 | /**
6 | * 键值关系comparator
7 | *
8 | * @author sobann
9 | */
10 | public final class ComparatorKeyValue {
11 |
12 | private String key;
13 |
14 | private Object value;
15 |
16 | private Comparator relation;
17 |
18 | public ComparatorKeyValue(String key, Object value, Comparator relation) {
19 | this.key = key;
20 | this.value = value;
21 | this.relation = relation;
22 | }
23 |
24 | public String getKey() {
25 | return key;
26 | }
27 |
28 | public void setKey(String key) {
29 | this.key = key;
30 | }
31 |
32 | public Object getValue() {
33 | return value;
34 | }
35 |
36 | public void setValue(Object value) {
37 | this.value = value;
38 | }
39 |
40 | public Comparator getRelation() {
41 | return relation;
42 | }
43 |
44 | public void setRelation(Comparator relation) {
45 | this.relation = relation;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/query/LikeKeyValue.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition.query;
2 |
3 | import com.cheese.db.core.enums.LikeType;
4 |
5 | /**
6 | * 键值关系like
7 | *
8 | * @author sobann
9 | */
10 | public final class LikeKeyValue {
11 |
12 | private String key;
13 |
14 | private Object value;
15 |
16 | private LikeType likeType;
17 |
18 | public LikeKeyValue(String key, Object value, LikeType likeType) {
19 | this.key = key;
20 | this.value = value;
21 | this.likeType = likeType;
22 | }
23 |
24 | public String getKey() {
25 | return key;
26 | }
27 |
28 | public void setKey(String key) {
29 | this.key = key;
30 | }
31 |
32 | public Object getValue() {
33 | return value;
34 | }
35 |
36 | public void setValue(Object value) {
37 | this.value = value;
38 | }
39 |
40 | public LikeType getLikeType() {
41 | return likeType;
42 | }
43 |
44 | public void setLikeType(LikeType likeType) {
45 | this.likeType = likeType;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/query/RangeKeyValue.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition.query;
2 |
3 | import com.cheese.db.core.enums.RangeType;
4 |
5 | /**
6 | * 范围关系range
7 | *
8 | * @author sobann
9 | */
10 | public class RangeKeyValue {
11 |
12 | private String key;
13 |
14 | private Object[] values;
15 |
16 | private RangeType rangeType;
17 |
18 |
19 | public RangeKeyValue(String key, RangeType rangeType, Object... values) {
20 | this.key = key;
21 | this.values = values;
22 | this.rangeType = rangeType;
23 | }
24 |
25 | public String getKey() {
26 | return key;
27 | }
28 |
29 | public void setKey(String key) {
30 | this.key = key;
31 | }
32 |
33 | public Object[] getValues() {
34 | return values;
35 | }
36 |
37 | public void setValues(Object[] values) {
38 | this.values = values;
39 | }
40 |
41 | public RangeType getRangeType() {
42 | return rangeType;
43 | }
44 |
45 | public void setRangeType(RangeType rangeType) {
46 | this.rangeType = rangeType;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/simple/delete/DeleteTableAction.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition.simple.delete;
2 |
3 | import com.cheese.db.core.condition.AbstractTableAction;
4 | import com.cheese.db.core.condition.CommonSegmentProvider;
5 | import com.cheese.db.core.enums.ActionType;
6 | import com.cheese.db.core.enums.Comparator;
7 | import com.cheese.db.core.enums.LikeType;
8 | import com.cheese.db.core.enums.RangeType;
9 |
10 | import java.util.HashMap;
11 | import java.util.Map;
12 |
13 | /**
14 | * 删除操作
15 | *
16 | * @author sobann
17 | */
18 | public class DeleteTableAction extends AbstractTableAction {
19 |
20 | private Map param;
21 | private final CommonSegmentProvider commonSegmentProvider;
22 |
23 | public DeleteTableAction(String dbKey, String tableName) {
24 | super(dbKey, tableName);
25 | this.param = new HashMap<>(8);
26 | this.commonSegmentProvider = new CommonSegmentProvider();
27 | }
28 |
29 | public void putParam(String field, Object value) {
30 | this.param.put(field, value);
31 | }
32 |
33 | public Map getParam() {
34 | return param;
35 | }
36 |
37 | public void setParam(Map cdn) {
38 | this.param = cdn;
39 | }
40 |
41 | public void putLikeParam(String field, LikeType likeType, Object value) {
42 | commonSegmentProvider.putLikeParam(field, likeType, value);
43 | }
44 |
45 | public void putRangeParam(String field, RangeType rangeType, Object... rangeValues) {
46 | commonSegmentProvider.putRangeParam(field, rangeType, rangeValues);
47 | }
48 |
49 | public void putComparatorParam(String field, Comparator comparator, Object value) {
50 | commonSegmentProvider.putComparatorParam(field, comparator, value);
51 | }
52 |
53 | @Override
54 | public String getSqlSegment() {
55 | return commonSegmentProvider.supportSqlSegment();
56 | }
57 |
58 | @Override
59 | public ActionType getActionType() {
60 | return ActionType.DELETE;
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/simple/insert/InsertTableAction.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition.simple.insert;
2 |
3 | import com.cheese.db.core.condition.AbstractTableAction;
4 | import com.cheese.db.core.enums.ActionType;
5 |
6 | import java.util.HashMap;
7 | import java.util.Map;
8 |
9 | /**
10 | * 主键自增 使用Jdbc3KeyGenerator存在的问题
11 | * 1.数据表主键字段必须使用自增策略
12 | * 2.需要使用明确的TypeHandler来映射主键和数据库字段的关系,比如当前类中插入主键使用字段primary,java类型为Long,使用的字段处理器为LongTypeHandler (尝试使用Serializer 需要自定义一个 TypeHandler 否则会让主键无法回填哦)
13 | *
14 | * @author sobann
15 | */
16 | public class InsertTableAction extends AbstractTableAction {
17 |
18 | private Map data;
19 | private Long id;
20 |
21 | public InsertTableAction(String dbKey, String tableName) {
22 | super(dbKey, tableName);
23 | this.data = new HashMap<>(8);
24 | }
25 |
26 |
27 | public Map getData() {
28 | return this.data;
29 | }
30 |
31 | public void putData(String field, Object value) {
32 | this.data.put(field, value);
33 | }
34 |
35 | public void setData(Map data) {
36 | this.data = data;
37 | }
38 |
39 | public Long getId() {
40 | return id;
41 | }
42 |
43 | public void setId(Long id) {
44 | this.id = id;
45 | }
46 |
47 | @Override
48 | public ActionType getActionType() {
49 | return ActionType.INSERT;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/simple/query/AbstractQueryAction.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition.simple.query;
2 |
3 | import com.cheese.db.core.condition.AbstractTableAction;
4 | import com.cheese.db.core.enums.ActionType;
5 | import com.cheese.db.core.support.DevBaseConstant;
6 |
7 | import java.util.HashMap;
8 | import java.util.Map;
9 | import java.util.Objects;
10 |
11 | /**
12 | * 查询功能
13 | *
14 | * @author sobann
15 | */
16 | public abstract class AbstractQueryAction extends AbstractTableAction implements DevBaseConstant {
17 |
18 | private String sqlSelect = SQL_ALL;
19 | private Map param;
20 | /**
21 | * 条件传入实体实体,实体的参数最终都需要转入param中
22 | * 现阶段select语句script中仅使用#{ew.param.*}作为条件
23 | */
24 | private T entity;
25 |
26 | public AbstractQueryAction(String dbKey, String tableName) {
27 | super(dbKey, tableName);
28 | this.param = new HashMap<>(8);
29 | }
30 |
31 | public void setSqlSelect(String sqlSelect) {
32 | this.sqlSelect = Objects.nonNull(sqlSelect) && !BLANK_STR.equals(sqlSelect) ? sqlSelect : SQL_ALL;
33 | }
34 |
35 | public String getSqlSelect() {
36 | return sqlSelect;
37 | }
38 |
39 | public void putParam(String field, Object value) {
40 | this.param.put(field, value);
41 | }
42 |
43 | public void setParam(Map param) {
44 | this.param = param;
45 | }
46 |
47 | protected Map getParam() {
48 | return this.param;
49 | }
50 |
51 | public T getEntity() {
52 | return entity;
53 | }
54 |
55 | public void setEntity(T entity) {
56 |
57 | this.entity = entity;
58 | }
59 |
60 | @Override
61 | public ActionType getActionType() {
62 | return ActionType.SELECT;
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/cheese-db-core/src/main/java/com/cheese/db/core/condition/simple/query/QueryTableAction.java:
--------------------------------------------------------------------------------
1 | package com.cheese.db.core.condition.simple.query;
2 |
3 | import com.cheese.db.core.condition.CommonSegmentProvider;
4 | import com.cheese.db.core.enums.ActionType;
5 | import com.cheese.db.core.enums.Comparator;
6 | import com.cheese.db.core.enums.LikeType;
7 | import com.cheese.db.core.enums.RangeType;
8 |
9 | import java.util.Map;
10 |
11 | /**
12 | * 查询操作 使用mybatis的MapWrapper处理参数和返回
13 | *
14 | * @author sobann
15 | */
16 | public class QueryTableAction extends AbstractQueryAction