一步一步搭建springboot+mybatis+jta框架
程序员文章站
2024-02-25 14:51:03
...
0. 项目目录一览图:
1. 导入项目需要的jar包,如下:
<dependencies>
<!--fastjson json库-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- Apache工具组件 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>com.xiaoleilu</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool-all.version}</version>
</dependency>
<!--lombok jar-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--springmvc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--数据库操作 start-->
<!--使用mysql数据库,导入mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--使用阿里的Druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- mybatis start -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot-starter.version}</version>
</dependency>
<!--分布式事务支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
<!--数据库操作 end-->
<!--使用actuator监控spring boot各种指标,需要打开指标对应的开关-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
2. 配置数据源属性文件(这里配置两个,分别为car,test)
# car数据源配置
spring.datasource.car.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.car.driverClassName=com.mysql.jdbc.Driver
spring.datasource.car.url=jdbc:mysql://ip:3306/db_electric_car?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.car.username=root
spring.datasource.car.password=123456
spring.datasource.car.initialSize=5
spring.datasource.car.minIdle=5
spring.datasource.car.maxActive=20
spring.datasource.car.maxWait=60000
spring.datasource.car.timeBetweenEvictionRunsMillis=60000
spring.datasource.car.minEvictableIdleTimeMillis=300000
spring.datasource.car.validationQuery=SELECT 1 FROM DUAL
spring.datasource.car.testWhileIdle=true
spring.datasource.car.testOnBorrow=false
spring.datasource.car.testOnReturn=false
spring.datasource.car.poolPreparedStatements=true
spring.datasource.car.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.car.filters=stat,wall,log4j
spring.datasource.car.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# test数据源配置
spring.datasource.test.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.test.driverClassName=com.mysql.jdbc.Driver
spring.datasource.test.url=jdbc:mysql://ip:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.test.username=root
spring.datasource.test.password=123456
spring.datasource.test.initialSize=5
spring.datasource.test.minIdle=5
spring.datasource.test.maxActive=20
spring.datasource.test.maxWait=60000
spring.datasource.test.timeBetweenEvictionRunsMillis=60000
spring.datasource.test.minEvictableIdleTimeMillis=300000
spring.datasource.test.validationQuery=SELECT 1 FROM DUAL
spring.datasource.test.testWhileIdle=true
spring.datasource.test.testOnBorrow=false
spring.datasource.test.testOnReturn=false
spring.datasource.test.poolPreparedStatements=true
spring.datasource.test.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.test.filters=stat,wall,log4j
spring.datasource.test.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
3. 将数据源配置映射到对象中
- car属性
@Component //自动注入
@ConfigurationProperties(prefix = "spring.datasource.car")
@Data // lombok注解,生成getter/setter等方法
public class DataSourceCarProperties {
private String type;
private String driverClassName;
private String url;
private String username;
private String password;
private int initialSize;
private int minIdle;
private int maxActive;
private int maxWait;
private int timeBetweenEvictionRunsMillis;
private int minEvictableIdleTimeMillis;
private String validationQuery;
private boolean testWhileIdle;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean poolPreparedStatements;
private int maxPoolPreparedStatementPerConnectionSize;
private String filters;
private String connectionProperties;
}
-
test属性
@Component //自动注入 @ConfigurationProperties(prefix = "spring.datasource.test") @Data public class DataSourceTestProperties { private String type; private String driverClassName; private String url; private String username; private String password; private int initialSize; private int minIdle; private int maxActive; private int maxWait; private int timeBetweenEvictionRunsMillis; private int minEvictableIdleTimeMillis; private String validationQuery; private boolean testWhileIdle; private boolean testOnBorrow; private boolean testOnReturn; private boolean poolPreparedStatements; private int maxPoolPreparedStatementPerConnectionSize; private String filters; private String connectionProperties; }
4. 配置数据源,代码如下:
- car配置
@Configuration
@MapperScan(basePackages = {"com.lcj.web.mapper.car*"}, sqlSessionTemplateRef = "sqlSessionTemplateCar") // 扫描dao或mapper接口
public class DataSourceCarConfig {
@Primary
@Bean(name = "dataSourceCar")
public DataSource dataSourceCar(DataSourceCarProperties dataSourceCarProperties){
DruidXADataSource dataSource = new DruidXADataSource();
BeanUtils.copyProperties(dataSourceCarProperties,dataSource);
AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
xaDataSource.setXaDataSource(dataSource);
xaDataSource.setUniqueResourceName("dataSourceCar");
return xaDataSource;
}
@Bean(name = "sqlSessionFactoryCar")
public SqlSessionFactory sqlSessionFactoryCar(@Qualifier("dataSourceCar") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setTypeAliasesPackage("com.lcj.web.entity.car");
//bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/car/*Mapper.xml"));
return bean.getObject();
}
@Bean(name = "sqlSessionTemplateCar")
public SqlSessionTemplate sqlSessionTemplateCar(
@Qualifier("sqlSessionFactoryCar") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
- test配置
@Configuration
@MapperScan(basePackages = {"com.lcj.web.mapper.test*"}, sqlSessionTemplateRef = "sqlSessionTemplateTest") // 扫描dao或mapper接口
public class DataSourceTestConfig {
@Bean(name = "dataSourceTest")
public DataSource dataSourceTest(DataSourceTestProperties dataSourceTestProperties){
DruidXADataSource dataSource = new DruidXADataSource();
BeanUtils.copyProperties(dataSourceTestProperties,dataSource);
AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
xaDataSource.setXaDataSource(dataSource);
xaDataSource.setUniqueResourceName("dataSourceTest");
return xaDataSource;
}
@Bean(name = "sqlSessionFactoryTest")
public SqlSessionFactory sqlSessionFactoryTest(@Qualifier("dataSourceTest") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setTypeAliasesPackage("com.lcj.web.entity.test");
//bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/test/*Mapper.xml"));
return bean.getObject();
}
@Bean(name = "sqlSessionTemplateTest")
public SqlSessionTemplate sqlSessionTemplateTest(
@Qualifier("sqlSessionFactoryTest") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
5. 配置分布式事务,代码如下:
@Configuration
@EnableTransactionManagement
public class XATransactionManagerConfig {
@Bean(name = "userTransaction")
public UserTransaction userTransaction() throws Throwable {
UserTransactionImp userTransactionImp = new UserTransactionImp();
userTransactionImp.setTransactionTimeout(10000);
return userTransactionImp;
}
@Bean(name = "atomikosTransactionManager", initMethod = "init", destroyMethod = "close")
public TransactionManager atomikosTransactionManager() throws Throwable {
UserTransactionManager userTransactionManager = new UserTransactionManager();
userTransactionManager.setForceShutdown(false);
return userTransactionManager;
}
@Bean(name = "transactionManager")
@DependsOn({ "userTransaction", "atomikosTransactionManager" })
public PlatformTransactionManager transactionManager() throws Throwable {
return new JtaTransactionManager(userTransaction(),atomikosTransactionManager());
}
}
6. 整个项目使用到的技术如下:
- spring boot 1.5.6
- mybatis
- mysql
- druid连接池
- jta分布式事务
- lombok
7. 数据库文件放在entity目录下,其他问题请联系我.
8. 项目源码地址: https://gitee.com/liuchangng/springboot-mybatis-jta
推荐阅读
-
一步一步搭建 Python 高效开发环境: Pycharm + Anaconda
-
一步一步搭建springboot+mybatis+jta框架
-
一步步搭建ssh框架
-
从零一步一步搭建TypeScript+webpack4开发环境
-
JAVA学习之一步步搭建spring框架
-
一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程
-
ef框架-vs2015连接数据库,实体数据模型向导在选择完后点击下一步就闪退
-
使用eclipse + maven一步步搭建SSM框架教程详解
-
一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程
-
新手和企业如何一步步搭建自己的网站?