├── mybatis-sqlhelper-spring-boot
├── images
│ └── sqlhelperTip.png
├── mybatis-sqlhelper-spring-boot-autoconfigure
│ ├── src
│ │ ├── main
│ │ │ ├── resources
│ │ │ │ └── META-INF
│ │ │ │ │ └── spring.factories
│ │ │ └── java
│ │ │ │ └── io
│ │ │ │ └── github
│ │ │ │ └── heykb
│ │ │ │ └── sqlhelper
│ │ │ │ └── autoconfigure
│ │ │ │ ├── SqlHelperMultiTenantProperties.java
│ │ │ │ ├── NoConflictAutoConfiguration.java
│ │ │ │ ├── SqlHelperLogicDeleteProperties.java
│ │ │ │ ├── HasConflictAutoConfiguration.java
│ │ │ │ ├── SqlHelperAutoConfiguration.java
│ │ │ │ └── SqlHelperProperties.java
│ │ └── test
│ │ │ ├── resources
│ │ │ └── application.yml
│ │ │ └── java
│ │ │ └── io
│ │ │ └── github
│ │ │ └── heykb
│ │ │ └── sqlhelper
│ │ │ └── primary
│ │ │ └── AutoConfigurationTests.java
│ └── pom.xml
├── mybatis-sqlhelper-spring-boot-samples
│ ├── mybatis-spring-boot-dynamic-datasource
│ │ ├── src
│ │ │ └── main
│ │ │ │ ├── resources
│ │ │ │ ├── application.properties
│ │ │ │ ├── data.sql
│ │ │ │ ├── application.yml
│ │ │ │ ├── schema.sql
│ │ │ │ ├── mybatis-config.xml
│ │ │ │ └── sample
│ │ │ │ │ └── mybatis
│ │ │ │ │ └── xml
│ │ │ │ │ └── mapper
│ │ │ │ │ ├── CityMapper.xml
│ │ │ │ │ └── HotelMapper.xml
│ │ │ │ └── java
│ │ │ │ └── sample
│ │ │ │ └── mybatis
│ │ │ │ └── xml
│ │ │ │ ├── service
│ │ │ │ ├── TestService.java
│ │ │ │ ├── AnotherTestService.java
│ │ │ │ └── impl
│ │ │ │ │ ├── AnotherTestServiceImpl.java
│ │ │ │ │ └── TestServiceImpl.java
│ │ │ │ ├── config
│ │ │ │ └── SqlHelperManagerAutoConfiguration.java
│ │ │ │ ├── sqlhelper
│ │ │ │ └── SimpleTenantInfoHandler.java
│ │ │ │ ├── mapper
│ │ │ │ ├── HotelMapper.java
│ │ │ │ └── CityMapper.java
│ │ │ │ ├── domain
│ │ │ │ ├── Hotel.java
│ │ │ │ └── City.java
│ │ │ │ └── Application.java
│ │ ├── license.txt
│ │ ├── format.xml
│ │ └── pom.xml
│ ├── mybatis-spring-boot-sample-xml
│ │ ├── src
│ │ │ └── main
│ │ │ │ ├── java
│ │ │ │ └── sample
│ │ │ │ │ └── mybatis
│ │ │ │ │ └── xml
│ │ │ │ │ ├── service
│ │ │ │ │ ├── TestService.java
│ │ │ │ │ └── impl
│ │ │ │ │ │ └── TestServiceImpl.java
│ │ │ │ │ ├── sqlhelper
│ │ │ │ │ └── SimpleTenantInfoHandler.java
│ │ │ │ │ ├── mapper
│ │ │ │ │ ├── HotelMapper.java
│ │ │ │ │ └── CityMapper.java
│ │ │ │ │ ├── SampleXmlApplication.java
│ │ │ │ │ └── domain
│ │ │ │ │ ├── Hotel.java
│ │ │ │ │ └── City.java
│ │ │ │ └── resources
│ │ │ │ ├── data.sql
│ │ │ │ ├── application.properties
│ │ │ │ ├── schema.sql
│ │ │ │ ├── mybatis-config.xml
│ │ │ │ └── sample
│ │ │ │ └── mybatis
│ │ │ │ └── xml
│ │ │ │ └── mapper
│ │ │ │ ├── CityMapper.xml
│ │ │ │ └── HotelMapper.xml
│ │ ├── license.txt
│ │ ├── format.xml
│ │ └── pom.xml
│ └── pom.xml
├── mybatis-sqlhelper-spring-boot-starter
│ └── pom.xml
└── pom.xml
├── mybatis-sqlhelper-spring
├── src
│ ├── test
│ │ ├── java
│ │ │ └── io
│ │ │ │ └── github
│ │ │ │ └── heykb
│ │ │ │ └── sqlhelper
│ │ │ │ └── spring
│ │ │ │ └── test
│ │ │ │ ├── db
│ │ │ │ ├── logicDeleteTest.sql
│ │ │ │ └── createDb.sql
│ │ │ │ ├── dao
│ │ │ │ ├── EmployeeMapper.xml
│ │ │ │ ├── EmployeeMapper.java
│ │ │ │ ├── PeopleMapper.java
│ │ │ │ └── PeopleMapper.xml
│ │ │ │ ├── domain
│ │ │ │ ├── People.java
│ │ │ │ └── Employee.java
│ │ │ │ ├── handlers
│ │ │ │ ├── SimpleColumnFilterHandler.java
│ │ │ │ └── SimpleTenantInfoHandler.java
│ │ │ │ ├── BaseDataUtils.java
│ │ │ │ ├── applicationContext.xml
│ │ │ │ └── PluginTests.java
│ │ └── resources
│ │ │ └── log4j.properties
│ └── main
│ │ └── java
│ │ └── io
│ │ └── github
│ │ └── heykb
│ │ └── sqlhelper
│ │ └── spring
│ │ ├── dynamicds
│ │ ├── SqlHelperDynamicDataSourceProxy.java
│ │ └── SpringSqlHelperDsManager.java
│ │ ├── SqlHelperPluginFactoryBean.java
│ │ └── PropertyLogicDeleteInfoHandler.java
├── README.md
└── pom.xml
├── mybatis-sqlhelper
├── src
│ ├── test
│ │ └── java
│ │ │ ├── log4j.properties
│ │ │ └── io
│ │ │ └── github
│ │ │ └── heykb
│ │ │ └── sqlhelper
│ │ │ └── test
│ │ │ ├── SqlTest.java
│ │ │ ├── baseTest.sql
│ │ │ ├── DynamicDatasourceTest.java
│ │ │ ├── CompareSqlTest.java
│ │ │ └── BaseUtils.java
│ └── main
│ │ └── java
│ │ └── io
│ │ └── github
│ │ └── heykb
│ │ └── sqlhelper
│ │ ├── handler
│ │ ├── dynamic
│ │ │ ├── DynamicFindColumnFilterHandler.java
│ │ │ ├── DynamicFindInjectInfoHandler.java
│ │ │ ├── AbstractDynamicFindColumnFilterHandler.java
│ │ │ └── AbstractDynamicFindInjectInfoHandler.java
│ │ ├── ColumnFilterInfoHandler.java
│ │ ├── abstractor
│ │ │ ├── LogicDeleteInfoHandler.java
│ │ │ ├── TenantInfoHandler.java
│ │ │ └── BinaryConditionInjectInfoHandler.java
│ │ └── InjectColumnInfoHandler.java
│ │ ├── config
│ │ ├── SqlHelperException.java
│ │ └── SqlHelperAutoDbType.java
│ │ ├── dynamicdatasource
│ │ ├── ConnectionSubspaceTypeEnum.java
│ │ ├── SqlHelperDsManager.java
│ │ ├── PrimaryDatasource.java
│ │ ├── SqlHelperDsContextHolder.java
│ │ ├── SimpleProxyDatasource.java
│ │ ├── SqlHelperDynamicDataSourceProxy.java
│ │ ├── SupportedConnectionSubspaceChange.java
│ │ ├── LogicDsMeta.java
│ │ └── DefaultSqlHelperDsManager.java
│ │ ├── helper
│ │ └── MySchemaStatVisitor.java
│ │ ├── utils
│ │ ├── Asserts.java
│ │ └── CommonUtils.java
│ │ ├── interceptor
│ │ ├── SimpleProxyCursor.java
│ │ └── ColumnFilterCursor.java
│ │ └── typeHandler
│ │ └── ColumnFilterTypeHandler.java
└── pom.xml
├── .gitignore
├── LICENSE
├── README_SPRING_BOOT.md
├── DYNAMIC_DATASOURCE_README.md
├── Mybatis-README.md
├── README_DATA_PERMISSION.md
├── pom.xml
└── README.md
/mybatis-sqlhelper-spring-boot/images/sqlhelperTip.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/heykb/mybatis-sqlhelper/HEAD/mybatis-sqlhelper-spring-boot/images/sqlhelperTip.png
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories:
--------------------------------------------------------------------------------
1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=io.github.heykb.sqlhelper.autoconfigure.SqlHelperAutoConfiguration
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | mybatis.config-location=classpath:mybatis-config.xml
2 | logging.level.root=WARN
3 | logging.level.sample.mybatis.xml.mapper=TRACE
4 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring/src/test/java/io/github/heykb/sqlhelper/spring/test/db/logicDeleteTest.sql:
--------------------------------------------------------------------------------
1 | create table employees (
2 | id integer not null,
3 | is_deleted char(1) default 'N' not null
4 | );
5 | insert into employees(id) VALUES (1);
6 | insert into employees(id) VALUES (2);
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-autoconfigure/src/test/resources/application.yml:
--------------------------------------------------------------------------------
1 | sqlhelper:
2 | logic-delete:
3 | column-name: is_deleted
4 | sql-demo: update xx set is_deleted = 'Y'
5 | not-deleted-value: 'N'
6 | enable: true
7 | properties:
8 | ss: xx
9 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/src/main/java/sample/mybatis/xml/service/TestService.java:
--------------------------------------------------------------------------------
1 | package sample.mybatis.xml.service;
2 |
3 | import sample.mybatis.xml.domain.City;
4 |
5 | public interface TestService {
6 | City findByState(String state);
7 | }
8 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/src/main/resources/data.sql:
--------------------------------------------------------------------------------
1 |
2 | insert into city (name, state, country) values ('San Francisco', 'CA', 'US');
3 | insert into hotel(city, name, address, zip) values (1, 'Conrad Treasury Place', 'William & George Streets', '4001')
4 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/java/sample/mybatis/xml/service/TestService.java:
--------------------------------------------------------------------------------
1 | package sample.mybatis.xml.service;
2 |
3 | import sample.mybatis.xml.domain.City;
4 |
5 | public interface TestService {
6 | City findByState(String state);
7 | }
8 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | mybatis.config-location=classpath:mybatis-config.xml
2 | logging.level.root=WARN
3 | logging.level.sample.mybatis.xml.mapper=TRACE
4 | sqlhelper.enable=true
5 | sqlhelper.multi-tenant.enable=true
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/resources/data.sql:
--------------------------------------------------------------------------------
1 |
2 | insert into city (name, state, country) values ('San Francisco', 'CA', 'US');
3 | insert into hotel(city, name, address, zip) values (1, 'Conrad Treasury Place', 'William & George Streets', '4001')
4 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/test/java/log4j.properties:
--------------------------------------------------------------------------------
1 |
2 | log4j.rootLogger=DEBUG,Console
3 | log4j.appender.Console=org.apache.log4j.ConsoleAppender
4 | log4j.appender.Console.Target=System.out
5 | log4j.appender.Console.layout=org.apache.log4j.PatternLayout
6 | log4j.appender.Console.layout.ConversionPattern=[%d{yy/MM/dd HH:mm:ss:SSS}]-%l:%m%n
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring/src/test/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | log4j.rootLogger=DEBUG,Console
2 | log4j.appender.Console=org.apache.log4j.ConsoleAppender
3 | log4j.appender.Console.Target=System.out
4 | log4j.appender.Console.layout=org.apache.log4j.PatternLayout
5 | log4j.appender.Console.layout.ConversionPattern=[%d{yy/MM/dd HH:mm:ss:SSS}]-%l:%m%n
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/java/sample/mybatis/xml/service/AnotherTestService.java:
--------------------------------------------------------------------------------
1 | package sample.mybatis.xml.service;
2 |
3 | import sample.mybatis.xml.domain.City;
4 |
5 | public interface AnotherTestService {
6 | City findByState(String state);
7 | }
8 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring/src/test/java/io/github/heykb/sqlhelper/spring/test/dao/EmployeeMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | sqlhelper:
2 | logic-delete:
3 | enable: true
4 | column-name: is_deleted
5 | not-deleted-value: "'N'"
6 | deleteSqlDemo: update xx set is_deleted = 'Y'
7 | ignoreMapperIds: '**.noPlugin*'
8 | ignoreTables: xx,xxx
9 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/src/main/resources/schema.sql:
--------------------------------------------------------------------------------
1 |
2 | drop table if exists city;
3 | drop table if exists hotel;
4 |
5 | create table city (id int primary key auto_increment, name varchar, state varchar, country varchar,tenant_id varchar);
6 | create table hotel (city int, name varchar, address varchar, zip varchar,is_deleted char(1));
7 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-autoconfigure/src/main/java/io/github/heykb/sqlhelper/autoconfigure/SqlHelperMultiTenantProperties.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.autoconfigure;
2 |
3 | import lombok.Data;
4 |
5 | @Data
6 | public class SqlHelperMultiTenantProperties {
7 | /**
8 | * Multi-tenant feature switch
9 | */
10 | private boolean enable = true;
11 | }
12 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/resources/schema.sql:
--------------------------------------------------------------------------------
1 |
2 | drop table if exists city;
3 | drop table if exists hotel;
4 |
5 | create table city (id int primary key auto_increment, name varchar, state varchar, country varchar,tenant_id varchar);
6 | create table hotel (city int, name varchar, address varchar, zip varchar,is_deleted char(1));
7 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/test/java/io/github/heykb/sqlhelper/test/SqlTest.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.test;
2 |
3 | import com.alibaba.druid.DbType;
4 | import io.github.heykb.sqlhelper.handler.InjectColumnInfoHandler;
5 |
6 | public interface SqlTest {
7 | String name();
8 | DbType db();
9 | String origin();
10 | String target();
11 | InjectColumnInfoHandler injectColumnInfoHandler();
12 | }
13 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/handler/dynamic/DynamicFindColumnFilterHandler.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.handler.dynamic;
2 |
3 | import io.github.heykb.sqlhelper.handler.ColumnFilterInfoHandler;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * @author heykb
9 | */
10 | public interface DynamicFindColumnFilterHandler {
11 | List findColumnFilterHandlers();
12 | default boolean checkMapperIds(String mapperId){
13 | return true;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/handler/dynamic/DynamicFindInjectInfoHandler.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.handler.dynamic;
2 |
3 | import io.github.heykb.sqlhelper.handler.InjectColumnInfoHandler;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * @author heykb
9 | */
10 | public interface DynamicFindInjectInfoHandler {
11 | List findInjectInfoHandlers();
12 |
13 | default boolean checkMapperIds(String mapperId){
14 | return true;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/config/SqlHelperException.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.config;
2 |
3 | /**
4 | * @author heykb
5 | */
6 | public class SqlHelperException extends RuntimeException {
7 | public SqlHelperException(Throwable cause) {
8 | super(cause);
9 | }
10 |
11 | public SqlHelperException(String message, Throwable cause) {
12 | super(message, cause);
13 | }
14 |
15 |
16 | public SqlHelperException(String message){
17 | super(message);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/dynamicdatasource/ConnectionSubspaceTypeEnum.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.dynamicdatasource;
2 |
3 |
4 | /**
5 | * The enum Connection subspace type.
6 | */
7 | public enum ConnectionSubspaceTypeEnum {
8 | /**
9 | * Not support connection subspace type.
10 | */
11 | NOT_SUPPORT,
12 | /**
13 | * Database connection subspace type.
14 | */
15 | DATABASE,
16 | /**
17 | * Schema connection subspace type.
18 | */
19 | SCHEMA;
20 |
21 | }
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/src/main/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-autoconfigure/src/main/java/io/github/heykb/sqlhelper/autoconfigure/NoConflictAutoConfiguration.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.autoconfigure;
2 |
3 | import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
4 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
5 |
6 | /**
7 | * @author heykb
8 | */
9 | @Deprecated
10 | @ConditionalOnMissingClass("com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration")
11 | @ImportAutoConfiguration(SqlHelperAutoConfiguration.class)
12 | public class NoConflictAutoConfiguration {
13 |
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/java/sample/mybatis/xml/config/SqlHelperManagerAutoConfiguration.java:
--------------------------------------------------------------------------------
1 | package sample.mybatis.xml.config;
2 |
3 | import io.github.heykb.sqlhelper.spring.dynamicds.SpringSqlHelperDsManager;
4 | import org.springframework.context.annotation.Bean;
5 | import org.springframework.context.annotation.Configuration;
6 |
7 | @Configuration
8 | public class SqlHelperManagerAutoConfiguration {
9 |
10 | @Bean
11 | public SpringSqlHelperDsManager springSqlHelperDsManager(){
12 | return new SpringSqlHelperDsManager();
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Created by .ignore support plugin (hsz.mobi)
2 | ### Java template
3 | # Compiled class file
4 | *.class
5 |
6 | # Log file
7 | *.log
8 |
9 | # BlueJ files
10 | *.ctxt
11 |
12 | # Mobile Tools for Java (J2ME)
13 | .mtj.tmp/
14 |
15 | # Package Files #
16 | *.jar
17 | *.war
18 | *.nar
19 | *.ear
20 | *.zip
21 | *.tar.gz
22 | *.rar
23 |
24 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
25 | hs_err_pid*
26 |
27 | ### Example user template template
28 | ### Example user template
29 |
30 | # IntelliJ project files
31 | .idea
32 | *.iml
33 | out
34 | gen
35 | target/
36 | sqlHelpDemo/
37 | .flattened-pom.xml
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring/src/test/java/io/github/heykb/sqlhelper/spring/test/domain/People.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.spring.test.domain;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Builder;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 |
8 | /**
9 | *
10 | * @author kb
11 | */
12 | @Data
13 | @Builder
14 | @AllArgsConstructor
15 | @NoArgsConstructor
16 | public class People {
17 | private String name;
18 | private Integer age;
19 | private String email;
20 | private String deptId;
21 | private String id;
22 | private String createdBy;
23 | private String updatedBy;
24 | private String tenantId;
25 | }
26 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-autoconfigure/src/main/java/io/github/heykb/sqlhelper/autoconfigure/SqlHelperLogicDeleteProperties.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.autoconfigure;
2 |
3 | import io.github.heykb.sqlhelper.spring.PropertyLogicDeleteInfoHandler;
4 |
5 |
6 | public class SqlHelperLogicDeleteProperties extends PropertyLogicDeleteInfoHandler {
7 | /**
8 | * Physical delete to logical delete switch
9 | */
10 | private boolean enable = false;
11 |
12 | public boolean isEnable() {
13 | return enable;
14 | }
15 |
16 | public void setEnable(boolean enable) {
17 | this.enable = enable;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring/src/test/java/io/github/heykb/sqlhelper/spring/test/dao/EmployeeMapper.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.spring.test.dao;
2 |
3 |
4 | import org.apache.ibatis.annotations.Delete;
5 | import org.apache.ibatis.annotations.Mapper;
6 | import org.apache.ibatis.annotations.Param;
7 | import org.apache.ibatis.annotations.Select;
8 |
9 | @Mapper
10 | public interface EmployeeMapper {
11 |
12 | @Select("select count(*) from employees")
13 | int count();
14 |
15 | @Select("select count(*) from employees")
16 | int noPluginCount();
17 |
18 | @Delete("delete from employees where id = #{id}")
19 | int deleteById(@Param("id")Integer id);
20 |
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/license.txt:
--------------------------------------------------------------------------------
1 | Copyright ${license.git.copyrightYears} the original author or authors.
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software
10 | distributed under the License is distributed on an "AS IS" BASIS,
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | See the License for the specific language governing permissions and
13 | limitations under the License.
14 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/license.txt:
--------------------------------------------------------------------------------
1 | Copyright ${license.git.copyrightYears} the original author or authors.
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software
10 | distributed under the License is distributed on an "AS IS" BASIS,
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | See the License for the specific language governing permissions and
13 | limitations under the License.
14 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring/src/test/java/io/github/heykb/sqlhelper/spring/test/handlers/SimpleColumnFilterHandler.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.spring.test.handlers;
2 |
3 | import io.github.heykb.sqlhelper.handler.ColumnFilterInfoHandler;
4 | import org.apache.commons.compress.utils.Sets;
5 |
6 | import java.util.Set;
7 |
8 | /**
9 | * @author heykb
10 | */
11 | public class SimpleColumnFilterHandler implements ColumnFilterInfoHandler {
12 |
13 | @Override
14 | public Set getFilterColumns() {
15 | return Sets.newHashSet("name");
16 | }
17 |
18 | @Override
19 | public boolean checkMapperId(String mapperId) {
20 | return true;
21 | }
22 |
23 | @Override
24 | public boolean checkTableName(String tableName) {
25 | return true;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/test/java/io/github/heykb/sqlhelper/test/baseTest.sql:
--------------------------------------------------------------------------------
1 | DROP TABLE IF EXISTS people;
2 | CREATE TABLE people (
3 | name varchar(255) ,
4 | age int,
5 | email varchar(255) ,
6 | id varchar(255) NOT NULL,
7 | tenant_id varchar(255),
8 | dept_id varchar(255),
9 | created_time timestamp(6),
10 | created_by varchar(255) ,
11 | updated_time timestamp(6),
12 | updated_by varchar(255),
13 | is_deleted char(1) default 'N' not null,
14 | CONSTRAINT people_pkey PRIMARY KEY (id)
15 | )
16 | ;
17 |
18 |
19 | INSERT INTO people VALUES ('tony', 10, 'tony@qq.com', '1', 'tenant_1', 'dept1', '2021-11-12 12:18:14.235029', 'admin', NULL, 'admin','N');
20 | INSERT INTO people VALUES ('tom', 20, 'tom@qq.com', '2', 'tenant_2', 'dept2', '2021-11-12 12:18:14.235029', 'admin', NULL, 'admin','N');
21 |
22 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/helper/MySchemaStatVisitor.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.helper;
2 |
3 | import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
4 | import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
5 |
6 | import java.util.HashMap;
7 | import java.util.Map;
8 |
9 | public class MySchemaStatVisitor extends SchemaStatVisitor {
10 | private Map aliasTableMap = new HashMap<>();
11 | @Override
12 | public void endVisit(SQLExprTableSource x) {
13 | super.endVisit(x);
14 | String alias = x.computeAlias();
15 | if(alias!=null){
16 | aliasTableMap.put(alias,x.getTableName());
17 | }
18 |
19 | }
20 | public Map getAliasTableMap() {
21 | return aliasTableMap;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/src/main/java/sample/mybatis/xml/service/impl/TestServiceImpl.java:
--------------------------------------------------------------------------------
1 | package sample.mybatis.xml.service.impl;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.stereotype.Service;
5 | import org.springframework.transaction.annotation.Transactional;
6 | import sample.mybatis.xml.domain.City;
7 | import sample.mybatis.xml.mapper.CityMapper;
8 | import sample.mybatis.xml.service.TestService;
9 |
10 | @Service
11 | public class TestServiceImpl implements TestService {
12 |
13 | @Autowired
14 | private CityMapper cityMapper;
15 |
16 | @Override
17 | @Transactional
18 | public City findByState(String state) {
19 | return cityMapper.findByState(state);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-autoconfigure/src/main/java/io/github/heykb/sqlhelper/autoconfigure/HasConflictAutoConfiguration.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.autoconfigure;
2 |
3 |
4 | import org.springframework.boot.autoconfigure.AutoConfigureBefore;
5 | import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
6 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
7 | import org.springframework.context.annotation.Configuration;
8 |
9 | /**
10 | * @author heykb
11 | */
12 | @Deprecated
13 | @Configuration
14 | @ConditionalOnClass(name="com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration")
15 | @AutoConfigureBefore(name = "com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration")
16 | @ImportAutoConfiguration(SqlHelperAutoConfiguration.class)
17 | public class HasConflictAutoConfiguration {
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/format.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/format.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/handler/ColumnFilterInfoHandler.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.handler;
2 |
3 | import org.apache.ibatis.mapping.SqlCommandType;
4 |
5 | import java.util.Set;
6 |
7 | /**
8 | * The interface Column filter info handler.
9 | *
10 | * @author heykb
11 | */
12 | public interface ColumnFilterInfoHandler {
13 | /**
14 | * Filter columns set.
15 | *
16 | * @return the set
17 | */
18 | Set getFilterColumns();
19 |
20 |
21 | /**
22 | * 设置mapperId方法级别过滤逻辑
23 | *
24 | * @param mapperId the mapper id
25 | * @return boolean boolean
26 | */
27 | default boolean checkMapperId(String mapperId) {
28 | return true;
29 | }
30 |
31 | boolean checkTableName(String tableName);
32 |
33 | default boolean checkCommandType(SqlCommandType commandType){
34 | return SqlCommandType.SELECT == commandType || SqlCommandType.UPDATE == commandType;
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/java/sample/mybatis/xml/service/impl/AnotherTestServiceImpl.java:
--------------------------------------------------------------------------------
1 | package sample.mybatis.xml.service.impl;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.stereotype.Service;
5 | import org.springframework.transaction.annotation.Propagation;
6 | import org.springframework.transaction.annotation.Transactional;
7 | import sample.mybatis.xml.domain.City;
8 | import sample.mybatis.xml.mapper.CityMapper;
9 | import sample.mybatis.xml.service.AnotherTestService;
10 |
11 | @Service
12 | public class AnotherTestServiceImpl implements AnotherTestService {
13 |
14 | @Autowired
15 | private CityMapper cityMapper;
16 |
17 | @Override
18 | @Transactional(propagation = Propagation.SUPPORTS)
19 | public City findByState(String state) {
20 | City city = cityMapper.findByState2(state);
21 | return city;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/dynamicdatasource/SqlHelperDsManager.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.dynamicdatasource;
2 |
3 |
4 | import io.github.heykb.sqlhelper.config.SqlHelperException;
5 |
6 | import javax.sql.DataSource;
7 | import java.util.List;
8 |
9 | public interface SqlHelperDsManager {
10 |
11 | void put(String logicName, LogicDsMeta dsMeta);
12 |
13 | /**
14 | * 移除某个逻辑数据源
15 | * @param logicName
16 | * @throws SqlHelperException 当logicName不存在
17 | * @return 当对应的数据源id没有被其他逻辑数据源引用时,从管理中删除的Datasource对象。否则返回null
18 | */
19 | DataSource remove(String logicName);
20 |
21 | boolean contains(String logicName);
22 |
23 | boolean containsId(String dsId);
24 |
25 | DataSource getByName(String logicName);
26 |
27 | DataSource getById(String dsId);
28 |
29 |
30 | LogicDsMeta getLogicDsMeta(String switchedDsName);
31 |
32 |
33 | List all();
34 |
35 | List allDatasourceIds();
36 | }
37 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring/src/main/java/io/github/heykb/sqlhelper/spring/dynamicds/SqlHelperDynamicDataSourceProxy.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.spring.dynamicds;
2 |
3 | import io.github.heykb.sqlhelper.dynamicdatasource.SqlHelperDsManager;
4 | import org.springframework.core.InfrastructureProxy;
5 |
6 | import javax.sql.DataSource;
7 | import java.util.function.Function;
8 |
9 | class SqlHelperDynamicDataSourceProxy extends io.github.heykb.sqlhelper.dynamicdatasource.SqlHelperDynamicDataSourceProxy implements InfrastructureProxy {
10 |
11 |
12 | public SqlHelperDynamicDataSourceProxy(SqlHelperDsManager sqlHelperDsManager) {
13 | super(sqlHelperDsManager);
14 | }
15 |
16 | /**
17 | * 适应spring事务,做到数据源切换后再开启的事务为一个新的事务,保证正常切换。
18 | * @See org.springframework.transaction.support.TransactionSynchronizationUtils#unwrapResourceIfNecessary()
19 | * @return
20 | */
21 | @Override
22 | public Object getWrappedObject() {
23 | return getDatasource();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring/src/test/java/io/github/heykb/sqlhelper/spring/test/handlers/SimpleTenantInfoHandler.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.spring.test.handlers;
2 |
3 |
4 | import io.github.heykb.sqlhelper.handler.abstractor.TenantInfoHandler;
5 |
6 | /**
7 | * @author heykb
8 | */
9 | public class SimpleTenantInfoHandler extends TenantInfoHandler {
10 | public static final String TENANT_ID = "1";
11 | @Override
12 | public String getTenantIdColumn() {
13 | return "tenant_id";
14 | }
15 | @Override
16 | public String getTenantId() {
17 | // 可以从
18 | // SecurityContextHolder.getContext().getAuthentication()
19 | return "'"+TENANT_ID+"'";
20 | }
21 | @Override
22 | public boolean checkTableName(String tableName) {
23 | return "people".equals(tableName)||"department".equals(tableName);
24 | }
25 |
26 | @Override
27 | public boolean checkMapperId(String mapperId) {
28 | return !mapperId.contains("noPluginSelect");
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/handler/dynamic/AbstractDynamicFindColumnFilterHandler.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.handler.dynamic;
2 |
3 | import io.github.heykb.sqlhelper.handler.ColumnFilterInfoHandler;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * The type Default dynamic find column filter handler.
9 | *
10 | * @param 数据权限信息
11 | * @author heykb
12 | */
13 | public abstract class AbstractDynamicFindColumnFilterHandler implements DynamicFindColumnFilterHandler{
14 | /**
15 | * 编写将获取到的数据权限信息解析成handlers逻辑
16 | *
17 | * @param o the o
18 | * @return the list
19 | */
20 | abstract public List parse(T o);
21 |
22 | /**
23 | * 编写从上下文获取用户的数据权限信息逻辑
24 | *
25 | * @return the permission info
26 | */
27 | abstract public T getPermissionInfo();
28 |
29 | @Override
30 | public List findColumnFilterHandlers() {
31 | return parse(getPermissionInfo());
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/utils/Asserts.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.utils;
2 |
3 | public class Asserts {
4 | public Asserts() {
5 | }
6 |
7 | public static void check(boolean expression, String message) {
8 | if (!expression) {
9 | throw new IllegalArgumentException(message);
10 | }
11 | }
12 |
13 | public static void check(boolean expression, String message, Object... args) {
14 | if (!expression) {
15 | throw new IllegalArgumentException(String.format(message, args));
16 | }
17 | }
18 |
19 | public static void check(boolean expression, String message, Object arg) {
20 | if (!expression) {
21 | throw new IllegalArgumentException(String.format(message, arg));
22 | }
23 | }
24 |
25 | public static void notNull(Object object, String name) {
26 | if (object == null) {
27 | throw new IllegalArgumentException(name + " is null");
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/handler/dynamic/AbstractDynamicFindInjectInfoHandler.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.handler.dynamic;
2 |
3 | import io.github.heykb.sqlhelper.handler.InjectColumnInfoHandler;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * The type Default dynamic find column filter handler.
9 | *
10 | * @param the type parameter
11 | * @author heykb
12 | */
13 | public abstract class AbstractDynamicFindInjectInfoHandler implements DynamicFindInjectInfoHandler{
14 |
15 | /**
16 | * 编写将获取到的数据权限信息解析成handlers逻辑
17 | *
18 | * @param o the o
19 | * @return the list
20 | */
21 | abstract public List parse(T o);
22 |
23 | /**
24 | * 编写从上下文获取用户的数据权限信息逻辑
25 | *
26 | * @return the permission info
27 | */
28 | abstract public T getPermissionInfo();
29 |
30 | @Override
31 | public List findInjectInfoHandlers() {
32 | return parse(getPermissionInfo());
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/java/sample/mybatis/xml/sqlhelper/SimpleTenantInfoHandler.java:
--------------------------------------------------------------------------------
1 | package sample.mybatis.xml.sqlhelper;
2 |
3 |
4 | import io.github.heykb.sqlhelper.handler.abstractor.TenantInfoHandler;
5 |
6 | /**
7 | * @author heykb
8 | */
9 | //@Component
10 | public class SimpleTenantInfoHandler extends TenantInfoHandler {
11 | public static final String TENANT_ID = "1";
12 | @Override
13 | public String getTenantIdColumn() {
14 | return "tenant_id";
15 | }
16 | @Override
17 | public String getTenantId() {
18 | // 可以从
19 | // SecurityContextHolder.getContext().getAuthentication()
20 | return "'"+TENANT_ID+"'";
21 | }
22 | @Override
23 | public boolean checkTableName(String tableName) {
24 | return "city".equals(tableName);
25 | }
26 |
27 | @Override
28 | public boolean checkMapperId(String mapperId) {
29 | return !mapperId.contains("noPluginSelect");
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring/src/test/java/io/github/heykb/sqlhelper/spring/test/domain/Employee.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2010-2021 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.github.heykb.sqlhelper.spring.test.domain;
17 |
18 | import lombok.AllArgsConstructor;
19 | import lombok.Builder;
20 | import lombok.Data;
21 | import lombok.NoArgsConstructor;
22 |
23 | @Data
24 | @AllArgsConstructor
25 | @NoArgsConstructor
26 | @Builder
27 | public class Employee {
28 | private int id;
29 | }
30 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/src/main/java/sample/mybatis/xml/sqlhelper/SimpleTenantInfoHandler.java:
--------------------------------------------------------------------------------
1 | package sample.mybatis.xml.sqlhelper;
2 |
3 |
4 | import io.github.heykb.sqlhelper.handler.abstractor.TenantInfoHandler;
5 | import org.springframework.stereotype.Component;
6 |
7 | /**
8 | * @author heykb
9 | */
10 | @Component
11 | public class SimpleTenantInfoHandler extends TenantInfoHandler {
12 | public static final String TENANT_ID = "1";
13 | @Override
14 | public String getTenantIdColumn() {
15 | return "tenant_id";
16 | }
17 | @Override
18 | public String getTenantId() {
19 | // 可以从
20 | // SecurityContextHolder.getContext().getAuthentication()
21 | return "'"+TENANT_ID+"'";
22 | }
23 | @Override
24 | public boolean checkTableName(String tableName) {
25 | return "city".equals(tableName);
26 | }
27 |
28 | @Override
29 | public boolean checkMapperId(String mapperId) {
30 | return !mapperId.contains("noPluginSelect");
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/interceptor/SimpleProxyCursor.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.interceptor;
2 |
3 |
4 | import org.apache.ibatis.cursor.Cursor;
5 |
6 | import java.io.IOException;
7 | import java.util.Iterator;
8 |
9 | public class SimpleProxyCursor implements Cursor {
10 |
11 | private Cursor target;
12 |
13 | public SimpleProxyCursor(Cursor target) {
14 | this.target = target;
15 | }
16 |
17 | public Cursor getTarget() {
18 | return target;
19 | }
20 |
21 | @Override
22 | public boolean isOpen() {
23 | return target.isOpen();
24 | }
25 |
26 | @Override
27 | public boolean isConsumed() {
28 | return target.isConsumed();
29 | }
30 |
31 | @Override
32 | public int getCurrentIndex() {
33 | return target.getCurrentIndex();
34 | }
35 |
36 | @Override
37 | public void close() throws IOException {
38 | target.close();
39 | }
40 |
41 | @Override
42 | public Iterator iterator() {
43 | return target.iterator();
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 heykb
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/src/main/java/sample/mybatis/xml/mapper/HotelMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2021 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package sample.mybatis.xml.mapper;
17 |
18 | import org.apache.ibatis.annotations.Mapper;
19 | import sample.mybatis.xml.domain.Hotel;
20 |
21 | /**
22 | * @author Eduardo Macarron
23 | */
24 | @Mapper
25 | public interface HotelMapper {
26 |
27 | Hotel selectByCityId(int cityId);
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/java/sample/mybatis/xml/mapper/HotelMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2021 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package sample.mybatis.xml.mapper;
17 |
18 | import org.apache.ibatis.annotations.Mapper;
19 | import sample.mybatis.xml.domain.Hotel;
20 |
21 | /**
22 | * @author Eduardo Macarron
23 | */
24 | @Mapper
25 | public interface HotelMapper {
26 |
27 | Hotel selectByCityId(int cityId);
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring/src/test/java/io/github/heykb/sqlhelper/spring/test/BaseDataUtils.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.spring.test;
2 |
3 | import org.apache.ibatis.io.Resources;
4 | import org.apache.ibatis.jdbc.ScriptRunner;
5 |
6 | import javax.sql.DataSource;
7 | import java.io.IOException;
8 | import java.io.Reader;
9 | import java.sql.Connection;
10 | import java.sql.SQLException;
11 |
12 | public class BaseDataUtils {
13 |
14 | public static void runScript(DataSource ds, String resource) throws IOException, SQLException {
15 | try (Connection connection = ds.getConnection()) {
16 | ScriptRunner runner = new ScriptRunner(connection);
17 | runner.setAutoCommit(true);
18 | runner.setStopOnError(false);
19 | runner.setLogWriter(null);
20 | runner.setErrorLogWriter(null);
21 | runScript(runner, resource);
22 | }
23 | }
24 | public static void runScript(ScriptRunner runner, String resource) throws IOException, SQLException {
25 | try (Reader reader = Resources.getResourceAsReader(resource)) {
26 | runner.runScript(reader);
27 | }
28 | }
29 |
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/java/sample/mybatis/xml/service/impl/TestServiceImpl.java:
--------------------------------------------------------------------------------
1 | package sample.mybatis.xml.service.impl;
2 |
3 | import io.github.heykb.sqlhelper.dynamicdatasource.SqlHelperDsContextHolder;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.stereotype.Service;
6 | import org.springframework.transaction.annotation.Transactional;
7 | import sample.mybatis.xml.domain.City;
8 | import sample.mybatis.xml.mapper.CityMapper;
9 | import sample.mybatis.xml.service.AnotherTestService;
10 | import sample.mybatis.xml.service.TestService;
11 |
12 | @Service
13 | public class TestServiceImpl implements TestService {
14 |
15 | @Autowired
16 | private CityMapper cityMapper;
17 |
18 | @Autowired
19 | private AnotherTestService anotherTestService;
20 |
21 | @Override
22 | @Transactional
23 | public City findByState(String state) {
24 | City city = cityMapper.findByState(state);
25 | SqlHelperDsContextHolder.switchTo("mysql");
26 | city = anotherTestService.findByState(state);
27 | SqlHelperDsContextHolder.clear();
28 | return city;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/handler/abstractor/LogicDeleteInfoHandler.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.handler.abstractor;
2 |
3 | import io.github.heykb.sqlhelper.handler.InjectColumnInfoHandler;
4 |
5 | /**
6 | * 实现InjectColumnInfoHandler自动注入配置接口,代表逻辑删除自动注入配置
7 | * 配置逻辑删除的字段名称、字段值、以及逻辑删除语法样例(其中表名和where条件会在运行中被替换)
8 | * 使用时继承该类,并注入为一个bean
9 | *
10 | * @author heykb sqlDemo: update xx set status = false where id = 'xx' columnName: status notDeleteValue: true
11 | */
12 | public abstract class LogicDeleteInfoHandler implements InjectColumnInfoHandler {
13 | /**
14 | * 设置逻辑删除实例:update xx set status = false
15 | * (其中表名和where条件会在运行中被替换)
16 | *
17 | * @return sql demo
18 | */
19 | public abstract String getDeleteSqlDemo();
20 |
21 | /**
22 | * 设置正常未被删除时,逻辑删除字段的value
23 | *
24 | * @return not deleted value
25 | */
26 | public abstract String getNotDeletedValue();
27 |
28 | @Override
29 | public String getValue() {
30 | return getNotDeletedValue();
31 | }
32 |
33 | /**
34 | * 为所有子查询添加逻辑删除条件
35 | * @return
36 | */
37 | @Override
38 | public int getInjectTypes() {
39 | return CONDITION;
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/src/main/resources/sample/mybatis/xml/mapper/CityMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
22 |
23 |
26 |
27 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/resources/sample/mybatis/xml/mapper/CityMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
22 |
23 |
26 |
27 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/src/main/resources/sample/mybatis/xml/mapper/HotelMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
22 |
23 |
26 |
27 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/resources/sample/mybatis/xml/mapper/HotelMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
22 |
23 |
26 |
27 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/handler/abstractor/TenantInfoHandler.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.handler.abstractor;
2 |
3 |
4 | import io.github.heykb.sqlhelper.handler.InjectColumnInfoHandler;
5 |
6 | /**
7 | * 实现InjectColumnInfoHandler自动注入配置接口,代表多租户自动注入配置
8 | * 配置多租户的字段名称、租户value获取方式
9 | * 使用时继承该类,并注入为一个bean
10 | *
11 | * @author heykb
12 | */
13 | public abstract class TenantInfoHandler implements InjectColumnInfoHandler {
14 | /**
15 | * 设置代表租户字段名称
16 | *
17 | * @return tenant id column
18 | */
19 | public abstract String getTenantIdColumn();
20 |
21 | /**
22 | * 当前租户value获取方式
23 | *
24 | * @return tenant id
25 | */
26 | public abstract String getTenantId();
27 |
28 | @Override
29 | public String getColumnName() {
30 | return getTenantIdColumn();
31 | }
32 |
33 | @Override
34 | public String getValue() {
35 | return getTenantId();
36 | }
37 |
38 | @Override
39 | public boolean isExistOverride() {
40 | return false;
41 | }
42 |
43 | /**
44 | * 为所有子查询添加租户条件 为所有插入语句注入租户id
45 | * @return
46 | */
47 | @Override
48 | public int getInjectTypes() {
49 | return CONDITION|INSERT;
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | mybatis-sqlhelper-spring-boot
7 | io.github.heykb
8 | ${revision}
9 |
10 | 4.0.0
11 |
12 | mybatis-spring-boot-sample-xml
13 | mybatis-spring-boot-dynamic-datasource
14 |
15 |
16 | mybatis-sqlhelper-spring-boot-samples
17 | pom
18 |
19 | true
20 |
21 |
22 |
23 |
24 |
25 | maven-deploy-plugin
26 | 3.0.0-M1
27 |
28 | true
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/src/main/java/sample/mybatis/xml/mapper/CityMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2021 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package sample.mybatis.xml.mapper;
17 |
18 | import org.apache.ibatis.annotations.Mapper;
19 | import org.apache.ibatis.annotations.Param;
20 | import org.apache.ibatis.annotations.Select;
21 | import sample.mybatis.xml.domain.City;
22 |
23 | /**
24 | * @author Eddú Meléndez
25 | */
26 | @Mapper
27 | public interface CityMapper {
28 |
29 | @Select("select id, name, state, country from city where state = #{state}")
30 | City findByState(@Param("state") String state);
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring/src/test/java/io/github/heykb/sqlhelper/spring/test/dao/PeopleMapper.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.spring.test.dao;
2 |
3 | import io.github.heykb.sqlhelper.spring.test.domain.People;
4 | import org.apache.ibatis.annotations.Mapper;
5 | import org.apache.ibatis.annotations.Param;
6 |
7 | import java.util.List;
8 |
9 | /**
10 | * @author kb
11 | */
12 | @Mapper
13 | public interface PeopleMapper {
14 | /**
15 | * [新增]
16 | * @author kb
17 | **/
18 | int insert(People people);
19 | /**
20 | * [刪除]
21 | * @author kb
22 | **/
23 | int delete(@Param("id") String id);
24 |
25 | /**
26 | * [更新]
27 | * @author kb
28 | **/
29 | int update(People people);
30 |
31 | /**
32 | * [查询] 根据主键 id 查询
33 | * @author kb
34 | **/
35 | People select(@Param("id") String id);
36 | /**
37 | * [查询] 分页查询
38 | * @author kb
39 | **/
40 | List selectList(People people);
41 |
42 |
43 | People leftJoinSelect(@Param("id") String id);
44 |
45 |
46 | List noPluginSelect(People people);
47 |
48 | /**
49 | * [查询] 批量插入
50 | * @author kb
51 | **/
52 | int batchInsert(List peoples);
53 |
54 | /**
55 | * [查询] 批量删除
56 | * @author kb
57 | **/
58 | int batchDelete(List ids);
59 | }
60 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-starter/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | mybatis-sqlhelper-spring-boot
7 | io.github.heykb
8 | ${revision}
9 |
10 | 4.0.0
11 | mybatis-sqlhelper-spring-boot-starter
12 | mybatis-sqlhelper-spring-boot-starter
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter
17 |
18 |
19 | org.mybatis.spring.boot
20 | mybatis-spring-boot-starter
21 |
22 |
23 | io.github.heykb
24 | mybatis-sqlhelper-spring-boot-autoconfigure
25 |
26 |
27 | io.github.heykb
28 | mybatis-sqlhelper-spring
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/java/sample/mybatis/xml/mapper/CityMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2021 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package sample.mybatis.xml.mapper;
17 |
18 | import org.apache.ibatis.annotations.Mapper;
19 | import org.apache.ibatis.annotations.Param;
20 | import org.apache.ibatis.annotations.Select;
21 | import sample.mybatis.xml.domain.City;
22 |
23 | /**
24 | * @author Eddú Meléndez
25 | */
26 | @Mapper
27 | public interface CityMapper {
28 |
29 | @Select("select id, name, state, country from city where state = #{state}")
30 | City findByState(@Param("state") String state);
31 |
32 | @Select("select id, name, state, country from city where state = #{state}")
33 | City findByState2(@Param("state") String state);
34 |
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-autoconfigure/src/main/java/io/github/heykb/sqlhelper/autoconfigure/SqlHelperAutoConfiguration.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.autoconfigure;
2 |
3 | import io.github.heykb.sqlhelper.handler.abstractor.LogicDeleteInfoHandler;
4 | import io.github.heykb.sqlhelper.spring.PropertyLogicDeleteInfoHandler;
5 | import io.github.heykb.sqlhelper.spring.SqlHelperPluginFactoryBean;
6 | import org.springframework.boot.autoconfigure.AutoConfigureAfter;
7 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
8 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
9 | import org.springframework.context.annotation.Bean;
10 | import org.springframework.context.annotation.Configuration;
11 |
12 | @Configuration
13 | @EnableConfigurationProperties(SqlHelperProperties.class)
14 |
15 | public class SqlHelperAutoConfiguration {
16 |
17 | private final SqlHelperProperties properties;
18 |
19 | public SqlHelperAutoConfiguration(SqlHelperProperties properties) {
20 | this.properties = properties;
21 | }
22 |
23 | @Bean
24 | @ConditionalOnMissingBean(LogicDeleteInfoHandler.class)
25 | PropertyLogicDeleteInfoHandler logicDeleteInfoHandler(){
26 | return properties.getLogicDelete();
27 | }
28 |
29 | @Bean
30 | SqlHelperPluginFactoryBean sqlHelperPluginFactoryBean(){
31 | SqlHelperPluginFactoryBean factoryBean = new SqlHelperPluginFactoryBean();
32 | factoryBean.setProperties(properties.getProperties());
33 | return factoryBean;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring/src/test/java/io/github/heykb/sqlhelper/spring/test/db/createDb.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE people (
2 | name varchar(255) ,
3 | age int,
4 | email varchar(255) ,
5 | id varchar(255) NOT NULL,
6 | tenant_id varchar(255),
7 | dept_id varchar(255),
8 | created_time timestamp(6),
9 | created_by varchar(255) ,
10 | updated_time timestamp(6),
11 | updated_by varchar(255),
12 | CONSTRAINT people_pkey PRIMARY KEY (id)
13 | )
14 | ;
15 | -- COMMENT ON COLUMN people.created_time IS '创建时间';
16 | -- COMMENT ON COLUMN people.created_by IS '创建人';
17 | -- COMMENT ON COLUMN people.updated_time IS '更新时间';
18 | -- COMMENT ON COLUMN people.updated_by IS '更新人';
19 |
20 | CREATE TABLE department (
21 | dept_id varchar(255) NOT NULL,
22 | dept_name varchar(255) ,
23 | tenant_id varchar(255),
24 | created_time timestamp(6) ,
25 | created_by varchar(255) ,
26 | updated_time timestamp(6),
27 | updated_by varchar(255) ,
28 | CONSTRAINT dept_pkey PRIMARY KEY (dept_id)
29 | )
30 | ;
31 |
32 |
33 | -- COMMENT ON COLUMN department.created_time IS '创建时间';
34 | --
35 | -- COMMENT ON COLUMN department.created_by IS '创建人';
36 | --
37 | -- COMMENT ON COLUMN department.updated_time IS '更新时间';
38 | --
39 | -- COMMENT ON COLUMN department.updated_by IS '更新人';
40 |
41 | -- ----------------------------
42 | -- Records of people
43 | -- ----------------------------
44 | INSERT INTO people VALUES ('tom', NULL, NULL, '1', '1', NULL, '2021-11-12 12:18:14.235029', 'heykb', NULL, 'heykb');
45 | INSERT INTO people VALUES ('tom', NULL, NULL, '2', '2', NULL, '2021-11-12 12:18:14.235029', 'heykb', NULL, 'heykb');
46 |
47 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/dynamicdatasource/PrimaryDatasource.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.dynamicdatasource;
2 |
3 | import io.github.heykb.sqlhelper.config.SqlHelperException;
4 |
5 | import javax.sql.DataSource;
6 | import java.sql.Connection;
7 | import java.sql.SQLException;
8 |
9 | /**
10 | * The type Primary datasource.
11 | */
12 | public class PrimaryDatasource extends SimpleProxyDatasource {
13 | private String primaryDsInitialSubspace;
14 |
15 | /**
16 | * Instantiates a new Primary datasource.
17 | *
18 | * @param dataSource the data source
19 | */
20 | public PrimaryDatasource(DataSource dataSource) {
21 | super(dataSource);
22 | init();
23 | }
24 |
25 | private void init() {
26 | try (Connection connection = super.getConnection()) {
27 | this.primaryDsInitialSubspace = SupportedConnectionSubspaceChange.getCurrentSubspaceIfSupport(connection, null);
28 | } catch (SQLException e) {
29 | throw new SqlHelperException(e);
30 | }
31 | }
32 |
33 | @Override
34 | public Connection getConnection() throws SQLException {
35 | Connection connection = super.getConnection();
36 | SupportedConnectionSubspaceChange.changeSubspaceIfSupport(connection, primaryDsInitialSubspace,null);
37 | return connection;
38 | }
39 |
40 | @Override
41 | public Connection getConnection(String username, String password) throws SQLException {
42 | Connection connection = super.getConnection(username, password);
43 | SupportedConnectionSubspaceChange.changeSubspaceIfSupport(connection, primaryDsInitialSubspace,null);
44 | return connection;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/src/main/java/sample/mybatis/xml/SampleXmlApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2021 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package sample.mybatis.xml;
17 |
18 | import org.springframework.boot.CommandLineRunner;
19 | import org.springframework.boot.SpringApplication;
20 | import org.springframework.boot.autoconfigure.SpringBootApplication;
21 | import sample.mybatis.xml.mapper.HotelMapper;
22 | import sample.mybatis.xml.service.TestService;
23 |
24 | @SpringBootApplication
25 | public class SampleXmlApplication implements CommandLineRunner {
26 |
27 | public static void main(String[] args) {
28 | SpringApplication.run(SampleXmlApplication.class, args);
29 | }
30 |
31 | private final TestService testService;
32 |
33 |
34 | public SampleXmlApplication(TestService testService, HotelMapper hotelMapper) {
35 | this.testService = testService;
36 | }
37 |
38 | @Override
39 | @SuppressWarnings("squid:S106")
40 | public void run(String... args) {
41 | System.out.println(this.testService.findByState("CA"));
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/README_SPRING_BOOT.md:
--------------------------------------------------------------------------------
1 | # MyBatis SqlHelper Spring Boot
2 |
3 | [](https://maven-badges.herokuapp.com/maven-central/io.github.heykb/mybatis-sqlhelper-spring-boot-starter)
4 |
5 | [MyBatis SqlHelper](https://github.com/heykb/mybatis-sqlhelper)的spring boot集成版本,自动配置插件、从容器中自动扫描注入信息添加到插件。
6 |
7 | ~~~xml
8 |
9 | io.github.heykb
10 | mybatis-sqlhelper-spring-boot-starter
11 | 3.0.0.SR1
12 |
13 | ~~~
14 |
15 | ## IDEA提示
16 | 
17 |
18 | ## 将创建的注入信息类生成bean注入到容器即可生效。
19 | ~~~java
20 | @Component
21 | public class InjectUpdateByHandler implements InjectColumnInfoHandler {
22 | @Override
23 | public String getColumnName() {
24 | return "updated_by";
25 | }
26 |
27 | @Override
28 | public String getValue() {
29 | return "'zrc'";
30 | }
31 |
32 | @Override
33 | public int getInjectTypes() {
34 | return INSERT|UPDATE;
35 | }
36 |
37 | @Override
38 | public boolean checkTableName(String tableName) {
39 | return tableName.equals("people") || tableName.equals("dept_data_test");
40 | }
41 | }
42 | ~~~
43 |
44 | 与直接使用[MyBatis SqlHelper](https://github.com/heykb/mybatis-sqlhelper)与[MyBatis SqlHelper Spring](https://github.com/heykb/mybatis-sqlhelper-spring)不同,逻辑删除的功能默认是关闭的。你可通过application.properties配置逻辑删除的信息。自定义LogicDeleteInfoHanlder的bean后,环境变量配置失效。
45 |
46 | ## 未完待续。。(如果你有兴趣,右上角watch该项目获得最新的动态)
47 |
48 | ## 参与贡献
49 |
50 | 如果你发现问题,提交issue。
51 |
52 | 如果你解决了问题,fork项目提交pull request。
53 |
54 | ## 联系我
55 | QQ: 1259609102
56 | email: 1259609102@qq.com,bigsheller08@gmail.com
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | mybatis-sqlhelper-spring-boot-samples
6 | io.github.heykb
7 | ${revision}
8 |
9 | mybatis-spring-boot-sample-xml
10 | jar
11 | mybatis-spring-boot-sample-xml
12 |
13 |
14 |
15 | io.github.heykb
16 | mybatis-sqlhelper-spring-boot-starter
17 | ${project.version}
18 |
19 |
20 | com.h2database
21 | h2
22 | runtime
23 |
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-test
28 | test
29 |
30 |
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-maven-plugin
36 | ${spring-boot.version}
37 |
38 |
39 |
40 | repackage
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/interceptor/ColumnFilterCursor.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.interceptor;
2 |
3 | import io.github.heykb.sqlhelper.utils.CommonUtils;
4 | import org.apache.ibatis.cursor.Cursor;
5 |
6 | import java.util.*;
7 | import java.util.function.Consumer;
8 |
9 | public class ColumnFilterCursor extends SimpleProxyCursor {
10 |
11 | private Set ignoreColumns;
12 | private boolean isMapUnderscoreToCamelCase;
13 |
14 |
15 | public ColumnFilterCursor(Cursor target, Set ignoreColumns, boolean isMapUnderscoreToCamelCase) {
16 | super(target);
17 | this.ignoreColumns = ignoreColumns;
18 | this.isMapUnderscoreToCamelCase = isMapUnderscoreToCamelCase;
19 | }
20 |
21 | @Override
22 | public Iterator iterator() {
23 | return new ColumnFilterIterator(super.iterator());
24 | }
25 |
26 | @Override
27 | public void forEach(Consumer super T> action) {
28 | Objects.requireNonNull(action);
29 | for (T t : this) {
30 | action.accept(t);
31 | }
32 | }
33 |
34 | @Override
35 | public Spliterator spliterator() {
36 | return Spliterators.spliteratorUnknownSize(iterator(), 0);
37 | }
38 |
39 |
40 | class ColumnFilterIterator implements Iterator {
41 | private Iterator target;
42 |
43 | public ColumnFilterIterator(Iterator target) {
44 | this.target = target;
45 | }
46 |
47 | @Override
48 | public Object next() {
49 | Object re = target.next();
50 | CommonUtils.filterColumns(re, ignoreColumns, isMapUnderscoreToCamelCase);
51 | return re;
52 | }
53 |
54 | public Iterator getTarget() {
55 | return target;
56 | }
57 |
58 | @Override
59 | public boolean hasNext() {
60 | return target.hasNext();
61 | }
62 |
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/src/main/java/sample/mybatis/xml/domain/Hotel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2021 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | /**
17 | * @author Eduardo Macarron
18 | */
19 | package sample.mybatis.xml.domain;
20 |
21 | import java.io.Serializable;
22 |
23 | public class Hotel implements Serializable {
24 |
25 | private static final long serialVersionUID = 1L;
26 |
27 | private Long city;
28 |
29 | private String name;
30 |
31 | private String address;
32 |
33 | private String zip;
34 |
35 | public Long getCity() {
36 | return city;
37 | }
38 |
39 | public void setCity(Long city) {
40 | this.city = city;
41 | }
42 |
43 | public String getName() {
44 | return name;
45 | }
46 |
47 | public void setName(String name) {
48 | this.name = name;
49 | }
50 |
51 | public String getAddress() {
52 | return address;
53 | }
54 |
55 | public void setAddress(String address) {
56 | this.address = address;
57 | }
58 |
59 | public String getZip() {
60 | return zip;
61 | }
62 |
63 | public void setZip(String zip) {
64 | this.zip = zip;
65 | }
66 |
67 | @Override
68 | public String toString() {
69 | return getCity() + "," + getName() + "," + getAddress() + "," + getZip();
70 | }
71 |
72 | }
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/java/sample/mybatis/xml/domain/Hotel.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2021 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | /**
17 | * @author Eduardo Macarron
18 | */
19 | package sample.mybatis.xml.domain;
20 |
21 | import java.io.Serializable;
22 |
23 | public class Hotel implements Serializable {
24 |
25 | private static final long serialVersionUID = 1L;
26 |
27 | private Long city;
28 |
29 | private String name;
30 |
31 | private String address;
32 |
33 | private String zip;
34 |
35 | public Long getCity() {
36 | return city;
37 | }
38 |
39 | public void setCity(Long city) {
40 | this.city = city;
41 | }
42 |
43 | public String getName() {
44 | return name;
45 | }
46 |
47 | public void setName(String name) {
48 | this.name = name;
49 | }
50 |
51 | public String getAddress() {
52 | return address;
53 | }
54 |
55 | public void setAddress(String address) {
56 | this.address = address;
57 | }
58 |
59 | public String getZip() {
60 | return zip;
61 | }
62 |
63 | public void setZip(String zip) {
64 | this.zip = zip;
65 | }
66 |
67 | @Override
68 | public String toString() {
69 | return getCity() + "," + getName() + "," + getAddress() + "," + getZip();
70 | }
71 |
72 | }
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-sample-xml/src/main/java/sample/mybatis/xml/domain/City.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2021 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package sample.mybatis.xml.domain;
17 |
18 | import java.io.Serializable;
19 |
20 | /**
21 | * @author Eddú Meléndez
22 | */
23 | public class City implements Serializable {
24 |
25 | private static final long serialVersionUID = 1L;
26 |
27 | private Long id;
28 |
29 | private String name;
30 |
31 | private String state;
32 |
33 | private String country;
34 |
35 | public Long getId() {
36 | return this.id;
37 | }
38 |
39 | public void setId(Long id) {
40 | this.id = id;
41 | }
42 |
43 | public String getName() {
44 | return this.name;
45 | }
46 |
47 | public void setName(String name) {
48 | this.name = name;
49 | }
50 |
51 | public String getState() {
52 | return this.state;
53 | }
54 |
55 | public void setState(String state) {
56 | this.state = state;
57 | }
58 |
59 | public String getCountry() {
60 | return this.country;
61 | }
62 |
63 | public void setCountry(String country) {
64 | this.country = country;
65 | }
66 |
67 | @Override
68 | public String toString() {
69 | return getId() + "," + getName() + "," + getState() + "," + getCountry();
70 | }
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/java/sample/mybatis/xml/domain/City.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2021 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package sample.mybatis.xml.domain;
17 |
18 | import java.io.Serializable;
19 |
20 | /**
21 | * @author Eddú Meléndez
22 | */
23 | public class City implements Serializable {
24 |
25 | private static final long serialVersionUID = 1L;
26 |
27 | private Long id;
28 |
29 | private String name;
30 |
31 | private String state;
32 |
33 | private String country;
34 |
35 | public Long getId() {
36 | return this.id;
37 | }
38 |
39 | public void setId(Long id) {
40 | this.id = id;
41 | }
42 |
43 | public String getName() {
44 | return this.name;
45 | }
46 |
47 | public void setName(String name) {
48 | this.name = name;
49 | }
50 |
51 | public String getState() {
52 | return this.state;
53 | }
54 |
55 | public void setState(String state) {
56 | this.state = state;
57 | }
58 |
59 | public String getCountry() {
60 | return this.country;
61 | }
62 |
63 | public void setCountry(String country) {
64 | this.country = country;
65 | }
66 |
67 | @Override
68 | public String toString() {
69 | return getId() + "," + getName() + "," + getState() + "," + getCountry();
70 | }
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/typeHandler/ColumnFilterTypeHandler.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.typeHandler;
2 |
3 | import org.apache.ibatis.type.JdbcType;
4 | import org.apache.ibatis.type.TypeHandler;
5 |
6 | import java.sql.CallableStatement;
7 | import java.sql.PreparedStatement;
8 | import java.sql.ResultSet;
9 | import java.sql.SQLException;
10 | import java.util.Collections;
11 | import java.util.List;
12 |
13 | /**
14 | * @author heykb
15 | */
16 | public class ColumnFilterTypeHandler implements TypeHandler {
17 |
18 | private TypeHandler typeHandler;
19 | private List removeIndex;
20 |
21 | public ColumnFilterTypeHandler(TypeHandler typeHandler, List removeIndex) {
22 | this.typeHandler = typeHandler;
23 | this.removeIndex = removeIndex;
24 | Collections.sort(removeIndex,Integer::compareTo);
25 | }
26 |
27 | /*
28 | 1 2 3 4 5 6
29 | 1 3 4 6
30 | 2 5
31 | */
32 | @Override
33 | public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
34 | int j = 0;
35 | for(Integer item:removeIndex){
36 | if(i < item){
37 | break;
38 | }else if(i==item){
39 | return;
40 | }else if(i > item){
41 | ++j;
42 | }
43 | }
44 | typeHandler.setParameter(ps,i-j,parameter,jdbcType);
45 | }
46 |
47 | @Override
48 | public Object getResult(ResultSet rs, String columnName) throws SQLException {
49 | return typeHandler.getResult(rs,columnName);
50 | }
51 |
52 | @Override
53 | public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
54 | return typeHandler.getResult(rs,columnIndex);
55 | }
56 |
57 | @Override
58 | public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
59 | return typeHandler.getResult(cs,columnIndex);
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | mybatis-sqlhelper-spring-boot-samples
6 | io.github.heykb
7 | ${revision}
8 |
9 | mybatis-spring-boot-dynamic-datasource
10 | jar
11 | mybatis-spring-boot-dynamic-datasource
12 |
13 |
14 |
15 | io.github.heykb
16 | mybatis-sqlhelper-spring-boot-starter
17 | ${project.version}
18 |
19 |
20 | com.h2database
21 | h2
22 | runtime
23 |
24 |
25 | mysql
26 | mysql-connector-java
27 | 8.0.27
28 | runtime
29 |
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-test
34 | test
35 |
36 |
37 |
38 |
39 |
40 | org.springframework.boot
41 | spring-boot-maven-plugin
42 | ${spring-boot.version}
43 |
44 |
45 |
46 | repackage
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring/src/main/java/io/github/heykb/sqlhelper/spring/SqlHelperPluginFactoryBean.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.spring;
2 |
3 | import io.github.heykb.sqlhelper.handler.ColumnFilterInfoHandler;
4 | import io.github.heykb.sqlhelper.handler.InjectColumnInfoHandler;
5 | import io.github.heykb.sqlhelper.handler.dynamic.DynamicFindColumnFilterHandler;
6 | import io.github.heykb.sqlhelper.handler.dynamic.DynamicFindInjectInfoHandler;
7 | import io.github.heykb.sqlhelper.interceptor.SqlHelperPlugin;
8 | import lombok.Setter;
9 | import org.springframework.beans.factory.FactoryBean;
10 | import org.springframework.beans.factory.annotation.Autowired;
11 |
12 | import java.util.List;
13 | import java.util.Properties;
14 |
15 |
16 | public class SqlHelperPluginFactoryBean implements FactoryBean {
17 | @Autowired(required = false)
18 | private List injectColumnInfoHandlers;
19 | @Autowired(required = false)
20 | private List columnFilterInfoHandlers;
21 | @Autowired(required = false)
22 | private DynamicFindColumnFilterHandler dynamicFindColumnFilterHandler;
23 | @Autowired(required = false)
24 | private DynamicFindInjectInfoHandler dynamicFindInjectInfoHandler;
25 |
26 | @Setter
27 | private Properties properties;
28 |
29 | @Override
30 | public SqlHelperPlugin getObject() throws Exception {
31 | SqlHelperPlugin re = new SqlHelperPlugin();
32 | re.setDynamicFindColumnFilterHandler(dynamicFindColumnFilterHandler);
33 | re.setDynamicFindInjectInfoHandler(dynamicFindInjectInfoHandler);
34 | re.setInjectColumnInfoHandlers(injectColumnInfoHandlers);
35 | re.setColumnFilterInfoHandlers(columnFilterInfoHandlers);
36 | re.setProperties(properties);
37 | return re;
38 | }
39 |
40 |
41 | @Override
42 | public Class> getObjectType() {
43 | return SqlHelperPlugin.class;
44 | }
45 |
46 | @Override
47 | public boolean isSingleton() {
48 | return true;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/dynamicdatasource/SqlHelperDsContextHolder.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.dynamicdatasource;
2 |
3 |
4 | import org.apache.ibatis.logging.Log;
5 | import org.apache.ibatis.logging.LogFactory;
6 |
7 | import java.util.Stack;
8 | import java.util.concurrent.Callable;
9 |
10 | /**
11 | * The type Sql helper ds context holder.
12 | */
13 | public class SqlHelperDsContextHolder {
14 | private static final Log log = LogFactory.getLog(SqlHelperDsContextHolder.class);
15 | private static final ThreadLocal> CONTEXTHOLDER = new ThreadLocal<>();
16 |
17 | /**
18 | * 切换数据源
19 | *
20 | * @param logicName 数据源名称,null代表默认数据源
21 | */
22 | public static void switchTo(String logicName) {
23 | Stack switchStack = CONTEXTHOLDER.get();
24 | if(switchStack == null){
25 | switchStack = new Stack<>();
26 | CONTEXTHOLDER.set(switchStack);
27 | }
28 | switchStack.push(logicName);
29 | }
30 |
31 | /**
32 | * 退出当前数据源,会返回到上一次设置的值
33 | */
34 | public static void backToLast(){
35 | Stack switchStack = CONTEXTHOLDER.get();
36 | if(switchStack!=null && !switchStack.empty()){
37 | switchStack.pop();
38 | }
39 | }
40 |
41 | /**
42 | * Clear.
43 | */
44 | public static void clear() {
45 | CONTEXTHOLDER.remove();
46 | }
47 |
48 |
49 | /**
50 | * Get string.
51 | *
52 | * @return the string
53 | */
54 | public static String get() {
55 | Stack switchStack = CONTEXTHOLDER.get();
56 | if(switchStack!=null && !switchStack.empty()){
57 | return switchStack.peek();
58 | }
59 | return null;
60 | }
61 |
62 | public static R executeOn(String datasourceName, Callable callable) {
63 | switchTo(datasourceName);
64 | try {
65 | R re = callable.call();
66 | return re;
67 | } catch (Exception e) {
68 | throw new RuntimeException(e);
69 | }finally {
70 | backToLast();
71 | }
72 | }
73 |
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/dynamicdatasource/SimpleProxyDatasource.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.dynamicdatasource;
2 |
3 | import javax.sql.DataSource;
4 | import java.io.PrintWriter;
5 | import java.sql.Connection;
6 | import java.sql.SQLException;
7 | import java.sql.SQLFeatureNotSupportedException;
8 | import java.util.logging.Logger;
9 |
10 | /**
11 | * The type Simple proxy datasource.
12 | */
13 | public class SimpleProxyDatasource implements DataSource {
14 | /**
15 | * The Data source.
16 | */
17 | protected DataSource dataSource;
18 |
19 | /**
20 | * Instantiates a new Simple proxy datasource.
21 | *
22 | * @param dataSource the data source
23 | */
24 | public SimpleProxyDatasource(DataSource dataSource) {
25 | this.dataSource = dataSource;
26 | }
27 |
28 | @Override
29 | public Connection getConnection() throws SQLException {
30 | return dataSource.getConnection();
31 | }
32 |
33 | @Override
34 | public Connection getConnection(String username, String password) throws SQLException {
35 | return dataSource.getConnection(username, password);
36 | }
37 |
38 | @Override
39 | public T unwrap(Class iface) throws SQLException {
40 | return dataSource.unwrap(iface);
41 | }
42 |
43 | @Override
44 | public boolean isWrapperFor(Class> iface) throws SQLException {
45 | return dataSource.isWrapperFor(iface);
46 | }
47 |
48 | @Override
49 | public PrintWriter getLogWriter() throws SQLException {
50 | return dataSource.getLogWriter();
51 | }
52 |
53 | @Override
54 | public void setLogWriter(PrintWriter out) throws SQLException {
55 | dataSource.setLogWriter(out);
56 | }
57 |
58 | @Override
59 | public int getLoginTimeout() throws SQLException {
60 | return dataSource.getLoginTimeout();
61 | }
62 |
63 | @Override
64 | public void setLoginTimeout(int seconds) throws SQLException {
65 | dataSource.setLoginTimeout(seconds);
66 | }
67 |
68 | @Override
69 | public Logger getParentLogger() throws SQLFeatureNotSupportedException {
70 | return dataSource.getParentLogger();
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/DYNAMIC_DATASOURCE_README.md:
--------------------------------------------------------------------------------
1 | # 多数据源专篇
2 | * 动态新增数据源,数据源管理能力
3 | * 动态切换数据源
4 | * 支持连接子空间,如mysql 一个连接支持切换不同数据库,可以配置不同的逻辑数据源名称(添加时设置不同的数据源名称,相同的数据源id)但是使用同一个数据源连接池,使用过程中我们会帮你自动切换到指定子空间
5 |
6 |
7 | ## 使用多数据源功能,配置SpringSqlHelperDsManager数据源管理bean即可开始使用。
8 | xml配置
9 | ~~~xml
10 |
11 | ~~~
12 | spring boot配置
13 | ~~~java
14 | @Bean
15 | public SqlHelperDsManager springSqlHelperDsManager(){
16 | return new SpringSqlHelperDsManager();
17 | }
18 | ~~~
19 | 然后在程序中使用SqlHelperDsManager新增添加数据源
20 | ~~~java
21 | @Autowired
22 | private SqlHelperDsManager sqlHelperDsManager;
23 | ~~~
24 | ~~~java
25 | sqlHelperDsManager.put("mysql", LogicDsMeta.builder()
26 | .datasourceId("localhost:3306")
27 | .expectedSubspaceType(ConnectionSubspaceTypeEnum.DATABASE)
28 | .createFunc(s->new PooledDataSource("com.mysql.cj.jdbc.Driver", "jdbc:mysql://localhost:3306/test", "root", "123456")).build());
29 | ~~~
30 | LogicDsMeta新增数据源参数解释
31 | ~~~java
32 | /**
33 | * 数据源id,当已注册数据源中存在相同id时会复用数据源,并且会触发数据源升级回调方法如果存在的话。
34 | * 使用同一个数据源id的不同逻辑数据源可以设置不同的subspace子空间。如mysql支持同一个连接切换不同数据库
35 | */
36 | private String datasourceId;
37 | /**
38 | * 设置期望的子空间类型(不是所有的数据库都支持同一个连接进行切换)
39 | * 主要作用在于当用户期望和软件支持不匹配能快速失败及时报错
40 | */
41 | private ConnectionSubspaceTypeEnum expectedSubspaceType;
42 | /**
43 | * 当逻辑数据源使用时将连接切换到指定的子空间。子空间的名称。仅当数据库类型支持子空间时有效
44 | * nullable
45 | */
46 | private String subspace;
47 | /**
48 | * 当数据源id不存在时,使用此回调创建新的数据源
49 | */
50 | private Function createFunc;
51 | ~~~
52 | 切换数据源
53 | ~~~java
54 | SqlHelperDsContextHolder.switchTo("mysql");
55 |
56 | // 返回上一次设置
57 | SqlHelperDsContextHolder.backToLast();
58 |
59 | // 清空切换栈,使用主数据源
60 | SqlHelperDsContextHolder.clear();
61 |
62 | // 切换到主数据源
63 | SqlHelperDsContextHolder.switchTo(null)
64 |
65 | // 切换到指定数据源下运行,完毕后自动切换回来
66 | SqlHelperDsContextHolder.executeOn("mysql",()->{
67 | // 执行逻辑
68 | return xxx;
69 | })
70 | ~~~
71 |
72 | ## 未完待续。。(如果你有兴趣,右上角watch该项目获得最新的动态)
73 |
74 | ## 参与贡献
75 |
76 | 如果你发现问题,提交issue。
77 |
78 | 如果你解决了问题,fork项目提交pull request。
79 |
80 | ## 联系我
81 | QQ: 1259609102
82 | email: 1259609102@qq.com,bigsheller08@gmail.com
--------------------------------------------------------------------------------
/mybatis-sqlhelper-spring-boot/mybatis-sqlhelper-spring-boot-samples/mybatis-spring-boot-dynamic-datasource/src/main/java/sample/mybatis/xml/Application.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015-2021 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package sample.mybatis.xml;
17 |
18 |
19 | import io.github.heykb.sqlhelper.dynamicdatasource.ConnectionSubspaceTypeEnum;
20 | import io.github.heykb.sqlhelper.dynamicdatasource.LogicDsMeta;
21 | import io.github.heykb.sqlhelper.dynamicdatasource.SqlHelperDsManager;
22 | import org.apache.ibatis.datasource.pooled.PooledDataSource;
23 | import org.springframework.boot.CommandLineRunner;
24 | import org.springframework.boot.SpringApplication;
25 | import org.springframework.boot.autoconfigure.SpringBootApplication;
26 | import sample.mybatis.xml.service.TestService;
27 |
28 | @SpringBootApplication
29 | public class Application implements CommandLineRunner {
30 |
31 | public static void main(String[] args) {
32 | SpringApplication.run(Application.class, args);
33 | }
34 |
35 | private final TestService testService;
36 |
37 |
38 | public Application(TestService testService, SqlHelperDsManager sqlHelperDsManager) {
39 | this.testService = testService;
40 | sqlHelperDsManager.put("mysql", LogicDsMeta.builder()
41 | .datasourceId("localhost:3306")
42 | .expectedSubspaceType(ConnectionSubspaceTypeEnum.DATABASE)
43 | .createFunc(()->new PooledDataSource("com.mysql.cj.jdbc.Driver", "jdbc:mysql://localhost:3306/test", "root", "123456")).build());
44 | }
45 |
46 | @Override
47 | @SuppressWarnings("squid:S106")
48 | public void run(String... args) {
49 | System.out.println(this.testService.findByState("CA"));
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/main/java/io/github/heykb/sqlhelper/handler/abstractor/BinaryConditionInjectInfoHandler.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.handler.abstractor;
2 |
3 | import com.alibaba.druid.DbType;
4 | import com.alibaba.druid.sql.ast.SQLExpr;
5 | import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
6 | import io.github.heykb.sqlhelper.handler.InjectColumnInfoHandler;
7 | import io.github.heykb.sqlhelper.utils.CommonUtils;
8 |
9 | import java.util.Map;
10 |
11 | /**
12 | * 复杂条件注入
13 | *
14 | * @author heykb
15 | */
16 | public abstract class BinaryConditionInjectInfoHandler implements InjectColumnInfoHandler {
17 |
18 |
19 | @Override
20 | public int getInjectTypes() {
21 | return CONDITION;
22 | }
23 | /**
24 | * Gets left condition inject info.
25 | *
26 | * @return the left condition inject info
27 | */
28 | abstract public InjectColumnInfoHandler getLeftConditionInjectInfo();
29 |
30 | /**
31 | * Gets right condition inject info.
32 | *
33 | * @return the right condition inject info
34 | */
35 | abstract public InjectColumnInfoHandler getRightConditionInjectInfo();
36 |
37 | @Override
38 | public String op() {
39 | return "and";
40 | }
41 |
42 | @Override
43 | public SQLExpr toConditionSQLExpr(String tableAlias, DbType dbType, Map columnAliasMap, boolean isMapUnderscoreToCamelCase) {
44 | SQLExpr left = null;
45 | SQLExpr right = null;
46 | if(getLeftConditionInjectInfo()!=null){
47 | left = getLeftConditionInjectInfo().toConditionSQLExpr(tableAlias, dbType, columnAliasMap, isMapUnderscoreToCamelCase);
48 | }
49 | if(getRightConditionInjectInfo()!=null){
50 | right = getRightConditionInjectInfo().toConditionSQLExpr(tableAlias, dbType, columnAliasMap, isMapUnderscoreToCamelCase);
51 | }
52 | if(left != null && right !=null){
53 | return new SQLBinaryOpExpr(left, right, CommonUtils.convert(op()));
54 | }else if(left == null){
55 | return right;
56 | }else if(right == null){
57 | return left;
58 | }
59 | return null;
60 | }
61 |
62 |
63 |
64 | @Override
65 | public String getColumnName() {
66 | return null;
67 | }
68 |
69 | @Override
70 | public String getValue() {
71 | return null;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/mybatis-sqlhelper/src/test/java/io/github/heykb/sqlhelper/test/DynamicDatasourceTest.java:
--------------------------------------------------------------------------------
1 | package io.github.heykb.sqlhelper.test;
2 |
3 | import io.github.heykb.sqlhelper.dynamicdatasource.*;
4 | import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
5 | import org.apache.ibatis.session.SqlSession;
6 | import org.apache.ibatis.session.SqlSessionFactory;
7 | import org.junit.jupiter.api.BeforeAll;
8 | import org.junit.jupiter.api.DisplayName;
9 | import org.junit.jupiter.api.Test;
10 |
11 | import javax.sql.DataSource;
12 | import java.util.List;
13 | import java.util.Map;
14 |
15 | @DisplayName("动态数据源")
16 | public class DynamicDatasourceTest {
17 |
18 | static SqlSessionFactory sqlSessionFactory;
19 | static DefaultSqlHelperDsManager sqlHelperDsManager;
20 | @BeforeAll
21 | static void setUp() throws Exception {
22 | DataSource dataSource = new UnpooledDataSource("org.hsqldb.jdbcDriver","jdbc:hsqldb:mem:automapping","sa",null);
23 | SqlHelperDsManager sqlHelperDsManager = new DefaultSqlHelperDsManager(dataSource);
24 | SqlHelperDynamicDataSourceProxy dataSourceProxy = new SqlHelperDynamicDataSourceProxy(sqlHelperDsManager);
25 | sqlHelperDsManager.put("ds2", LogicDsMeta.builder()
26 | .expectedSubspaceType(ConnectionSubspaceTypeEnum.NOT_SUPPORT)
27 | .datasourceId("ds2").createFunc(()->{
28 | return new UnpooledDataSource("org.hsqldb.jdbcDriver","jdbc:hsqldb:mem:automapping2","sa",null);
29 | }).build());
30 | sqlSessionFactory = BaseUtils.generateSqlSessionFactory(dataSourceProxy, "io/github/heykb/sqlhelper/test/baseTest.sql",
31 | BaseTest.TestMapper.class, null,null);
32 | BaseUtils.runScript(sqlHelperDsManager.getById("ds2"),"io/github/heykb/sqlhelper/test/baseTest.sql");
33 | }
34 |
35 | @Test
36 | void cacheTest() throws Exception {
37 | try(SqlSession sqlSession =sqlSessionFactory.openSession()){
38 | BaseTest.TestMapper testMapper = sqlSession.getMapper(BaseTest.TestMapper.class);
39 | List