1. 程式人生 > >xml型多資料來源mybatis.md

xml型多資料來源mybatis.md


  1. 例項需求:輸出庫test1中表user的記錄和庫test2中表user的記錄總和

  2. 技術要點:

  • springboot整合mybatis配置

mybatis.config-locations=classpath:mybatis/mybatis-config.xml

spring.datasource.test1.driverClassName = com.mysql.jdbc.Driver
spring.datasource.test1.url = jdbc:mysql://192.168.32.128:3306/test1?useUnicode=true&characterEncoding=utf-8
spring.datasource.test1.username = root
spring.datasource.test1.password = root


spring.datasource.test2.driverClassName = com.mysql.jdbc.Driver
spring.datasource.test2.url = jdbc:mysql://192.168.32.128:3306/test2?useUnicode=true&characterEncoding=utf-8
spring.datasource.test2.username = root
spring.datasource.test2.password = root


  • 多資料來源實現

1)配置需要掃描的mapper介面
@MapperScan(basePackages = "com.lm.mapper.test1", sqlSessionTemplateRef = "test1SqlSessionTemplate")


2)定義datasource,以及已application.properties配置檔案資料來源對應的名稱
@Primary,當多個數據源時,需要定義一個主資料來源,即優先考慮被註解的物件注入
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test1")
@Primary


3)配置資料來源對應的myabtis中mapper.xml路徑
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"))

/**
 * test1資料來源
 * 
 * @author liangming.deng
 * @date 2017年6月30日
 *
 */
@Configuration
@MapperScan(basePackages = "com.lm.mapper.test1", sqlSessionTemplateRef = "test1SqlSessionTemplate")
public class DataSource1Config {

	@Bean(name = "test1DataSource")
	@ConfigurationProperties(prefix = "spring.datasource.test1")
	@Primary
	public DataSource testDataSource() {
		return DataSourceBuilder.create().build();
	}

	@Bean(name = "test1SqlSessionFactory")
	@Primary
	public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setMapperLocations(
				new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
		return bean.getObject();
	}

	@Bean(name = "test1TransactionManager")
	@Primary
	public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

	@Bean(name = "test1SqlSessionTemplate")
	@Primary
	public SqlSessionTemplate testSqlSessionTemplate(
			@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}

}
  • 不同的DataSource,不同的SqlSessionFactory和不同的DAO層,在業務邏輯層做整合。總結的架構圖如下:
    在這裡插入圖片描述
  1. 進行controller層的開發:注入相應的Mapper介面進行操作

附上Mapper.xml和Mapper介面程式碼:

package com.lm.mapper.test1;
 
import java.util.List;
 
import com.lm.entity.UserEntity;
/**
 * mybatis中mapper介面
 * @author liangming.deng
 * @date   2017年6月21日
 *
 */
@Mapper
public interface User1Mapper {
	List<UserEntity> getAll();
 
	UserEntity getUserById(Long id);
 
	void insert(UserEntity user);
 
	void update(UserEntity user);
 
	void delete(Long id);
}

<?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.lm.mapper.test1.User1Mapper">
	<resultMap id="BaseResultMap" type="com.lm.entity.UserEntity">
		<id column="id" property="id" jdbcType="BIGINT" />
		<result column="userName" property="userName" jdbcType="VARCHAR" />
		<result column="nickName" property="nickName" jdbcType="VARCHAR" />
		<result column="passWord" property="passWord" jdbcType="VARCHAR" />
		<result column="email" property="email" jdbcType="VARCHAR" />
		<result column="regTime" property="regTime" jdbcType="DATE" />
		<result column="sex" property="sexEnums" javaType="com.lm.enums.SexEnums" />
	</resultMap>
 
	<sql id="Base_Column_List">
		id, userName, nickName,passWord, email,regTime,sex
	</sql>
 
	<select id="getAll" resultMap="BaseResultMap">
		SELECT
		<include refid="Base_Column_List" />
		FROM user
	</select>
 
	<select id="getUserById" parameterType="java.lang.Long" resultMap="BaseResultMap">
		SELECT
		<include refid="Base_Column_List" />
		FROM user
		WHERE id = #{id}
	</select>
 
	<insert id="insert" parameterType="com.lm.entity.UserEntity">
		INSERT INTO
		user
		(userName, nickName,passWord, email,regTime,sex)
		VALUES
		(#{userName},#{nickName}, #{passWord},#{email}, #{regTime},#{sexEnums})
	</insert>
 
	<update id="update" parameterType="com.lm.entity.UserEntity">
		UPDATE
		user
		SET
		<if test="userName != null">userName = #{userName},</if>
		<if test="passWord != null">passWord = #{passWord}</if>
		WHERE
		id = #{id}
	</update>
 
	<delete id="delete" parameterType="java.lang.Long">
		DELETE FROM
		user
		WHERE
		id =#{id}
	</delete>
 
</mapper>