@Configuration
@MapperScan(basePackages ="com.example.demo3.mapper.mapper23", sqlSessionTemplateRef ="primarySqlSessionTemplate")
public class DataSource23Config {
@Bean(name ="primaryDataSource")
@ConfigurationProperties(prefix ="datasource.primary")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name ="primarySqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource)throws Exception {
SqlSessionFactoryBean bean =new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name ="primaryTransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name ="primarySqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory)throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
=====================================================
@Configuration
@MapperScan(basePackages ="com.example.demo3.mapper.mapper51New", sqlSessionTemplateRef ="freeSqlSessionTemplate")
public class DataSource51ceshiConfig {
@Bean(name ="freeDataSource")
@ConfigurationProperties(prefix ="datasource.free")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name ="freeSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("freeDataSource") DataSource dataSource)throws Exception {
SqlSessionFactoryBean bean =new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name ="freeTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("freeDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name ="freeSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("freeSqlSessionFactory") SqlSessionFactory sqlSessionFactory)throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
==============================================
@Configuration
@MapperScan(basePackages ="com.example.demo3.mapper.mapper51", sqlSessionTemplateRef ="secondarySqlSessionTemplate")
public class DataSource51Config {
@Bean(name ="secondaryDataSource")
@ConfigurationProperties(prefix ="datasource.secondary")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name ="secondarySqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource)throws Exception {
SqlSessionFactoryBean bean =new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name ="secondaryTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name ="secondarySqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory)throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
======================================================
@Service
@Mapper
public interface UserMapper1 {
@Select("select id,name,age from stu")
@Results({
@Result(property ="id", column ="id"),
@Result(column="name", property="name"),
@Result(column="age", property="age")
})
List findAll();
@Select("SELECT * FROM stu WHERE id=#{id}")
@Results({
@Result(property ="id", column ="id"),
@Result(column="name", property="name"),
@Result(column="age", property="age")
})
StuCeshi findOne(Integer id);
@Insert("INSERT INTO stu(name,age) VALUES (#{name},#{age})")
void addOne(StuCeshi user);
@Update("UPDATE stu SET name=#{name},age=#{age} WHERE id=#{id}")
void updateOne(StuCeshi user);
@Delete("DELETE FROM stu WHERE id=#{id}")
void delOne(Integer id);
}
========================================================
@Service
@Mapper
public interface People {
@Select("select * from v_SubERP_EmpInfo where Status = '在职'")
List findAll();
}
===========================================================
@Service
@Mapper
public interface UserMapper2 {
@Select("select id,mail,name,phone from contact")
@Results({
@Result(column="id",property="id"),
@Result(column="mail",property="mail"),
@Result(column="name",property="name"),
@Result(column="phone",property="phone")
})
List findAll();
@Select("SELECT * FROM contact WHERE id=#{id}")
@Results({
@Result(column="id",property="id"),
@Result(column="mail",property="mail"),
@Result(column="name",property="name"),
@Result(column="phone",property="phone")
})
User findOne(Integer id);
@Insert("INSERT INTO contact(mail,name,phone) VALUES (#{mail},#{name},#{phone})")
void addOne(User user);
@Update("UPDATE contact SET mail=#{mail},name=#{name},phone=#{phone} WHERE id=#{id}")
void updateOne(User user);
@Delete("delete FROM contact WHERE id=#{id}")
void delOne(Integer id);
}
=========================================================
datasource.primary.jdbc-url=jdbc:sqlserver://192.168.0.23:1433;DatabaseName=xiaoceshi
datasource.primary.username=sa
datasource.primary.password=yanmade2016!
datasource.secondary.jdbc-url=jdbc:sqlserver://192.168.0.51:1433;DatabaseName=AIS002test20161201
datasource.secondary.username=suberp
datasource.secondary.password=K3@)!*2018
datasource.free.jdbc-url=jdbc:sqlserver://192.168.0.51:1433;DatabaseName=xiaoceshi
datasource.free.username=suberp
datasource.free.password=K3@)!*2018
====================================================
##################################################
maven的pom文件添加以下:
##################################################
=====================================================
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1
com.microsoft.sqlserver
mssql-jdbc
runtime
org.springframework.boot
spring-boot-devtools
true