作者简介:zhz小白
公众号:小白的Java进阶之路
专业技能:
1、Java基础,并精通多线程的开发,熟悉JVM原理
2、熟悉Java基础,并精通多线程的开发,熟悉JVM原理,具备⼀定的线上调优经验
3、熟悉MySQL数据库调优,索引原理等,⽇志原理等,并且有出过⼀篇专栏
4、了解计算机⽹络,对TCP协议,滑动窗⼝原理等有⼀定了解
5、熟悉Spring,Spring MVC,Mybatis,阅读过部分Spring源码
6、熟悉SpringCloud Alibaba体系,阅读过Nacos,Sentinel,Seata,Dubbo,Feign,Gateway核⼼源码与设计,⼆次开发能⼒
7、熟悉消息队列(Kafka,RocketMQ)的原理与设计
8、熟悉分库分表ShardingSphere,具有真实⽣产的数据迁移经验
9、熟悉分布式缓存中间件Redis,对其的核⼼数据结构,部署架构,⾼并发问题解决⽅案有⼀定的积累
10、熟悉常⽤设计模式,并运⽤于实践⼯作中
11、了解ElasticSearch,对其核⼼的原理有⼀定的了解
12、了解K8s,Jekins,GitLab
13、了解VUE,GO
14、⽬前有正在利⽤闲暇时间做互游游戏,开发、运维、运营、推销等
本人著作git项目:https://gitee.com/zhouzhz/star-jersey-platform,有兴趣的可以私聊博主一起编写,或者给颗star
领域:对支付(FMS,FUND,PAY),订单(OMS),出行行业等有相关的开发领域
如果此文还不错的话,还请关注、点赞、收藏三连支持一下博主~
applicationContext.getBean(类名.class);
applicationContext.getBean("组件的id");
例子如下:
@Component
public class StudentService {
@Autowired
private StudentDao studentDao;
public Student findStudentById(int id) {
return studentDao.findById(id);
}
}
@Test
public void t2(){
ApplicationContext ac = new
ClassPathXmlApplicationContext("bean.xml")
;
StudentService studentService = (StudentService)ac.getBean("studentService");
System.out.println(studentService.findStudentById(1));
}
@Component
public class StudentService {
private StudentDao studentDao;
@Autowired
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public Student findStudentById(int id) {
return studentDao.findById(id);
}
}
@Test
public void t2(){
ApplicationContext ac = new
ClassPathXmlApplicationContext("bean.xml")
;
StudentService studentService = (StudentService)ac.getBean("studentService");
System.out.println(studentService.findStudentById(1));
}
@Component
public class StudentService {
private StudentDao studentDao;
@Autowired
public StudentService(StudentDao studentDao) {
this.studentDao = studentDao;
}
public Student findStudentById(int id) {
return studentDao.findById(id);
}
}
@Test
public void t2(){
ApplicationContext ac = new
ClassPathXmlApplicationContext("bean.xml")
;
StudentService studentService = (StudentService)ac.getBean("studentService");
System.out.println(studentService.findStudentById(1));
}
@Component
public class StudentService {
@Autowired
@Qualifier("studentDaoImpl2")
private StudentDao studentDao;
public Student findStudentById(int id){
return studentDao.findById(id);
}
}
spring.datasource.first.url = jdbc:mysql://192.168.0.1:3306/test1?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT&useSSL=false
spring.datasource.first.username = root
spring.datasource.first.password = 123456
spring.datasource.first.driverClassName = com.mysql.jdbc.Driver
spring.datasource.second.url = jdbc:mysql://192.168.0.2:3306/test1?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT&useSSL=false
spring.datasource.second.username = root
spring.datasource.second.password = 123456
spring.datasource.second.driverClassName = com.mysql.jdbc.Driver
#获取连接时验证
spring.datasource.first.test-on-borrow=true
#验证连接的有效性
spring.datasource.first.test-while-idle=true
#空闲连接回收的时间间隔,和test-while-idle一起使用
spring.datasource.first.time-between-eviction-runs-millis=300000
#连接池空闲连接的有效时间
spring.datasource.first.min-evictable-idle-time-millis=1800000
spring.datasource.first.test-on-return=true
spring.datasource.first.validation-query=SELECT 1
#获取连接时验证
spring.datasource.second.test-on-borrow=true
#验证连接的有效性
spring.datasource.second.test-while-idle=true
#空闲连接回收的时间间隔,和test-while-idle一起使用
spring.datasource.second.time-between-eviction-runs-millis=300000
#连接池空闲连接的有效时间
spring.datasource.second.min-evictable-idle-time-millis=1800000
spring.datasource.second.test-on-return=true
spring.datasource.second.validation-query=SELECT 1
每个数据源都需要一个配置类。同时dao包和mapping包下需要分别再分成first包和second包存放对应的Mapper文件和xml文件。
@Configuration
@MapperScan(basePackages = "com.test.dao.first", sqlSessionFactoryRef = "firstSqlSessionFactory")
public class FirstDataSourceConfig{
@Primary
@Bean(name = "firstDataSource")
@ConfigurationProperties(prefix = "spring.datasource.first")
public DataSource firstDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "firstSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("firstDataSource") Datasource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapping/first/*.xml"));
return bean.getObject();
}
@Bean(name = "firstSqlSessionTemplate")
public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(basePackages = "com.test.dao.second", sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondDataSourceConfig{
@Primary
@Bean(name = "secondDataSource")
@ConfigurationProperties(prefix = "spring.datasource.second")
public DataSource secondDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("secondDataSource") Datasource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapping/second/*.xml"));
return bean.getObject();
}
@Bean(name = "secondSqlSessionTemplate")
public SqlSessionTemplate
secondSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
将first和second数据源相关的maper和xml文件分别放在dao包和mapping包下的first和second目录中。这个时候就已经可以连接数据库测试了,可以从不同数据源的mapper中操作数据。