java架构学习——28. SpringBoot高级

这篇博客详细介绍了SpringBoot的高级应用,包括多数据源整合、分布式事务管理(使用jta-atomikos)、日志管理(使用log4j)、定时任务和异步调用的实现,以及EhCache缓存的配置和使用。内容涵盖数据源配置、事务管理、日志记录、定时任务和异步方法的注解使用,以及多环境配置和项目打包发布。
摘要由CSDN通过智能技术生成

本篇博文主要包含:

  • springboot整合多数据源实例
    -分包结构(推荐使用)
    -使用自定义注解
  • 事物管理实例
    -用于多数据源:使用springboot+jta-atomikos 分布式事物管理
  • 日志管理
    -使用log4j记录日志
    -log4j结合AOP统一处理Web请求日志
    -springboot + mybatis设置将SQL语句打印到控制台
  • 使用@Scheduled创建定时任务
  • 使用@Async实现异步调用
  • application配置文件里自定义参数
  • 多环境配置
  • 发布打包
  • 缓存支持–注解配置与EhCache使用

一、springboot整合多数据源
如何区分数据源?
1) 分包结构(推荐使用),如:wmq.fly.test01包:访问test01数据库;wmq.fly.test02包:访问test02数据库。
2) 使用自定义注解,如:@DataSource01:访问test01数据库;@DataSource02:访问test02数据库。

分布式事务解决方案:jpa+automikos(传统项目)

  1. 项目目录:
    在这里插入图片描述
    数据库目录:
    在这里插入图片描述
    建表语句:
CREATE TABLE `users`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `age` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) 
  1. application.yml 配置文件中新增两个自定义数据源
spring:
  datasource:
    url: jdbc:mysql://localhost:3308/school?characterEncoding=utf-8
    #url: jdbc:mysql://localhost:8066/test?characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
    
     ###自定义数据源
    test1: 
      url: jdbc:mysql://localhost:3308/test01?useUnicode=true&characterEncoding=utf-8
      username: root
      password: 123456
      driver-class-name: com.mysql.jdbc.Driver
    test2: 
      url: jdbc:mysql://localhost:3308/test02?useUnicode=true&characterEncoding=utf-8
      username: root
      password: 123456
      driver-class-name: com.mysql.jdbc.Driver
  1. 配置文件中新增两个数据源
    DataSource1Config 类,配置test01数据源,用@Primary标记为主数据源:
package wmq.fly.multipleDataSource.source;

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 // 注册到springboot容器中
//wmq.fly.multipleDataSource.test01.dao下的的*mapper.java使用test01数据源
@MapperScan(basePackages = "wmq.fly.multipleDataSource.test01.dao", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class DataSource1Config {

	/**
	 * 配置test1数据库
	 */
	@Bean(name = "test1DataSource")
	@Primary
	@ConfigurationProperties(prefix = "spring.datasource.test1") //从配置文件中读取spring.datasource.test1开头的配置
	public DataSource testDataSource() {
		return DataSourceBuilder.create().build();
	}

	/**
	 * test1 sql会话工厂
	 */
	@Bean(name = "test1SqlSessionFactory")
	@Primary  //设置为主链接,启动项目时使用此链接,不然不知道使用哪个而报错
	public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		//从xml配置文件中读取配置信息
//		bean.setMapperLocations(
//				new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
		return bean.getObject();
	}

	/**
	 * test1 事物管理
	 */
	@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);
	}

}

DataSource2Config ,配置test02数据源,类:

package wmq.fly.multipleDataSource.source;

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.jdbc.datasource.DataSourceTransactionManager;

@Configuration // 注册到springboot容器中
//wmq.fly.multipleDataSource.test02.dao下的的*mapper.java使用test02数据源
@MapperScan(basePackages = "wmq.fly.multipleDataSource.test02.dao", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSource2Config {

	/**
	 * 配置test2数据库
	 */
	@Bean(name = "test2DataSource")
	@ConfigurationProperties(prefix = "spring.datasource.test2")
	public DataSource testDataSource() {
		return DataSourceBuilder.create().build();
	}

	/**
	 * test2 sql会话工厂
	 */
	@Bean(name = "test2SqlSessionFactory")
	public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
//		bean.setMapperLocations(
//				new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test2/*.xml"));
		return bean.getObject();
	}

	/**
	 * test2 事物管理
	 */
	@Bean(name = "test2TransactionManager")
	public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

	@Bean(name = "test2SqlSessionTemplate")
	public SqlSessionTemplate testSqlSessionTemplate(
			@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}

}
  1. 实体类User类
package wmq.fly.multipleDataSource.entity;

public class User {

	private String name;
	
	private Integer age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}
		
}

  1. 创建分包Mapper
    multipleDataSource.test01.dao下的UserMapper01 类,使用test01数据源:
package wmq.fly.multipleDataSource.test01.dao;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import wmq.fly.multipleDataSource.entity.User;

public interface UserMapper01 {
	
	@Select("SELECT * FROM USERS WHERE NAME = #{name}")
	User findByName(@Par
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值