プロジェクトの作成
まず、MyBatisのプロジェクト、プロジェクトの作成を作成する必要があり、前述のように、MyBatisのは、MySQLとWeb依存を追加します。
ポンポンファイル
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
<scope>runtime</scope>
</dependency>
注:なしデータソースインスタンスのドルイド伝統は、あなたが私たち自身を設定する必要があるという伝統的なドルイドよりも、アリババのドルイド・春・ブート・スターターを使用して、ここでドルイド、推奨されません。ドルイドスプリングブート・スタータDruidDataSourceBuilder依存クラスを提供するので、この例では、データソースを構築することができます
application.properties設定
spring.datasource.one.url=jdbc:mysql:///test01?useUnicode=true&characterEncoding=utf-8
spring.datasource.one.username=root
spring.datasource.one.password=root
spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.two.url=jdbc:mysql:///test02?useUnicode=true&characterEncoding=utf-8
spring.datasource.two.username=root
spring.datasource.two.password=root
spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource
次のように続いて、2つのDataSourceを提供します:
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.one")
DataSource dsOne() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.two")
DataSource dsTwo() {
return DruidDataSourceBuilder.create().build();
}
}
MyBatisの設定
次は、MyBatisの構成である、JdbcTemplate異なり、MyBatisの構成は、2つの豆を提供するため、より困難ビット、私は二つのクラス、最初に第一のルックに配置された2つの別個のデータソースが存在することになりますコンフィギュレーション・データ・ソース
@Configuration
@MapperScan(basePackages = "org.javayihao.mybatis.mapper1",sqlSessionFactoryRef = "sqlSessionFactory1",sqlSessionTemplateRef = "sqlSessionTemplate1")
public class MyBatisConfigOne {
@Resource(name = "dsOne")
DataSource dsOne;
@Bean
SqlSessionFactory sqlSessionFactory1() {
SqlSessionFactory sessionFactory = null;
try {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dsOne);
sessionFactory = bean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return sessionFactory;
}
@Bean
SqlSessionTemplate sqlSessionTemplate1() {
return new SqlSessionTemplate(sqlSessionFactory1());
}
}
MyBatisConfigOneクラスを作成し、最初のクラスが設定で指定されている、構成、パッケージがorg.javayihao.mybatis.mapper1走査する、すなわちマッパーインタフェース動作におけるパケットデータdsOneに、対応するSqlSessionTemplate SqlSessionFactoryありますsqlSessionFactory1とsqlSessionTemplate1は、MyBatisConfigOne以内に、それぞれ、dsOne SqlSessionFactoryが作成さに応じて、SqlSessionFactoryとSqlSessionTemplateを提供し、その後、良いSqlSessionFactoryを作成するには応じSqlSessionTemplateを作成します。
このように構成した後、このような構成によれば、第二のデータソースが再び配置しました。
@Configuration
@MapperScan(basePackages = "org.javayihao.mybatis.mapper2",sqlSessionFactoryRef = "sqlSessionFactory2",sqlSessionTemplateRef = "sqlSessionTemplate2")
public class MyBatisConfigTwo {
@Resource(name = "dsTwo")
DataSource dsTwo;
@Bean
SqlSessionFactory sqlSessionFactory2() {
SqlSessionFactory sessionFactory = null;
try {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dsTwo);
sessionFactory = bean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return sessionFactory;
}
@Bean
SqlSessionTemplate sqlSessionTemplate2() {
return new SqlSessionTemplate(sqlSessionFactory2());
}
}
MyBatisのは、基本的に構成され、複数のデータソースのみ以下に異なるマッパーorg.javayihao.mybatis.mapper1 org.javayihao.mybatis.mapper2パッケージと異なるマッパ・サービスは異なる注入で動作することができる提供する必要がありますデータソース。
マッパーを作成します。
マッパーでorg.javayihao.mybatis.mapper1:
public interface UserMapperOne {
List<User> getAllUser();
}
対応する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="org.javayihao.mybatis.mapper1.UserMapperOne">
<select id="getAllUser" resultType="org.javayihao.mybatis.model.User">
select * from t_user;
</select>
</mapper>
マッパーでorg.javayihao.mybatis.mapper2:
public interface UserMapper {
List<User> getAllUser();
}
対応する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="org.javayihao.mybatis.mapper2.UserMapper">
<select id="getAllUser" resultType="org.javayihao.mybatis.model.User">
select * from t_user;
</select>
</mapper>
私たちは、その後、さまざまなデータソースへの2つの異なるマッパサービス、異なるマッパー操作に注入します