用户工具

站点工具


分享:技术:数据源:spring_mybatis_多数据源配置_1

Spring+Mybatis 多数据源配置_1

方法描述

采用spring配置文件直接配置多个数据源,指定某个路径下的mapper文件使用哪个数据源,分别配置事务管理

优缺点

  1. 不支持分布式事务,一个事务中不能操作多个数据源
  2. 配置简单,但是不灵活
  3. 适合两个数据库没有相关性的情况
  4. 不适合master-slave性的多数据源的配置,需要根据业务类型进行细致配置,比如增删改在master上,不允许有主从同步时间延时的查询在master上,耗时查询在slave上等等
  5. vo,dao,mapper,service都需要两套

主要代码

application-context.xml

application-context.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:security="http://www.springframework.org/schema/security"
	xsi:schemaLocation="
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/security 
		http://www.springframework.org/schema/security/spring-security-3.2.xsd
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/data/jpa 
		http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
	<!-- Activates annotation-based bean configuration -->
	<context:annotation-config />
 
	<!-- Scans for application @Components to deploy -->
	<context:component-scan base-package="com.gxx.record" />
 
	<!-- 数据库配置文件位置 -->
	<context:property-placeholder location="classpath:/jdbc.properties,classpath:/redis.properties,classpath:/memcached.properties,classpath:/mongodb.properties,classpath:/activemq.properties" />
 
	<!-- 主库配置 -->
	<!-- 使用JDBC事务 -->
	<bean id="master_transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="master_dataSource" />
	</bean>
 
	<!-- AOP配置事物 -->
	<tx:advice id="master_transactionAdvice" transaction-manager="master_transactionManager">
		<tx:attributes>
			<tx:method name="query*" read-only="true" propagation="REQUIRED" />
			<tx:method name="delete*"  propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>
 
	<!-- 配置AOP切面 -->
	<aop:config>
        <aop:pointcut id="master_transactionPointcut" expression="execution(* com.gxx.record.service.master.impl.*.*(..))"/>
        <aop:advisor pointcut-ref="master_transactionPointcut" advice-ref="master_transactionAdvice"/>
    </aop:config>
 
 
	<!-- 使用annotation注解方式配置事务 -->
	<tx:annotation-driven transaction-manager="master_transactionManager" />
 
	<bean id="master_sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="master_dataSource" />
		<property name="configLocation" value="classpath:mybatis.xml"></property>
		<property name="mapperLocations" value="classpath:com/gxx/record/base/master/mapping/*.xml"></property>
	</bean>
 
	<!-- 配置SQLSession模板 -->
	<bean id="master_sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="master_sqlSessionFactory" />
	</bean>
 
	<!--扫描basePackage下所有以@Repository注解的接口  -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="master_sqlSessionFactory" />
        <property name="basePackage" value="com.gxx.record.base.master"/>
        <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
    </bean>
 
    <!-- 从库配置 -->
	<!-- 使用JDBC事务 -->
	<bean id="slave_transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="slave_dataSource" />
	</bean>
 
	<!-- AOP配置事物 -->
	<tx:advice id="slave_transactionAdvice" transaction-manager="slave_transactionManager">
		<tx:attributes>
			<tx:method name="query*" read-only="true" propagation="REQUIRED" />
			<tx:method name="delete*"  propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>
 
	<!-- 配置AOP切面 -->
	<aop:config>
        <aop:pointcut id="slave_transactionPointcut" expression="execution(* com.gxx.record.service.slave.impl.*.*(..))"/>
        <aop:advisor pointcut-ref="slave_transactionPointcut" advice-ref="slave_transactionAdvice"/>
    </aop:config>
 
 
	<!-- 使用annotation注解方式配置事务 -->
	<tx:annotation-driven transaction-manager="slave_transactionManager" />
 
	<bean id="slave_sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="slave_dataSource" />
		<property name="configLocation" value="classpath:mybatis.xml"></property>
		<property name="mapperLocations" value="classpath:com/gxx/record/base/slave/mapping/*.xml"></property>
	</bean>
 
	<!-- 配置SQLSession模板 -->
	<bean id="slave_sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="slave_sqlSessionFactory" />
	</bean>
 
	<!--扫描basePackage下所有以@Repository注解的接口  -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="slave_sqlSessionFactory" />
        <property name="basePackage" value="com.gxx.record.base.slave"/>
        <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
    </bean>
 
</beans>

application-local-profile.xml

application-local-profile.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:p="http://www.springframework.org/schema/p"
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 			
				http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
				http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
	<description>数据源配置,主要用于开发测试环境 </description>
	<!-- 配置dbcp数据源 -->
	<bean id="master_dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${master.local.jdbc.driverClassName}" />
		<property name="url" value="${master.local.jdbc.url}" />
		<property name="username" value="${master.local.jdbc.username}" />
		<property name="password" value="${master.local.jdbc.password}" />
	</bean>
	<bean id="slave_dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${slave.local.jdbc.driverClassName}" />
		<property name="url" value="${slave.local.jdbc.url}" />
		<property name="username" value="${slave.local.jdbc.username}" />
		<property name="password" value="${slave.local.jdbc.password}" />
	</bean>
 
</beans>

application-test-profile.xml

