无废话mybatis九(简单粗暴的读写分离)
2018-11-11 本文已影响0人
毛仑上保罗先生
首先我们需要添加多个数据源,找到mybatis的配置类mybatis-config.xml
<?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>
<!-- 设置日志输出为LOG4J -->
<setting name="logImpl" value="LOG4J" />
<!--将以下画线方式命名的数据库列映射到 Java 对象的驼峰式命名属性中 -->
<setting name="mapUnderscoreToCamelCase" value="true" />
<setting name="cacheEnabled" value="false" />
</settings>
<!--简化类命名空间 -->
<typeAliases>
<package name="tk.mybatis.simple.model" />
</typeAliases>
<environments default="reader">
<environment id="reader">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?useSSL=false" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="poolPingEnabled" value="true" />
<property name="poolPingQuery" value="select 1" />
<property name="poolPingConnectionsNotUsedFor"
value="3600000" />
</dataSource>
</environment>
<environment id="write">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?useSSL=false" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="poolPingEnabled" value="true" />
<property name="poolPingQuery" value="select 1" />
<property name="poolPingConnectionsNotUsedFor"
value="3600000" />
</dataSource>
</environment>
</environments>
<mappers>
<!--常规做法 -->
<!--<mapper resource="tk/mybatis/simple/mapper/CountryMapper.xml" /> -->
<!--<mapper resource="tk/mybatis/simple/mapper/PrivilegeMapper.xml" /> -->
<!--<mapper resource="tk/mybatis/simple/mapper/RoleMapper.xml" /> -->
<!--<mapper resource="tk/mybatis/simple/mapper/RolePrivilegeMapper.xml"/> -->
<!--<mapper resource="tk/mybatis/simple/mapper/UserMapper.xml" /> -->
<!--<mapper resource="tk/mybatis/simple/mapper/UserRoleMapper.xml" /> -->
<!--第二种做法 -->
<package name="tk.mybatis.simple.mapper" />
</mappers>
</configuration>
一个是 <environment id="reader">, 一个是 <environment id="write"> 就是一个是读库一个是写库
然后简单的改造下BaseMapperTest.java
package simple;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.BeforeClass;
import java.io.IOException;
import java.io.Reader;
public class BaseMapperTest {
private static SqlSessionFactory sqlSessionFactory;
private static SqlSessionFactory wirteSqlSessionFactory;
private static String mybatisfilename="mybatis-config.xml";
@BeforeClass
public static void init() {
try {
Reader readerinputStream = Resources.getResourceAsReader(mybatisfilename);
Reader wireteinputStream = Resources.getResourceAsReader(mybatisfilename);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
sqlSessionFactory = builder.build(readerinputStream, "reader");
wirteSqlSessionFactory = builder.build(wireteinputStream, "write");
readerinputStream.close();
wireteinputStream.close();
} catch (IOException ignore) {
ignore.printStackTrace();
}
}
public SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
public SqlSession getWriteSqlSession() {
return wirteSqlSessionFactory.openSession();
}
}
为什么要两个Reader , 因为在SqlSessionFactoryBuilder 的build方法之后直接吧Reader 给close了, 所以我们这里需要new两个, 当然有别的好方法麻烦提供,谢谢
UserMapperTest.java测试类, 里面找到testInsert3() 代码稍微改动下
public class UserMapperTest extends BaseMapperTest{
.
.
.
@Test
public void testInsert3() {
SqlSession sqlSession = getWriteSqlSession();
try {
SysUser sysUser = new SysUser();
sysUser.setUserName("obj");
sysUser.setUserInfo("ifno");
sysUser.setUserEmail("obj@sina.com");
sysUser.setUserPassword("123456");
sysUser.setCreateTime(new Date());
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int result = userMapper.insert3(sysUser);
System.out.println(result);
System.out.println("返回数据库生成的id" + sysUser.getId());
} finally {
sqlSession.commit();
sqlSession.close();
}
}
}
getWriteSqlSession();就是我们的写库, 然后执行发现成功
至于读库我们默认的就是读库,所以代码完全一样, 另外我个人来说一定会把mybatis的缓存去掉 在所有查询节点上加上flushCache="true"
<select id="selectByid" resultMap="userMap" flushCache="true">
select * from sys_user where id =#{id}
</select>
下一个章节为事务, 既然涉及到读写分离就应该多事务严格起来