Skip to content

Commit 2e06048

Browse files
committed
@多数据源 & 事物 代码完成@
1 parent 602d914 commit 2e06048

34 files changed

Lines changed: 1897 additions & 0 deletions

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.gradle/
2+
.idea/
3+
out/
4+
**/.DS_Store

build.gradle

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
group 'cn.viewt'
2+
version '1.0-SNAPSHOT'
3+
4+
apply plugin: 'java'
5+
6+
sourceCompatibility = 1.8
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
testCompile group: 'junit', name: 'junit', version: '4.12'
14+
compile 'org.springframework.boot:spring-boot-starter-web:2.1.3.RELEASE'
15+
compile 'com.alibaba:druid:1.1.0'
16+
compile 'org.yaml:snakeyaml:1.24'
17+
compile 'org.projectlombok:lombok:1.16.8'
18+
compile 'org.mybatis:mybatis:3.5.1'
19+
compile 'org.mybatis:mybatis-spring:2.0.0'
20+
compile 'org.springframework:spring-jdbc:5.1.5.RELEASE'
21+
compile 'mysql:mysql-connector-java:6.0.5'
22+
}

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rootProject.name = 'java-practice'
2+
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package cn.iwuliao.cons;
2+
3+
public class Cons {
4+
5+
6+
public interface Http {
7+
/**
8+
* 连接超时时间
9+
*/
10+
int CONNECT_TIME_OUT = 5_000;
11+
/**
12+
* 读取超时时间
13+
*/
14+
int READ_TIME_OUT = 5_000;
15+
16+
/**
17+
* 连接超时时间
18+
*/
19+
int CONNECT_LONG_TIME_OUT_LONG = 15_000;
20+
/**
21+
* 读取超时时间
22+
*/
23+
int READ_LONG_TIME_OUT = 15_000;
24+
25+
/**
26+
* 连接超时时间
27+
*/
28+
int CONNECT_LONG_TIME_OUT_LONG_60 = 60_000;
29+
/**
30+
* 读取超时时间
31+
*/
32+
int READ_LONG_TIME_OUT_60 = 60_000;
33+
String POST = "POST";
34+
35+
String CONTENT_TYPE = "application/json";
36+
}
37+
38+
public interface B_PHONE {
39+
/**
40+
* 10000000000
41+
*/
42+
String ONE_ZERO = "10000000000";
43+
/**
44+
* 11111111111
45+
*/
46+
String ONE_ONE = "11111111111";
47+
}
48+
49+
50+
public interface DataType {
51+
/**
52+
* Map 默认初始化大
53+
*/
54+
int HASH_INITIALCAPACITY = 16;
55+
}
56+
57+
public interface Wold {
58+
}
59+
60+
/**
61+
* 业务字符
62+
*/
63+
public interface Service {
64+
65+
66+
67+
68+
/**
69+
* 短信规则
70+
*/
71+
String PHONE_REGX = "1[3456789][0-9]{9}";
72+
73+
74+
75+
76+
}
77+
78+
/**
79+
* 符号专用类
80+
*/
81+
public interface Chars {
82+
/**
83+
* "" 空字符
84+
*/
85+
String EMPTY = "";
86+
String QUESTION = "?";
87+
String NULL = "null";
88+
/**
89+
* , 符号
90+
*/
91+
String COMMA = ",";
92+
93+
/*** | 符号 */
94+
String VERTICAL = "|";
95+
/**
96+
* " " 空格字符
97+
*/
98+
String BLANK = " ";
99+
/**
100+
* ; 分号
101+
*/
102+
String SEMICOLON = ";";
103+
/**
104+
* :
105+
* 冒号
106+
*/
107+
String COLON = ":";
108+
/**
109+
* " 引号
110+
*/
111+
String QUOTES = "\"";
112+
String NEW_LINE = "\n";
113+
String LEFT_BRACKET = "(";
114+
String LEFT_MID_BRACKET = "[";
115+
String LEFT_BIG_BRACKET = "{";
116+
String LEFT_ANGLE_BRACKET = "<";
117+
118+
String RIGHT_BRACKET = ")";
119+
String RIGHT_MID_BRACKET_RIGHT = "]";
120+
String RIGHT_BIG_BRACKET_RIGHT = "}";
121+
String RIGHT_ANGLE_BRACKET_RIGHT = ">";
122+
123+
String BIG_BRACKET = LEFT_BIG_BRACKET + RIGHT_BIG_BRACKET_RIGHT;
124+
String MID_BRACKET = LEFT_MID_BRACKET + RIGHT_MID_BRACKET_RIGHT;
125+
String BRACKET = LEFT_BRACKET + RIGHT_BRACKET;
126+
127+
String EQUA = "=";
128+
String EXCLAMATION_MARK = "!";
129+
String AT = "@";
130+
String UNDERLINE = "_";
131+
String SHORTLINE = "-";
132+
// String a = "#";
133+
// String a = "$";
134+
// String a = "%";
135+
// String a = "^";
136+
String AND = "&";
137+
// String a = "*";
138+
139+
140+
}
141+
142+
143+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.iwuliao.ds;
2+
3+
import cn.iwuliao.ds.core.DsScannerConfigurer;
4+
import org.mybatis.spring.SqlSessionFactoryBean;
5+
import org.mybatis.spring.mapper.MapperScannerConfigurer;
6+
import org.springframework.boot.SpringApplication;
7+
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
9+
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
10+
import org.springframework.context.ConfigurableApplicationContext;
11+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
12+
import org.springframework.transaction.annotation.EnableTransactionManagement;
13+
14+
import java.util.Map;
15+
16+
/**
17+
* @author tangyu
18+
* @since 2019-04-22 20:20
19+
*/
20+
//@Slf4j
21+
@EnableTransactionManagement
22+
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class})
23+
public class MultiDsConfApp {
24+
25+
public static void main(String[] args) {
26+
ConfigurableApplicationContext run = SpringApplication.run(MultiDsConfApp.class, args);
27+
Map<String, SqlSessionFactoryBean> beansOfType = run.getBeansOfType(SqlSessionFactoryBean.class);
28+
Map<String, MapperScannerConfigurer> beansOfType1 = run.getBeansOfType(MapperScannerConfigurer.class);
29+
Map<String, DataSourceTransactionManager> beansOfType2 = run.getBeansOfType(DataSourceTransactionManager.class);
30+
DataSourceTransactionManager bean = run.getBean("dba" + DsScannerConfigurer.TRANSACTIONMANAGER, DataSourceTransactionManager.class);
31+
System.out.println(beansOfType);
32+
System.out.println(beansOfType1);
33+
System.out.println(beansOfType2);
34+
System.out.println(bean);
35+
}
36+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cn.iwuliao.ds.controller;
2+
3+
import cn.iwuliao.ds.service.HeloService;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
/**
9+
* @author tangyu
10+
* @since 2019-04-22 20:30
11+
*/
12+
@RestController
13+
@RequestMapping("/hi")
14+
public class HeloController {
15+
16+
17+
private HeloService heloService;
18+
19+
public HeloController(HeloService heloService) {
20+
this.heloService = heloService;
21+
}
22+
23+
24+
@RequestMapping("/hi")
25+
public String hi() {
26+
27+
Integer hia = heloService.hia();
28+
Integer hib = heloService.hib();
29+
30+
return "hia:" + hia + "<-->hib:" + hib;
31+
}
32+
33+
34+
@RequestMapping("/tx/{commit}")
35+
public String tx(@PathVariable("commit") String commit) {
36+
37+
if ("commit".equals(commit)) {
38+
Integer commit1 = heloService.commit();
39+
System.out.println("it has commited ? " + (commit1 > 0));
40+
} else {
41+
Integer commit1 = 0;
42+
try {
43+
commit1 = heloService.rollback();
44+
} finally {
45+
System.out.println("it is rollback ? " + (commit1 > 0));
46+
}
47+
}
48+
return "i am tx ";
49+
}
50+
51+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package cn.iwuliao.ds.core;
2+
3+
4+
import org.apache.ibatis.session.SqlSessionFactory;
5+
import org.mybatis.spring.SqlSessionFactoryBean;
6+
import org.mybatis.spring.mapper.MapperScannerConfigurer;
7+
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
8+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
9+
import org.springframework.transaction.PlatformTransactionManager;
10+
11+
import javax.sql.DataSource;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.Objects;
15+
import java.util.Properties;
16+
17+
/**
18+
* @author tangyu
19+
* @since 2019-04-06 03:13
20+
*/
21+
public class DatasourceUtil {
22+
23+
/**
24+
* 获取 SqlsessionFactory 对象
25+
*
26+
* @param dataSource
27+
* @param location
28+
* @param handlersPackage
29+
* @return
30+
* @throws Exception
31+
*/
32+
public static SqlSessionFactory getSqlSessionFactory(DataSource dataSource, String location, String handlersPackage) throws Exception {
33+
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
34+
factory.setDataSource(dataSource);
35+
factory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(location));
36+
if (Objects.nonNull(handlersPackage)) {
37+
factory.setTypeHandlersPackage(handlersPackage);
38+
}
39+
return factory.getObject();
40+
}
41+
42+
/**
43+
* 将Mapper 文件 注入到IOC 容器
44+
*
45+
* @param baseRestSlaveSqlSessionFactory
46+
* @param basePackage
47+
* @return
48+
*/
49+
public static MapperScannerConfigurer getMapperScannerConfigurer(String baseRestSlaveSqlSessionFactory, String basePackage) {
50+
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
51+
mapperScannerConfigurer.setBasePackage(basePackage);
52+
//使用实体类名/属性名作为表名/字段名
53+
return mapperScannerConfigurer;
54+
}
55+
56+
57+
/**
58+
* 动态获取数据源
59+
*
60+
* @param master
61+
* @param slave
62+
* @return
63+
*/
64+
public static MasterSlaveRoutingDataSource getRoutingDataSource(DataSource master, DataSource slave) {
65+
66+
MasterSlaveRoutingDataSource proxy = new MasterSlaveRoutingDataSource();
67+
68+
Map<Object, Object> targetDataResources = new HashMap<>();
69+
targetDataResources.put(DbContextHolder.DbType.MASTER, master);
70+
targetDataResources.put(DbContextHolder.DbType.SLAVE, slave);
71+
72+
proxy.setDefaultTargetDataSource(master);
73+
proxy.setTargetDataSources(targetDataResources);
74+
75+
proxy.afterPropertiesSet();
76+
return proxy;
77+
}
78+
79+
/**
80+
* 事物管理
81+
*
82+
* @param nowwarningDataSource
83+
* @return
84+
*/
85+
public static PlatformTransactionManager getransactionManager(DataSource nowwarningDataSource) {
86+
87+
return new DataSourceTransactionManager(nowwarningDataSource);
88+
}
89+
90+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.iwuliao.ds.core;
2+
3+
import com.alibaba.druid.pool.DruidDataSource;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import lombok.ToString;
7+
8+
import java.util.Properties;
9+
10+
@Setter
11+
@Getter
12+
@ToString
13+
public class Db {
14+
15+
// private DruidDataSource master;
16+
// private DruidDataSource slave;
17+
// private DruidDataSource slave1;
18+
//
19+
private Properties master;
20+
private Properties slave;
21+
private Properties slave1;
22+
23+
private String mapperLocations;
24+
private String typeHandlersPackage;
25+
private String mapperScannerPackage;
26+
}

0 commit comments

Comments
 (0)