环境: intellij idea 2017.1.4 + spring boot 2.0
注:此代码中的db1,db2为mysql数据源相关,db3为sqlserver数据源
先在application.properties 文件中添加数据库的配置
#spring.datasource.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
#spring.datasource.username=root
#spring.datasource.password=
#mybatis.mapper-locations: classpath:mapper/*.xml
## db1 database
spring.datasource.db1.jdbc-url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.db1.username=root
spring.datasource.db1.password=
spring.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
## db2 database
spring.datasource.db2.jdbc-url=jdbc:mysql://localhost:3306/news?useUnicode=true&characterEncoding=UTF-8
spring.datasource.db2.username=root
spring.datasource.db2.password=
spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver
## db3 database
spring.datasource.db3.jdbc-url=jdbc:sqlserver://ip:port;DatabaseName=dbname
spring.datasource.db3.username=dbaccountname
spring.datasource.db3.password=dbaccountpassowrd
spring.datasource.db3.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
## Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=200
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=10
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=3000
在datasouce目录下增加DataSourceConfig3文件,DataSourceConfig1,DataSourceConfig2略(资源代码里有),如下:
package com.neo.datasource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.neo.mapper.db3", sqlSessionFactoryRef = "db3SqlSessionTemplate")
public class DataSourceConfig3 {
@Bean(name = "sqlServerDataSource")
@Qualifier("sqlServerDataSource")
@ConfigurationProperties(prefix="spring.datasource.db3")
public DataSource getMyDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("sqlServerDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
private static final String MAPPER_PATH = "classpath:mapper/db3/*.xml";
private static final String ENTITY_PACKAGE = "com.neo.mapper.db3";
@Bean(name = "db3SqlSessionTemplate")
public SqlSessionFactory devSqlSessionFactory(
@Qualifier("sqlServerDataSource") DataSource ddataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(ddataSource);
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactory.setMapperLocations(resolver.getResources(MAPPER_PATH));
sessionFactory.setTypeAliasesPackage(ENTITY_PACKAGE);
return sessionFactory.getObject();
}
@Bean
public PlatformTransactionManager sqlServerTransactionManager(@Qualifier("sqlServerDataSource") DataSource sqlServerDataSource)
{
return new DataSourceTransactionManager(sqlServerDataSource);
}
}
entity
package com.neo.entity.db3;
import org.springframework.stereotype.Component;
@Component
public class City {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getParentId() {
return ParentId;
}
public void setParentId(int parentId) {
ParentId = parentId;
}
public int getSort() {
return Sort;
}
public void setSort(int sort) {
Sort = sort;
}
private String Name;
private int ParentId;
private int Sort;
}
resource/mapper/db3/city.xml
delete from T_City where id =#{id}
insert into T_City(Name,ParentId,Sort)values(#{Name},#{ParentId},#{Sort})
update T_City
city.name = #{name},
where id = #{id}
mapper/db3/CityMapper
package com.neo.mapper.db3;
import com.neo.entity.db3.City;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface CityMapper {
//获取列表
public List getCity() throws Exception;
//根据id删除
public void deleteCity(int id)throws Exception;
//新增
public void addCity(City city)throws Exception;
//修改信息
//public void updateCity(City city) throws Exception;
}
CityService:
package com.neo.service;
import com.neo.entity.db3.City;
import java.util.List;
public interface CityService {
//显示所有用户
public List getCity()throws Exception;
//根据id删除用户
public void deleteCity(int id)throws Exception;
//新增用户
public void addCity(City city)throws Exception;
}
package com.neo.service;
import com.neo.entity.db3.City;
import com.neo.mapper.db3.CityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CityServiceImpl implements CityService {
@Autowired
private CityMapper cityMapper;
@Override
public List getCity() throws Exception {
return cityMapper.getCity();
}
//根据id删除用户
@Override
public void deleteCity(int id) throws Exception {
cityMapper.deleteCity(id);
}
//新增用户
@Override
public void addCity(City city) throws Exception {
cityMapper.addCity(city);
}
}
controller
package com.neo.controller;
import com.neo.entity.db3.City;
import com.neo.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class CityController {
@Autowired
private CityService cityService;
@Autowired
private City city;
//显示用户
@RequestMapping("city/list")
public List index() throws Exception {
return cityService.getCity();
}
//增加用户
@RequestMapping("city/add")
public String addCity() throws Exception {
city.setName("test省份");
city.setParentId(0);
city.setSort(0);
cityService.addCity(city);
return "增加";
}
}
pom.xml如下:
4.0.0
com.neo
spring-boot-hello
1.0
jar
spring-boot-hello
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
mysql
mysql-connector-java
junit
junit
org.springframework.boot
spring-boot-starter-data-redis
com.microsoft.sqlserver
mssql-jdbc
6.4.0.jre8
runtime
org.springframework.boot
spring-boot-maven-plugin
至些所有的文件都ok了,编译运行
http://localhost:8080/city/list
http://localhost:8080/list
都正常显示,如果数据库表里没有数据,可以用add方法先添加。
附完整测试项目包地址: https://download.csdn.net/download/huwei2003/11131140
--- end ---