目录
1、添加依赖
2. 配置数据源
3. 创建数据源配置类
4. 创建Mapper接口和XML映射文件
5. 使用Mapper
6.启动类配置
7.项目结构目录
1、添加依赖
首先,在pom.xml
文件中添加SQL Server的JDBC驱动:
<!-- SQL Server Connector -->
<dependency><groupId>com.microsoft.sqlserver</groupId><artifactId>mssql-jdbc</artifactId>
</dependency>
2. 配置数据源
在application.yml文件中为每个数据库配置数据源:
spring:datasource:p1:jdbc-url: jdbc:sqlserver://localhost:1433;databaseName=p1username: sapassword: passworddriver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriverp2:jdbc-url: jdbc:sqlserver://localhost:1433;databaseName=p2username: sapassword: passworddriver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDrivermybatis:mapper-locations: classpath:mapper/**/*.xml
3. 创建数据源配置类
为每个数据源创建单独的配置类。这些配置类将负责配置数据源和SqlSessionFactory。
p1 数据源配置
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.xxx.parkrecord.mapper.p1", sqlSessionFactoryRef = "p1SqlSessionFactory")
public class DataSourceConfigP1 {@Primary // 表示这个数据源是默认数据源, 这个注解必须要加,因为不加的话spring将分不清楚那个为主数据源(默认数据源)@Bean("p1DataSource")@ConfigurationProperties(prefix = "spring.datasource.p1") //读取application.yml中的配置参数映射成为一个对象public DataSource getDb1DataSource(){return DataSourceBuilder.create().build();}@Primary@Bean("p1SqlSessionFactory")public SqlSessionFactory p1SqlSessionFactory(@Qualifier("p1DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);// mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/p1/*.xml"));bean.setTypeAliasesPackage("com.xxx.parkrecord.model");return bean.getObject();}@Primary@Bean("p1SqlSessionTemplate")public SqlSessionTemplate p1SqlSessionTemplate(@Qualifier("p1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){return new SqlSessionTemplate(sqlSessionFactory);}@Primary@Bean("p1TransactionManager")public DataSourceTransactionManager p1TransactionManager(@Qualifier("p1DataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}
}
p2 数据源配置
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.xxx.parkrecord.mapper.p2", sqlSessionFactoryRef = "p2SqlSessionFactory")
public class DataSourceConfigP2 {@Bean("p2DataSource")@ConfigurationProperties(prefix = "spring.datasource.p2") //读取application.yml中的配置参数映射成为一个对象public DataSource getDb1DataSource(){return DataSourceBuilder.create().build();}@Bean("p2SqlSessionFactory")public SqlSessionFactory p2SqlSessionFactory(@Qualifier("p2DataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);// mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/p2/*.xml"));bean.setTypeAliasesPackage("com.xxx.parkrecord.model");return bean.getObject();}@Bean("p2SqlSessionTemplate")public SqlSessionTemplate p2SqlSessionTemplate(@Qualifier("p2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){return new SqlSessionTemplate(sqlSessionFactory);}@Bean("p2TransactionManager")public DataSourceTransactionManager p2TransactionManager(@Qualifier("p2DataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}
}
4. 创建Mapper接口和XML映射文件
为每个数据源创建对应的Mapper接口和XML映射文件。
p1 Mapper接口
import com.xxx.parkrecord.model.P1Entity;
import org.apache.ibatis.annotations.Mapper;import java.util.List;
@Mapper
public interface ParkRecordP1Dao{List<P1Entity> findAll();
}
p1 XML映射文件
在src/main/resources/mapper/p1/ParkRecordP1Dao.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.xxx.parkrecord.mapper.p1.ParkRecordP1Dao"><select id="findAll" resultType="com.xxx.parkrecord.model.P1Entity">SELECT * FROM table1</select>
</mapper>
p2 Mapper接口
import com.xxx.parkrecord.model.P2Entity;
import org.apache.ibatis.annotations.Mapper;import java.util.List;
@Mapper
public interface ParkRecordP2Dao{List<P2Entity> findAll();
}
p2 XML映射文件
在src/main/resources/mapper/p2/ParkRecordP2Dao.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.xxx.parkrecord.mapper.p2.ParkRecordP2Dao"><select id="findAll" resultType="com.xxx.parkrecord.model.P2Entity">SELECT * FROM table2</select>
</mapper>
5. 使用Mapper
@Service
public class MyService {@Autowiredprivate ParkRecordP1Dao parkRecordP1Dao;@Autowiredprivate ParkRecordP2Dao parkRecordP2Dao;public void performOperations() {// 使用 p1 数据源List<P1Entity> p1Entities = parkRecordP1Dao.findAll();// 使用 p2 数据源List<P2Entity> p2Entities = parkRecordP2Dao.findAll();}
}
6.启动类配置
添加MapperScan配置
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan(basePackages = {"com.xxx.parkrecord.mapper.p1","com.xxx.parkrecord.mapper.p2"})
public class XxxApplication {public static void main(String[] args) {SpringApplication.run(XxxApplication.class, args);}}
7.项目结构目录
亲测有效!
注:文中如有问题,欢迎指正。