不多说,直接上代码
application.properties
spring.datasource.dev.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dev.username=root
spring.datasource.dev.password=root
spring.datasource.dev.jdbc-url=jdbc\:mysql\://ip\:3306/dev?autoReconnect\=true\&failOverReadOnly\=false&characterEncoding\=utf8
spring.datasource.dev1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dev1.username=root
spring.datasource.dev1.password=root
spring.datasource.dev1.jdbc-url=jdbc\:mysql\://ip\:3306/dev1?autoReconnect\=true\&failOverReadOnly\=false&characterEncoding\=utf8
创建config包,创建相应的配置文件
DataSourseConfig.java
@Configuration这个注解不能缺少,缺少了不起作用。
@Primary //第一个使用此注解,此后其他配置类删除此注解
/**
* @author john
* @date 2019/2/21
*/
@Configuration
@MapperScan(basePackages = "com.test.dao.dev",sqlSessionTemplateRef = "testSqlSessionTemplate")
public class DataSourseConfig {
@Bean(name = "testDataSource")
@ConfigurationProperties(prefix = "spring.datasource.dev")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "testSqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//使用注解,所以注掉此行,使用xml开启这行
//bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/dev/*.xml"));
return bean.getObject();
}
@Bean(name = "testTransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("testDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "testSqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
DataSourse1Config.java
@Primary 删除,只有第一个配置要添加
/**
* @author john
* @date 2019/2/21
*/
@Configuration
@MapperScan(basePackages = "com.test.dao.dev1",sqlSessionTemplateRef = "test1SqlSessionTemplate")
public class DataSourse1Config {
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.dev1")
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/dev1/*.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);
}
}
创建dao包,分dev和dev1
只粘贴一个dao类作为实例。
/**
* @author john
* @date 2019/2/21
*/
public interface TestDao {
/**
* 查询
* @param keyword
* @return
* @throws Exception
*/
@Select("select * from t_test where keyword like #{keyword}")
List<TestBean> select(@Param("keyword")String keyword)throws Exception;
/**
* 更新url
* @param url
* @param id
* @return
* @throws Exception
*/
@Update("update t_test set url=#{url} where id =#{id}")
int update(@Param("url")String url,@Param("id")long id)throws Exception;
}
启动类
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class EmotionApplication {
public static void main(String[] args) {
SpringApplication.run(EmotionApplication.class, args);
}
}
TestService
/**
* @author john
* @date 2019/2/21
*/
@Component
public class EmotionService {
@Autowired
TestDao testDao;
public void init(Map<String,String> map)throws Exception{
testDao.update("http://",12);
System.out.println("="+keyword+"查询结果保存完成");
}
}