1、@springbootapplication注解
启动项目,整合了一些常用的注解,具有扫包的作用,只在当前同级包下进行扫描
可以不写@ComponentScan(),@EnableAutoConfiguration,将appController.class放在外层目录
2、springboot整合多数据源(分布式,微服务)
在一个项目中,有多个jdbc连接。
多数据源产生的问题:事务管理
在实际项目中,怎么样搭建多数据源,区分数据源
举例:2个数据源test001,test002
1、分包结构(使用较多)
com.soft.test001---访问test001数据库
dao
service
com.soft.test002---访问test002数据库
dao
service
2、使用注解方式
com.soft
dao
service
class UserService{
}
@datasourceTest001--自定义注解形式
public void test001(){}
@datasourceTest002
public void test002(){}
整合多数据源的过程
(1)application.properties自定义数据源
#test1datasources
spring.datasource.test1.url=jdbc:
mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8
spring.datasource.test1.username=root
spring.datasource.test1.password=root
spring.datasource.test1.driver-class-name=com.mysql.jdbc.Driver
#test2datasources
spring.datasource.test2.url=jdbc:
mysql://localhost:3306/springboot1?useUnicode=true&characterEncoding=utf-8
spring.datasource.test2.username=root
spring.datasource.test2.password=root
spring.datasource.test2.driver-class-name=com.mysql.jdbc.Driver
(2)AppJsp.java
package com.soft;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages={"com.soft.mapper"})
public class AppJsp {
public static void main(String[] args) {
SpringApplication.run(AppJsp.class, args);
}
}
(3)创建数据源,多个,
DataSourceConfigTest1.java
package com.soft.datasource;
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.autoconfigure.jdbc.DataSourceBuilder;
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.jdbc.datasource.DataSourceTransactionManager;
@Configuration//注册到spring容器中
@MapperScan(basePackages = "com.soft.test1",sqlSessionFactoryRef = "test1SqlSessionFactory")
//属于自定义数据源,将自定义的数据源注入容器
public class DataSourceConfigTest1 {
@Bean(name = "test1DataSource")
@Primary//
默认数据源,众多数据源里面只有一个自定义数据源里面使用该注解
@ConfigurationProperties(prefix = "spring.datasource.test1")
public DataSource testDataSource(){
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "test1SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource")DataSource dataSource)throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//mybatis配置文件
//bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
return bean.getObject();
}
@Primary
//事务管理
@Bean(name = "test1TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource")DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean(name = "test1SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory")SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
DataSourceConfigTest2.java和上面类似,但是没有@Primary注解
(4)创建相对应的mapper
内容代码如下
package com.soft.test1.dao;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.soft.entity.User;
public interface UserMapperTest1 {
@Select("select * from users where name=#{name}")
User findByname(@Param("name")String name);
@Insert("insert into users(name,age) values(#{name},#{age})")
int insert(@Param("name")String name,@Param("age")Integer age);
}
(5)在controller中进行使用
package com.soft.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.soft.entity.User;
import com.soft.mapper.UserMapper;
import com.soft.test1.dao.UserMapperTest1;
import com.soft.test2.dao.UserMapperTest2;
@Controller
public class IndexController {
@Autowired
private UserMapper userMapper;
@Autowired
private UserMapperTest1 test1Mapper ;
@Autowired
private UserMapperTest2 test2Mapper;
@RequestMapping("/index")
public String index(){
return "index";
}
@RequestMapping("/findByName")
@ResponseBody
public User findByName(String name){
return userMapper.findByname(name);
}
@RequestMapping("/insert")
@ResponseBody
public String insert(String name,Integer age){
userMapper.insert(name, age);
return "success";
}
@RequestMapping("/insert01")
@ResponseBody
public String insert1(String name,Integer age){
test1Mapper.insert(name, age);
System.out.println("insert1");
return "success";
}
@RequestMapping("/insert02")
@ResponseBody
public String insert2(String name,Integer age){
test2Mapper.insert(name, age);
return "success";
}
}
(6)运行代码