parent
d2099524ac
commit
af3a894c14
@ -0,0 +1,50 @@
|
|||||||
|
package me.zhengjie.config.mybatis;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
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.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||||
|
|
||||||
|
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@MapperScan(basePackages = "me.zhengjie.mapper.db1.**", sqlSessionFactoryRef = "primarySqlSessionFactory")
|
||||||
|
public class DataSourceConfigPrimary {
|
||||||
|
/**
|
||||||
|
* 配置主数据源
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Bean(name = "primaryDataSource")
|
||||||
|
@Primary
|
||||||
|
@ConfigurationProperties("spring.datasource.druid")
|
||||||
|
public DataSource dataSource() {
|
||||||
|
return DruidDataSourceBuilder.create().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "primarySqlSessionFactory")
|
||||||
|
@Primary
|
||||||
|
public SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource)
|
||||||
|
throws Exception {
|
||||||
|
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
|
||||||
|
bean.setDataSource(dataSource);
|
||||||
|
bean.setMapperLocations(
|
||||||
|
new PathMatchingResourcePatternResolver().getResources("classpath*:me.zhengjie.mapper.db1/*.xml"));
|
||||||
|
return bean.getObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("primarySqlSessionTemplate")
|
||||||
|
@Primary
|
||||||
|
public SqlSessionTemplate sqlSessionTemplate(
|
||||||
|
@Qualifier("primarySqlSessionFactory") SqlSessionFactory sessionFactory) {
|
||||||
|
return new SqlSessionTemplate(sessionFactory);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,44 @@
|
|||||||
|
package me.zhengjie.config.mybatis;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
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.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Primary;
|
||||||
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||||
|
|
||||||
|
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@MapperScan(basePackages = "me.zhengjie.mapper.db2.**", sqlSessionFactoryRef = "secondSqlSessionFactory")
|
||||||
|
public class DataSourceConfigSecond {
|
||||||
|
|
||||||
|
@Bean(name = "secondDataSource")
|
||||||
|
@ConfigurationProperties("spring.datasource.secondary")
|
||||||
|
public DataSource dataSource() {
|
||||||
|
return DruidDataSourceBuilder.create().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "secondSqlSessionFactory")
|
||||||
|
public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource)
|
||||||
|
throws Exception {
|
||||||
|
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
|
||||||
|
bean.setDataSource(dataSource);
|
||||||
|
bean.setMapperLocations(
|
||||||
|
new PathMatchingResourcePatternResolver().getResources("classpath*:me.zhengjie.mapper.db2/*.xml"));
|
||||||
|
return bean.getObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean("secondSqlSessionTemplate")
|
||||||
|
@Primary
|
||||||
|
public SqlSessionTemplate secondSqlSessionTemplate(
|
||||||
|
@Qualifier("secondSqlSessionFactory") SqlSessionFactory sessionFactory) {
|
||||||
|
return new SqlSessionTemplate(sessionFactory);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
package me.zhengjie.mapper.db1;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
|
import me.zhengjie.modules.system.pojo.IStore;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface IStoreMapper {
|
||||||
|
/**
|
||||||
|
* 查询所有门店
|
||||||
|
*/
|
||||||
|
List<IStore> getAll();
|
||||||
|
|
||||||
|
@Select("SELECT * FROM sys_store WHERE name = #{name}")
|
||||||
|
IStore findByName(@Param("name") String name);
|
||||||
|
}
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
package me.zhengjie.mapper.db2;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
|
import me.zhengjie.modules.system.pojo.IStore;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface IStoreMapper2 {
|
||||||
|
/**
|
||||||
|
* 查询所有门店
|
||||||
|
*/
|
||||||
|
List<IStore> getAll();
|
||||||
|
|
||||||
|
@Select("SELECT * FROM sys_store WHERE name = #{name}")
|
||||||
|
IStore findByName(@Param("name") String name);
|
||||||
|
}
|
||||||
@ -1,15 +0,0 @@
|
|||||||
package me.zhengjie.modules.system.mapper;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
|
||||||
|
|
||||||
import me.zhengjie.modules.system.pojo.IStore;
|
|
||||||
|
|
||||||
@Mapper
|
|
||||||
public interface IStoreMapper {
|
|
||||||
/**
|
|
||||||
* 查询所有门店
|
|
||||||
*/
|
|
||||||
List<IStore> getAll();
|
|
||||||
}
|
|
||||||
@ -1,5 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?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">
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
<mapper namespace="me.zhengjie.modules.system.mapper.IStoreMapper">
|
<mapper namespace="me.zhengjie.mapper.db1.IStoreMapper">
|
||||||
<select id="getAll" resultType="IStore"> select * from sys_store </select>
|
<select id="getAll" resultType="IStore"> select * from sys_store </select>
|
||||||
</mapper>
|
</mapper>
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
<?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="me.zhengjie.mapper.db2.IStoreMapper2">
|
||||||
|
<select id="getAll" resultType="IStore"> select * from sys_store </select>
|
||||||
|
</mapper>
|
||||||
Loading…
Reference in new issue