application-test-profile.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:jee="http://www.springframework.org/schema/jee"
		xmlns:context="http://www.springframework.org/schema/context"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 			
				http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
	<description>JNDI数据源,在生产环境使用应用服务器的数据库连接池</description>
	<!-- 配置jndi数据源 -->
	<jee:jndi-lookup id="master_dataSource" jndi-name="RECORD_DS_MASTER" />
	<jee:jndi-lookup id="slave_dataSource" jndi-name="RECORD_DS_SLAVE" />
</beans>

jdbc.properties

jdbc.properties
#本地环境主数据源
master.local.jdbc.driverClassName=com.mysql.jdbc.Driver
master.local.jdbc.url=jdbc:mysql://127.0.0.1:3306/record?useUnicode=true&characterEncoding=UTF-8&useOldAlias
master.local.jdbc.username=root
master.local.jdbc.password=root
 
#本地环境备数据源
slave.local.jdbc.driverClassName=com.mysql.jdbc.Driver
slave.local.jdbc.url=jdbc:mysql://127.0.0.1:3306/record_slave?useUnicode=true&characterEncoding=UTF-8&useOldAlias
slave.local.jdbc.username=root
slave.local.jdbc.password=root

MasterUserService.java

MasterUserService.java
package com.gxx.record.service.master;
 
import com.gxx.record.base.master.vo.User;
 
/**
 * <dl>
 *    <dt><b>Title:</b></dt>
 *    <dd>
 *    	用户服务接口
 *    </dd>
 *    <dt><b>Description:</b></dt>
 *    <dd>
 *    	<p>none
 *    </dd>
 * </dl>
 *
 * @author Administrator
 * @version 1.0, 2015年6月18日
 * @since record
 *
 */
public interface MasterUserService {
	/**
	 * 新增用户
	 * @param user
	 */
	public void doSaveUser(User user);
 
	/**
	 * 根据姓名查用户
	 * @param name
	 * @return
	 */
	public User getUserByName(String name);
}

MasterUserServiceImpl.java

MasterUserServiceImpl.java
package com.gxx.record.service.master.impl;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
 
import com.gxx.record.base.master.dao.UserMapper;
import com.gxx.record.base.master.vo.User;
import com.gxx.record.service.master.MasterUserService;
 
/**
 * <dl>
 *    <dt><b>Title:</b></dt>
 *    <dd>
 *    	用户服务实现类
 *    </dd>
 *    <dt><b>Description:</b></dt>
 *    <dd>
 *    	<p>none
 *    </dd>
 * </dl>
 *
 * @author Administrator
 * @version 1.0, 2015年6月18日
 * @since record
 *
 */
@Service("masterUserService")
public class MasterUserServiceImpl implements MasterUserService {
 
	@Autowired
	@Qualifier("masterUserMapper")
	private UserMapper userDao;
 
	/**
	 * 新增用户
	 * @param user
	 */
	public void doSaveUser(User user) {
		userDao.insert(user);
	}
 
	/**
	 * 根据姓名查用户
	 * @param name
	 * @return
	 */
	public User getUserByName(String name) {
		return userDao.getUserByName(name);
	}
}

UserMapper.java

UserMapper.java
package com.gxx.record.base.master.dao;
 
import org.springframework.stereotype.Repository;
 
import com.gxx.record.base.master.vo.User;
 
@Repository("masterUserMapper")
public interface UserMapper {
    int deleteByPrimaryKey(Integer id);
 
    int insert(User record);
 
    int insertSelective(User record);
 
    User selectByPrimaryKey(Integer id);
 
    int updateByPrimaryKeySelective(User record);
 
    int updateByPrimaryKey(User record);
 
    /**
     * 根据姓名查用户
     * @param name
     * @return
     */
    User getUserByName(String name);
}

User.java

User.java
package com.gxx.record.base.master.vo;
 
public class User {
    private Integer id;
 
    private String name;
 
    private String password;
 
    private String createDate;
 
    private String createTime;
 
    public Integer getId() {
        return id;
    }
 
    public void setId(Integer id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }
 
    public String getCreateDate() {
        return createDate;
    }
 
    public void setCreateDate(String createDate) {
        this.createDate = createDate == null ? null : createDate.trim();
    }
 
    public String getCreateTime() {
        return createTime;
    }
 
    public void setCreateTime(String createTime) {
        this.createTime = createTime == null ? null : createTime.trim();
    }
}

UserMapper.xml

UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.gxx.record.base.master.dao.UserMapper" >
  <resultMap id="BaseResultMap" type="com.gxx.record.base.master.vo.User" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="create_date" property="createDate" jdbcType="VARCHAR" />
    <result column="create_time" property="createTime" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, name, password, create_date, create_time
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.gxx.record.base.master.vo.User" >
    insert into user (id, name, password, 
      create_date, create_time)
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{createDate,jdbcType=VARCHAR}, #{createTime,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.gxx.record.base.master.vo.User" >
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="name != null" >
        name,
      </if>
      <if test="password != null" >
        password,
      </if>
      <if test="createDate != null" >
        create_date,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="createDate != null" >
        #{createDate,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.gxx.record.base.master.vo.User" >
    update user
    <set >
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="createDate != null" >
        create_date = #{createDate,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.gxx.record.base.master.vo.User" >
    update user
    set name = #{name,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      create_date = #{createDate,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
 
  <select id="getUserByName" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from user
    where name = #{name,jdbcType=VARCHAR}
  </select>
 
</mapper>

附上代码

分享/技术/数据源/spring_mybatis_多数据源配置_1.txt · 最后更改: 2016/01/12 14:06 由 gxx