首先回答你的问题,肯定是不可以的。提这个问题可能是你对Spring和SpringMVC本身理解不够。下面我简单介绍一下Spring。
Spring:
我们一般单说Spring是指的Spring Frameworkzh.wikipedia.org
是一个开源的全栈的开发框架,Spring提供一个功能强大的Spring容器。 核心是 AOP 和 IOC。
SpringMVC
要说SpringMVC前,先说一下什么是MVC。
MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式。 可以看到只有是这样三层结构设计的都是MVC框架。 较老的SSH中的 Struts 就是一个MVC框架。随着Spring的发展,Spring本身也有了自己的MVC实现,也就是SpringMVC。而且慢慢取代了Struts。
Spring Boot
因为Spring项目的配置复杂等问题, 为了简化开发者的使用,从而创造性地推出了Spring Boot。约定优于配置,简化了spring的配置流程。 Spring Boot里面有大量的第三方启动包,可以快速搭建项目,可以简单理解是一个 Spring项目的脚手架, 懒人包。
Spring Cloud
有了Spring Boot已经可以快速搭建一个服务。但是随着微服务的兴起,服务越来越多。服务的治理成了最主要的问题, Spring Cloud就应运而生。
Spring Cloud基于Spring Boot,为微服务体系开发中的架构问题,提供了一整完套的解决方案,包括:服务注册与发现,服务消费,服务保护与熔断,网关,分布式调用追踪,分布式配置管理等。
最后官网学习最靠谱,自己可以加深一下理解。
SpringBoot整合Mybatis中如何实现事务控制?
作为一名资深的CURD程序员,事务控制/事务管理是一项不可避免的工作,也是最常见的一项功能,简单说,事务管理就是在执行业务操作时,由于数据操作在顺序执行的过程中,任何一步操作都有可能发生异常,异常会导致后续操作无法完成,此时由于业务逻辑并未正确的完成,之前成功操作数据的并不可靠,需要在这种情况下进行回退。
1、默认的事务管理配置方式:
在引入相关的依赖之后(比如springboot的web组件依赖、父依赖、mysql驱动依赖以及mybatis依赖等)
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.0
需要在设计service层的时候,将方法用@Transational注解进行注释,默认的话在抛出Exception.class异常的时候,就会触发方法中所有数据库操作回滚。
而@Transational注解也拥有许多的参数,比如:
rollbackFor:可以指定需要进行回滚的异常,指定Class对象数组,且该Class必须继承自Throwable;
value:用于在多数据源的情况下,进行事务管理器的指定(下面描述下多数据源事务这种情况);
noRollbackFor:有rollbackFor自然有noRollbackFor,顾名思义,用于指定不需要进行回滚的异常;
readOnly:是读写还是只读事务,默认是false,读写;
还有许多,不一一描述了....
实例:
@Service
public class TestTransactionalService
@Autowired
private TestMapper testMapper;
@Transactional //当抛出Exception的时候,将进行回滚操作
public int insertTest(TestEntity testEntity) {
testEntity.setName("get out! helloService")
return testMapper.insertOne(testEntity);
}
}
另外,在SpringBoot的启动类中,需要增加@EnableTransactionManagement注解,用于启动事务管理。
实例:
@EnableTransactionManagement
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
至此,SpringBoot整合Mybatis的单数据源的事务管理便配置完成
2、多数据源的事务配置方式:
第一种方式基本上满足了普通项目的事务管理功能, 但当项目是比较大型的项目的时候(比如电商项目),可能会存在多个数据源,这时候会出现多个事务管理器,也就需要在声明的时候为不同数据源的数据操作指定不同的事务管理器。
1)首先,需要引入数据源依赖
com.alibaba
druid
1.0.19
2)配置properties配置文件(或者yml文件...)
#主数据源,多数据源的情况下,需要指定主数据源,在后续的config中进行,此时我们将#base数据源作为主数据源来看待~
spring.datasource.base.jdbc-url=jdbc:mysql://localhost:3306/test1
spring.datasource.base.username=root
spring.datasource.base.password=root
spring.datasource.base.driver-class-name=com.mysql.jdbc.Driver
#从数据源
spring.datasource.second2.jdbc-url=jdbc:mysql://localhost:3306/test2
spring.datasource.second2.username=root
spring.datasource.second2.password=root
spring.datasource.second2.driver-class-name=com.mysql.jdbc.Driver
3)新增配置类,读取配置文件,进行数据源的配置
注意,配置类需要对DataSource、DataSourceTransactionManager、SqlSessionFactory 、SqlSessionTemplate四个数据项进行配置;
其中DataSource类型需要引入javax.sql.DataSource;
配置主数据源:
如上文所说,当系统中有多个数据源时,必须有一个数据源为主数据源,在配置类中我们使用@Primary修饰。
通过@MapperScan注解对指定dao包建立映射,确保在多个数据源下,自动选择合适的数据源,而在service层里不需要做特殊说明,否则需要通过@Transactional的value属性进行指定
@Configuration
@MapperScan(basePackages = "com.livinghome.base", sqlSessionTemplateRef = "baseSqlSessionTemplate",sqlSessionFactoryRef = "baseSqlSessionFactory")
public class BaseDataSourceConfig {
/**读取base数据源**/
@Bean(name = "baseDataSource")
@ConfigurationProperties(prefix = "spring.datasource.base")
@Primary
public DataSource setDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "baseTransactionManager")
@Primary
public DataSourceTransactionManager setTransactionManager(@Qualifier("baseDataSource") DataSource dataSource) {
return new DruidDataSource();
}
@Bean(name = "baseSqlSessionFactory")
@Primary
public SqlSessionFactory setSqlSessionFactory(@Qualifier("baseDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/base/*.xml"));
return bean.getObject();
}
@Bean(name = "baseSqlSessionTemplate")
@Primary
public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("baseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
配置从数据源:
@Configuration
@MapperScan(basePackages = "com.livinghome.second2", sqlSessionTemplateRef = "zentaoSqlSessionTemplate",sqlSessionFactoryRef = "zentaoSqlSessionFactory")
public class Second2DataSourceConfig {
@Bean(name = "second2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.second2")
public DataSource setDataSource() {
return new DruidDataSource();
}
@Bean(name = "second2TransactionManager")
public DataSourceTransactionManager setTransactionManager(@Qualifier("second2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "second2SqlSessionFactory")
public SqlSessionFactory setSqlSessionFactory(@Qualifier("second2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/second2/*.xml"));
return bean.getObject();
}
@Bean(name = "second2SqlSessionTemplate")
public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("second2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
4)到了这里,我们基本的多数据源事务管理便已经完成了(真的)....
对于service层,不需要进行事务管理器的指定,因为我们上面使用了@MapperScan进行了包指定,当然也可以手动指定,方式便是 @Transactional(transactionManager="baseTransactionManager")
便可手动指定为base数据源。
另外,还有分布式事务管理,也就是在一次操作中,操作了不同的数据源的情况,对于service而言,便是在一次service里调用了两个数据源的方法,这种情况常见于微服务架构中,例如电商系统(第二次使用电商系统举例..)。
从百度上copy了一个简单的下单流程:
在微服务中,2、3、4步骤是涉及了3个系统以及3个数据库的,当某个操作出现问题时,会出现多数据源的事务管理问题,传统的方式是通过将不同数据源的事务都注册到一个全局事务中(可以通过jpa+atomikos来进行),但有大神告诉我这种方式性能差,具体还未有实践,不是很清楚。
我说完了... 因为对微服务架构学习还在进行中,所以对于分布式事务问题我还没有太多的理解和实践,等我回来....
同时,欢迎大神为我指点明路!!
——没事待在家里不出门的 居家程序员。(我不想脱发!)