主从库配置好后,实现读写分离操作
逻辑过程:在mybatis中设置拦截器,每当有涉及到mybatis.config.xml即增删改查操作时,都会被拦截器拦截下来,进入到DynamicDataSourceInterceptor中。在spring中也给DynamicDataSource注入属性。
1、DynamicDataSource类
DynamicDataSource顾名思义动态数据源。继承自AbstractRoutingDataSource。
AbstractRoutingDataSource 根据用户定义的规则选择当前的数据源,这样我们可以在执行查询之前,设置使用的数据源。实现可动态路由的数据源,在每次数据库查询操作前执行。它的抽象方法 determineCurrentLookupKey() 决定使用哪个数据源。
package com.imooc.o2o.dao.split;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
// TODO Auto-generated method stub
return DynamicDataSourceHolder.getDbType();
}
}
2、 DynamicDataSourceHolder类
ThreadLocal,可以称作线程本地变量,也可以称作线程本地存储,ThreadLocal为变量在每个线程中创建了一个副本,那么每个线程可以访问自己的线程。
package com.imooc.o2o.dao.split;
import org.slf4j.LoggerFactory;
public class DynamicDataSourceHolder {
private static org.slf4j.Logger logger= LoggerFactory.getLogger(DynamicDataSourceHolder.class);
private static ThreadLocal<String> contextHolder=new ThreadLocal<String>();
public static final String DB_MASTER="master";
public static final String DB_SLAVE="slave";
public static String getDbType(){
String db=contextHolder.get();
if(db==null){
db=DB_MASTER;
}
return db;
}
/**
* 设置线程的dbType
* @param str
*/
public static void setDbType(String str){
logger.debug("所使用的数据源为:"+str);
contextHolder.set(str);
}
/**
*清理连接类型
*/
public void clearDbType(){
contextHolder.remove();
}
}
3、DynamicDataSourceInterceptor类(动态数据源拦截器)
补充:关于过滤器和拦截器看到一张非常好的图,从发出request,到response,步骤1~14即过程
其中View Resolver:视图解析器
关于< url-pattern >个人理解如每次前端加载页面时如验证码会向DispatcheServlet发送信息如/或者/Kaptcha,当路径匹配时就会使用该servlet。如/xxx/:匹配以/xxx开头的路径,请求中必须包含xxx。使用/配置路径,直接访问到 jsp,不经springDispatcherServlet。/:配置/*路径,不能访问到多视图的jsp
< image id=“captcha_img” alt=“点击更换” title=“点击更换”
οnclick=“changeVerifyCode(this)” src="…/Kaptcha" />
这里我突然明白为什么项目的包为什么一般都以com打头,上面图片src的地址在com.google.code.kaptcha.之下,所以通过相对路径可以访问到
…/Kaptcha表示返回上两级路径直到WEB-INF,就可以访问web.xml中名为Kaptcha的servlet
package com.imooc.o2o.dao.split;
import java.util.Locale;
import java.util.Properties;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.keygen.SelectKeyGenerator;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.support.TransactionSynchronizationManager;
/**
* select则变为读操作
* query则变为写操作
*/
public class DynamicDataSourceInterceptor implements Interceptor {
private static final String REGEX=".*insert\\u0020.*|.*delete\\u0020.*|.*update\\u0020.*";
private static org.slf4j.Logger logger= LoggerFactory.getLogger(DynamicDataSourceInterceptor.class);
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object[] objects=invocation.getArgs();
MappedStatement ms=(MappedStatement)objects[0];
String lookupkey=DynamicDataSourceHolder.DB_MASTER;
//判断当前是否是事务
boolean synchronizationActice=TransactionSynchronizationManager.isActualTransactionActive();
if(synchronizationActice!=true){
//读方法
if(ms.getSqlCommandType().equals(SqlCommandType.SELECT)){
//如果selectkey为自增id查询主键(select last_insert_id)使用主库
if(ms.getId().contains(SelectKeyGenerator.SELECT_KEY_SUFFIX)){
lookupkey=DynamicDataSourceHolder.DB_MASTER;
}else{
BoundSql boundSql=ms.getSqlSource().getBoundSql(objects[1]);
String sql=boundSql.getSql().toLowerCase(Locale.CHINA).replaceAll("[\\t\\n\\r]", "");
if(sql.matches(REGEX)){
lookupkey=DynamicDataSourceHolder.DB_MASTER;
}else{
lookupkey=DynamicDataSourceHolder.DB_SLAVE;
}
}
}
}else{
//非事务管理的用主库
lookupkey=DynamicDataSourceHolder.DB_MASTER;
}
logger.debug("设置方法[{}] use [{}] Strategy,SqlCommanType [{}]..",
ms.getId(),lookupkey,ms.getSqlCommandType().name());
DynamicDataSourceHolder.setDbType(lookupkey);
return invocation.proceed();
}
/*
* Executor用来支持增删改查操作的
* (non-Javadoc)
* @see org.apache.ibatis.plugin.Interceptor#plugin(java.lang.Object)
*/
@Override
public Object plugin(Object target) {
if(target instanceof Executor){
return Plugin.wrap(target, this);
}else{
return target;
}
}
@Override
public void setProperties(Properties arg0) {
// TODO Auto-generated method stub
}
}
第四
mybatis.config改动
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置全局属性 -->
<settings>
<!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
<setting name="useGeneratedKeys" value="true" />
<!-- 使用列标签替换列别名 默认:true 列标签就是中的字段名-->
<setting name="useColumnLabel" value="true" />
<!-- 开启驼峰命名转换:Table{create_time} -> Entity{createTime} -->
<setting name="mapUnderscoreToCamelCase" value="true" />
<!-- 打印查询语句 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<plugins>
<plugin interceptor="com.imooc.o2o.dao.split.DynamicDataSourceInterceptor">
</plugin>
</plugins>
</configuration>
第五
spring-dao-xml改动
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置整合mybatis过程 -->
<!-- 1.配置数据库相关参数properties的属性:${url} -->
<!-- 不加密实现时<context:property-placeholder location="classpath:jdbc.properties"
/> -->
<!-- 加密实现 -->
<bean class="com.imooc.o2o.util.EncryptPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:redis.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8" />
</bean>
<!-- 2.数据库连接池 -->
<bean id="abstractdataSource" abstract="true"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false" />
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000" />
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2" />
</bean>
<bean id="master" parent="abstractdataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.master.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="slave" parent="abstractdataSource">
<!-- 配置连接池属性 -->
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.slave.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置动态数据源,这儿targetDataSources就是路由数据源所对应的名称 ,key对应DynamicDataSourceHolder中的值-->
<bean id="dynamicDataSource" class="com.imooc.o2o.dao.split.DynamicDataSource">
<property name="targetDataSources">
<map>
<entry value-ref="master" key="master"></entry>
<entry value-ref="slave" key="slave"></entry>
</map>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataourceProxy">
<property name="targetDataSource">
<ref bean="dynamicDataSource"/>
</property>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis.config.xml" />
<!-- 扫描entity包 使用别名 -->
<property name="typeAliasesPackage" value="com.imooc.o2o.entity" />
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.imooc.o2o.dao" />
</bean>
</beans>
第六
jdbc.properties改动
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.master.url=jdbc:mysql://39.105.196.133:3306/o2o?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
jdbc.slave.url=jdbc:mysql://39.105.196.133:3307/o2o?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
jdbc.username=WnplV/ietfQ=
jdbc.password=QAHlVoUc49w\=