栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBoot整合MyBatis

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合MyBatis

数据源配置

这里选择阿里的Druid数据源,Druid性能优秀,同时还提供对应用性能的监控。

首先,在POM文件中加入连接器和数据库的依赖


    mysql
    mysql-connector-java
    5.0.4
    com.alibaba
    druid
    1.1.0

在配置文件application.yml中,进行数据库连接信息和数据源的配置。

spring:
  datasource:    type: com.alibaba.druid.pool.DruidDataSource # 配置当前要使用的数据源的操作类型
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
    filters: stat,wall,log4j                       # Durid监控使用
    dbcp2:                                             # 进行数据库连接池的配置
      min-idle: 5
      initial-size: 5
      max-total: 10
      max-wait-millis: 200                      # 等待连接获取的最大超时时间

此时如果,想要验证数据的配置和连接池的配置是否生效,可以通过在测试类中获取数据库连接进行打印。

@SpringBootTest(classes = StartSpringBootMain.class)@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfigurationpublic class TestDataSource {  @Resource
  private DataSource dataSource;  @Test
  public void testConnection() throws Exception {
    System.out.println(this.dataSource.getConnection());
  }
}

在 mybatis 开发包里面会将 druid的配置的数据库连接池变为所需要的 DataSource 数据源对象。所以运行上面测试代码的前提条件是引入了mybatis-spring-boot-starter依赖。

配置Druid监控

要想进行 Druid 的性能的监控操作,则需要做一些基础配置,例如:你访问的 IP 地址是否是白名单。

@Configurationpublic class DruidConfig {    @Bean
    public ServletRegistrationBean druidServlet() { // 主要实现WEB监控的配置处理
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(                new StatViewServlet(), "/druid*.xml

下面通过一个产品(Product)的例子进行说明

在 src/main/resources 目录下建立 mybatis/mybatis.cfg.xml 配置文件


    
      
        
    

建立Product的Bean对象

public class Product {
  private Long id;  private String name;  private Long size;  // ignore get/set method}

创建ProductMapper接口

@Mapper   //此注解要加上,不然DAO接口和 Mapper 文件无法整合在一起。public interface ProductMapper {    List findAll();    boolean add(Product product);
}

在src/main/resources/mybatis 下建立有一个 mapper 子目录,然后创建Product的Mapper文件ProductMapper.xml


    
        SELECT * FROM product ;    

    
        INSERT INTO product(name,size) VALUES (#{name},#{size});    

Service和Controller相关的类省略,具体请参考:https://gitee.com/codingXcong/spring-boot-demo/tree/master/springboot-mybatis

Spring事务

在Spring中可以通过@EnableTransactionManagement启用事务。

@SpringBootApplication@EnableTransactionManagement //启用事务public class StartSpringBootMain {    public static void main(String[] args) {
        SpringApplication.run(StartSpringBootMain.class, args);
    }
}

如果想要对某个方式使用事务,可以通过@Transactional注解。

public interface ProductService {    //@Transactional(readOnly = true)
    @Transactional(propagation = Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)    boolean add(Product product);
}

其中propagation属性表示的为事务的传播行为,Spring当中定义了七种传播行为。


事务的传播行为


isolation属性表示设置隔离级别,Spring支持的事务隔离级别如下所示


事务隔离级别



作者:Coding小聪
链接:https://www.jianshu.com/p/65377224ef70


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235364.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号