1. 程式人生 > >註解型多資料來源Mybatis.md

註解型多資料來源Mybatis.md


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

  2. 技術要點

  • springboot整合mybatis配置:
註解型的,配置檔案去除mybatis相關配置

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

相對xml型的多資料來源,去除配置資料來源對應的myabtis中mapper.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);
		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);
	}
 
}

  • 使用mybatis註解對Mapper介面進行開發:

/**
 * mybatis中mapper介面
 * @author liangming.deng
 * @date   2017年6月21日
 *
 */
public interface User1Mapper {
	@Select("select * from user")
	@Results({
		@Result(id=true,property="id",column="id"),
		@Result(property="userName",column="userName"),
		@Result(property="nickName",column="nickName"),
		@Result(property="passWord",column="passWord"),
		@Result(property="email",column="email"),
		@Result(property="regTime",column="regTime"),
		@Result(property="sexEnums",column="sex",javaType=SexEnums.class)})
	List<UserEntity> getAll();
 
	@Select("select * from user where id=#{id}")
	@Results({
		@Result(id=true,property="id",column="id"),
		@Result(property="userName",column="userName"),
		@Result(property="nickName",column="nickName"),
		@Result(property="passWord",column="passWord"),
		@Result(property="email",column="email"),
		@Result(property="regTime",column="regTime"),
		@Result(property="sexEnums",column="sex",javaType=SexEnums.class)})
	UserEntity getUserById(Long id);
 
	@Insert("INSERT INTO user(userName,nickName,passWord,email,regTime,sex) "
			+ "VALUES(#{userName}, #{nickName}, #{passWord}, #{email}, #{regTime}, #{sex}) ")
	void insert(UserEntity user);
 
	
	@Update("UPDATE user SET userName=#{userName},nickName=#{nickName} where id=#{id}")
	void update(UserEntity user);
 
	@Delete("DELETE FROM user where id=#{id}")
	void delete(Long id);
}

  1. 進行controller層的開發:注入相應的Mapper介面進行操作