Skip to content

Commit 97beff4

Browse files
committed
增加读写数据源
1 parent c970885 commit 97beff4

File tree

9 files changed

+197
-195
lines changed

9 files changed

+197
-195
lines changed

src/main/java/com/isea533/mybatis/controller/demo/DemoController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public List<Country> requestTest7(
5454
@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
5555
@RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize
5656
) {
57-
demoService.test();
5857
return demoService.selectPage(pageNum, pageSize);
5958
}
6059
}

src/main/java/com/isea533/mybatis/model/Country.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@
2424

2525
package com.isea533.mybatis.model;
2626

27-
import javax.persistence.Column;
28-
import javax.persistence.GeneratedValue;
29-
import javax.persistence.GenerationType;
30-
import javax.persistence.Id;
27+
import javax.persistence.*;
3128

29+
@Table(name = "country")
3230
public class Country {
3331
/**
3432
* 主键

src/main/java/com/isea533/mybatis/service/BaseService.java

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/main/java/com/isea533/mybatis/service/DemoService.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,39 @@
2424

2525
package com.isea533.mybatis.service;
2626

27+
import com.github.pagehelper.PageHelper;
28+
import com.isea533.mybatis.mapper.CountryMapper;
2729
import com.isea533.mybatis.model.Country;
28-
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.apache.ibatis.session.SqlSession;
2931
import org.springframework.stereotype.Service;
30-
import tk.mybatis.mapper.common.Mapper;
32+
33+
import javax.annotation.Resource;
34+
import java.util.List;
3135

3236
/**
3337
* @author liuzh
3438
*/
3539
@Service
36-
public class DemoService extends BaseService<Country> {
40+
public class DemoService {
41+
private CountryMapper writeMapper;
42+
private CountryMapper readMapper;
3743

38-
@Autowired
39-
private Mapper<Country> countryMapper;
44+
@Resource(name = "sqlSessionMaster")
45+
public void setWriteMapper(SqlSession sqlSession) {
46+
this.writeMapper = sqlSession.getMapper(CountryMapper.class);
47+
}
4048

41-
public int save(Country country) {
42-
if (country == null) {
43-
throw new NullPointerException("保存的对象不能为空!");
44-
}
45-
if (country.getCountrycode() == null || country.getCountrycode().equals("")) {
46-
throw new RuntimeException("国家代码不能为空!");
47-
}
48-
if (country.getCountryname() == null || country.getCountryname().equals("")) {
49-
throw new RuntimeException("国家名称不能为空!");
50-
}
51-
return super.save(country);
49+
@Resource(name = "sqlSessionSlave")
50+
public void setReadMapper(SqlSession sqlSession) {
51+
this.readMapper = sqlSession.getMapper(CountryMapper.class);
5252
}
5353

54-
public void test() {
55-
int result = countryMapper.selectCount(null);
56-
System.out.println(result);
54+
public int save(Country country) {
55+
return writeMapper.insert(country);
5756
}
5857

58+
public List<Country> selectPage(int pageNum, int pageSize) {
59+
PageHelper.startPage(pageNum, pageSize);
60+
return readMapper.select(null);
61+
}
5962
}

src/main/resources/applicationContext.xml

Lines changed: 11 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -23,102 +23,21 @@
2323
~ THE SOFTWARE.
2424
-->
2525

26-
<beans xmlns="http://www.springframework.org/schema/beans"
27-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28-
xmlns:tx="http://www.springframework.org/schema/tx"
29-
xmlns:aop="http://www.springframework.org/schema/aop"
26+
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3027
xmlns:context="http://www.springframework.org/schema/context"
28+
xmlns:aop="http://www.springframework.org/schema/aop"
29+
xmlns="http://www.springframework.org/schema/beans"
3130
xsi:schemaLocation="http://www.springframework.org/schema/beans
3231
http://www.springframework.org/schema/beans/spring-beans.xsd
33-
http://www.springframework.org/schema/tx
34-
http://www.springframework.org/schema/tx/spring-tx.xsd
35-
http://www.springframework.org/schema/aop
36-
http://www.springframework.org/schema/aop/spring-aop.xsd
3732
http://www.springframework.org/schema/context
38-
http://www.springframework.org/schema/context/spring-context.xsd">
39-
40-
<context:component-scan base-package="com.isea533.mybatis.service"/>
41-
42-
<context:property-placeholder location="classpath:config.properties"/>
43-
44-
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
45-
<property name="driverClassName" value="${jdbc.driverClass}"/>
46-
<property name="url" value="${jdbc.url}" />
47-
<property name="username" value="${jdbc.user}"/>
48-
<property name="password" value="${jdbc.password}" />
49-
50-
<property name="filters" value="stat" />
51-
52-
<property name="maxActive" value="20"/>
53-
<property name="initialSize" value="1"/>
54-
<property name="maxWait" value="60000"/>
55-
<property name="minIdle" value="1"/>
56-
57-
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
58-
<property name="minEvictableIdleTimeMillis" value="300000"/>
59-
60-
<property name="validationQuery" value="SELECT 'x'"/>
61-
<property name="testWhileIdle" value="true"/>
62-
<property name="testOnBorrow" value="false"/>
63-
<property name="testOnReturn" value="false"/>
64-
</bean>
65-
66-
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
67-
<property name="dataSource" ref="dataSource"/>
68-
<property name="mapperLocations">
69-
<array>
70-
<value>classpath:mapper/*.xml</value>
71-
</array>
72-
</property>
73-
<property name="typeAliasesPackage" value="com.isea533.mybatis.model"/>
74-
<property name="plugins">
75-
<array>
76-
<bean class="com.github.pagehelper.PageHelper">
77-
<property name="properties">
78-
<value>
79-
dialect=mysql
80-
reasonable=true
81-
</value>
82-
</property>
83-
</bean>
84-
<bean class="tk.mybatis.mapper.mapperhelper.MapperInterceptor">
85-
<property name="properties">
86-
<value>
87-
mappers=tk.mybatis.mapper.common.Mapper
88-
IDENTITY=MYSQL
89-
notEmpty=true
90-
</value>
91-
</property>
92-
</bean>
93-
</array>
94-
</property>
95-
</bean>
96-
97-
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
98-
<property name="basePackage" value="com.isea533.mybatis.mapper"/>
99-
</bean>
100-
101-
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
102-
<constructor-arg index="0" ref="sqlSessionFactory"/>
103-
</bean>
104-
105-
<aop:aspectj-autoproxy/>
106-
107-
<aop:config>
108-
<aop:pointcut id="appService" expression="execution(* com.isea533.mybatis.service..*Service*.*(..))"/>
109-
<aop:advisor advice-ref="txAdvice" pointcut-ref="appService"/>
110-
</aop:config>
33+
http://www.springframework.org/schema/context/spring-context.xsd
34+
http://www.springframework.org/schema/aop
35+
http://www.springframework.org/schema/aop/spring-aop.xsd">
11136

112-
<tx:advice id="txAdvice" transaction-manager="transactionManager">
113-
<tx:attributes>
114-
<tx:method name="select*" read-only="true"/>
115-
<tx:method name="find*" read-only="true"/>
116-
<tx:method name="get*" read-only="true"/>
117-
<tx:method name="*"/>
118-
</tx:attributes>
119-
</tx:advice>
37+
<context:component-scan base-package="com.isea533.mybatis.service"/>
38+
<context:property-placeholder location="classpath:config.properties"/>
39+
<aop:aspectj-autoproxy/>
12040

121-
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
122-
<property name="dataSource" ref="dataSource"/>
123-
</bean>
41+
<import resource="spring-datasource-master.xml"/>
42+
<import resource="spring-datasource-slave.xml"/>
12443
</beans>

src/main/resources/config.properties

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@
2222
# THE SOFTWARE.
2323
#
2424

25-
# \u6570\u636E\u5E93\u914D\u7F6E
26-
jdbc.driverClass = com.mysql.jdbc.Driver
27-
jdbc.url = jdbc:mysql://192.168.1.200:3306/test
28-
jdbc.user = root
29-
jdbc.password =
25+
# \u6570\u636E\u5E93\u914D\u7F6E - Master
26+
master.jdbc.driverClass = com.mysql.jdbc.Driver
27+
master.jdbc.url = jdbc:mysql://192.168.1.11:3306/test
28+
master.jdbc.user = root
29+
master.jdbc.password = jj
30+
31+
# - Slave
32+
slave.jdbc.driverClass = com.mysql.jdbc.Driver
33+
slave.jdbc.url = jdbc:mysql://192.168.1.22:3306/test
34+
slave.jdbc.user = root
35+
slave.jdbc.password = jj
3036

3137
#c3p0
3238
jdbc.maxPoolSize=50
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns:tx="http://www.springframework.org/schema/tx"
4+
xmlns:aop="http://www.springframework.org/schema/aop" xmlns="http://www.springframework.org/schema/beans"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans.xsd
7+
http://www.springframework.org/schema/tx
8+
http://www.springframework.org/schema/tx/spring-tx.xsd
9+
http://www.springframework.org/schema/aop
10+
http://www.springframework.org/schema/aop/spring-aop.xsd">
11+
12+
<bean id="dataSourceMaster" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
13+
destroy-method="close">
14+
<property name="driverClassName" value="${master.jdbc.driverClass}"/>
15+
<property name="url" value="${master.jdbc.url}"/>
16+
<property name="username" value="${master.jdbc.user}"/>
17+
<property name="password" value="${master.jdbc.password}"/>
18+
19+
<property name="filters" value="stat"/>
20+
21+
<property name="maxActive" value="20"/>
22+
<property name="initialSize" value="1"/>
23+
<property name="maxWait" value="60000"/>
24+
<property name="minIdle" value="1"/>
25+
26+
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
27+
<property name="minEvictableIdleTimeMillis" value="300000"/>
28+
29+
<property name="validationQuery" value="SELECT 'x'"/>
30+
<property name="testWhileIdle" value="true"/>
31+
<property name="testOnBorrow" value="false"/>
32+
<property name="testOnReturn" value="false"/>
33+
</bean>
34+
35+
<bean id="sqlSessionFactoryMaster" class="org.mybatis.spring.SqlSessionFactoryBean">
36+
<property name="dataSource" ref="dataSourceMaster"/>
37+
<property name="mapperLocations">
38+
<array>
39+
<value>classpath:mapper/*.xml</value>
40+
</array>
41+
</property>
42+
</bean>
43+
44+
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
45+
<property name="basePackage" value="com.isea533.mybatis.mapper"/>
46+
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryMaster"/>
47+
</bean>
48+
49+
<bean id="sqlSessionMaster" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
50+
<constructor-arg index="0" ref="sqlSessionFactoryMaster"/>
51+
</bean>
52+
53+
<aop:config>
54+
<aop:pointcut id="appService" expression="execution(* com.isea533.mybatis.service..*Service*.*(..))"/>
55+
<aop:advisor advice-ref="txAdviceMaster" pointcut-ref="appService"/>
56+
</aop:config>
57+
58+
<tx:advice id="txAdviceMaster" transaction-manager="transactionManagerMaster">
59+
<tx:attributes>
60+
<tx:method name="select*" read-only="true"/>
61+
<tx:method name="find*" read-only="true"/>
62+
<tx:method name="get*" read-only="true"/>
63+
<tx:method name="*"/>
64+
</tx:attributes>
65+
</tx:advice>
66+
67+
<bean id="transactionManagerMaster" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
68+
<property name="dataSource" ref="dataSourceMaster"/>
69+
</bean>
70+
</beans>

0 commit comments

Comments
 (0)