spring boot整合MyBatis使用的一些扫描类

@Configuration可理解为用spring的时候xml里面的标签

@Bean可理解为用spring的时候xml里面的标签

从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法

@Configuration和@Bean的Demo类
在这里插入图片描述
在项目中
@Autowired
private DataSource dataSource;
的时候,这个dataSource 数据源 就是我们在ExampleConfiguration中配的DataSource

扫描mapper.xml文件
@Configuration
@ComponentScan
public class MyBatisConfig {

    @Autowired
    private DataSource dataSource;

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactoryBean sqlSessionFactory(ApplicationContext applicationContext) throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        // sessionFactory.setPlugins(new Interceptor[]{new PageInterceptor()});
 	    //扫描所有的mapper配置文件
       sessionFactory.setMapperLocations(applicationContext.getResources("classpath*:mapper/*.xml"));
        return sessionFactory;
    }
}

@ComponentScan做的事情就是告诉Spring从哪里找到bean
如果项目中所有的类都定义在@SpringBootApplication注解的main app所在的包及其下级包,那你不需要做任何事。

dao扫描器
@Configuration
public class MyBatisScannerConfig {
	//org.mybatis.spring.mapper.MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
    @Bean
    public MapperScannerConfigurer MapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("dao包路径");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        return mapperScannerConfigurer;
    }
}
开启事务管理

在Spring Boot事务管理中,实现自接口PlatformTransactionManager。

当我们使用了spring-boot-starter-jdbc依赖的时候,框 架会自动默认注入DataSourceTransactionManager。所以我们不需要任何额外 配置就可以用@Transactional注解进行事务的使用。

在Service中,被 @Transactional 注解的方法,将支持事务。如果注解在类上,则整个类的所有方法都默认支持事务

== 多事务管理器情况 ==
一:可以通过实现TransactionManagementConfigurer接口,里面方法返回值是默认的事务管理器

二:可以在具体执行方法上设置value

如果Spring容器中存在多个 PlatformTransactionManager 实例,并且没有实现接口TransactionManagementConfigurer 指定默认值,在我们在方法上使用注解 @Transactional 的时候,就必须要用value指定,如果不指定,则会抛出异常

//@EnableTransactionManagement // 开启注解事务管理,等同于xml配置文件中的 <tx:annotation-driven />
@SpringBootApplication
public class ProfiledemoApplication implements TransactionManagementConfigurer {
  @Resource(name="txManager2")
  private PlatformTransactionManager txManager2;
  // 手动创建事务管理器1 datasource框架会自动注入
  //在Spring容器中,我们手工注解@Bean 将被优先加载,框架不会重新实例化其他的 PlatformTransactionManager 实现类。
  @Bean(name = "txManager1")
  public PlatformTransactionManager txManager(DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
  }
 
  // 创建事务管理器2
  @Bean(name = "txManager2")
  public PlatformTransactionManager txManager2(EntityManagerFactory factory) {
    return new JpaTransactionManager(factory);
  }
 
  // 实现接口 TransactionManagementConfigurer 方法,其返回值代表在拥有多个事务管理器的情况下默认使用的事务管理器
  @Override
  public PlatformTransactionManager annotationDrivenTransactionManager() {
    return txManager2;
  }
 
  public static void main(String[] args) {
    SpringApplication.run(ProfiledemoApplication.class, args);
  }
 
}

具体实现

@Component
public class DevSendMessage implements SendMessage {
  // 使用value具体指定使用哪个事务管理器
  @Transactional(value="txManager1")
  @Override
  public void send() {
    System.out.println(">>>>>>>>Dev Send()<<<<<<<<");
    send2();
  }
 
  @Transactional
  public void send2() {
    System.out.println(">>>>>>>>Dev Send2()<<<<<<<<");
  }
}

隔离级别

  • DEFAULT :
    这是默认值,表示使用底层数据库的默认隔离级别。对大部分数据库而言,通常这值就是: READ_COMMITTED 。
  • READ_UNCOMMITTED
    :该隔离级别表示一个事务可以读取另一个事务修改但还没有提交的数据。该级别不能防止脏读和不可重复读,因此很少使用该隔离级别。
  • READ_COMMITTED :
    该隔离级别表示一个事务只能读取另一个事务已经提交的数据。该级别可以防止脏读,这也是大多数情况下的推荐值。
  • REPEATABLE_READ
    :该隔离级别表示一个事务在整个过程中可以多次重复执行某个查询,并且每次返回的记录都相同。即使在多次查询之间有新增的数据满足该查询,这些新增的记录也会被忽略。该级别可以防止脏读和不可重复读。
  • SERIALIZABLE
    :所有的事务依次逐个执行,这样事务之间就完全不可能产生干扰,也就是说,该级别可以防止脏读、不可重复读以及幻读。但是这将严重影响程序的性能。通常情况下也不会用到该级别。

使用配置文件扫描

## tomcat配置
server.port=8009
#server.tomcat.maxHttpHeaderSize=8192
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.risk.encoding=UTF-8
# tomcat最大线程数,默认为200  
server.tomcat.max-threads=800
# session最大超时时间(分钟),默认为30
server.session-timeout=60

## spring 配置
spring.application.name=springboot-mybatis2
application.main=cn.abel.Application

## LOG
logging.file=./logs/springboot-mybatis2.log


## 主数据源,默认的
spring.datasource.url=jdbc:mysql://localhost:3306/oldMan?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#最小空闲连接
spring.datasource.min-idle=5
#最大连接数量
spring.datasource.max-active=100
#检测数据库的查询语句
spring.datasource.validation-query=select 1 from dual
#等待连接池分配连接的最大时长(毫秒)
spring.datasource.connection-timeout=60000
#一个连接的生命时长(毫秒)
spring.datasource.max-left-time=60000
#生效超时
spring.datasource.validation-time-out=3000
#一个连接idle状态的最大时长(毫秒)
spring.datasource.idle-time-out=60000
#设置默认字符集
spring.datasource.connection-init-sql= set names utf8mb4

logging.level.cn.abel.dao=debug
#Mapper.xml所在的位置
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
smybatis.type-aliases-package=cn.abel.bean
#Mapper.xml所在的位置

## pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值