springboot 整合 mybatis

选择依赖

在这里插入图片描述

目录

在这里插入图片描述
其中,controller、service、dao、pojo类,分别是controller层、service层、dao层、pojo类的文件夹。目录功能和内容与springMVC相同,不在赘述。同样,UserMapper.xml文件也是与springMvc整合mybatis的方法也是一样的。Controller层、service层需要分别打上相应的注解,以方便容器进行依赖注入。

接下来,着重讲一下springboot的启动项文件,springboot 配置文件的内容。

配置文件

spring-datasource 配置数据源。这里使用的是druid的连接池,所以要引入相应的依赖,除此之外需要进行数据库的配置,这里与SSM框架中类似。
mybatis 是mybatis的配置,这里配置了mapper文件的位置,以及别名。

spring:
  datasource:
    name: mysql_test
    type: com.alibaba.druid.pool.DruidDataSource
    #druid相关配置
    druid:
      #监控统计拦截的filters
      filters: stat
      driver-class-name: com.mysql.cj.jdbc.Driver
      #基本属性
      # 要加上时区,不加会报错
      url: jdbc:mysql://127.0.0.1:3306/bi?serverTimezone=UTC
      username: root
      password: 1234
      #配置初始化大小/最小/最大
      initial-size: 1
      min-idle: 1
      max-active: 20
      #获取连接等待超时时间
      max-wait: 60000
      #间隔多久进行一次检测,检测需要关闭的空闲连接
      time-between-eviction-runs-millis: 60000
      #一个连接在池中最小生存的时间
      min-evictable-idle-time-millis: 300000
      validation-query: SELECT 'x'
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
      pool-prepared-statements: false
      max-pool-prepared-statement-per-connection-size: 20

mybatis:
  mapper-locations: classpath:mapper/*.xml  #注意:一定要对应mapper映射xml文件的所在路径
  type-aliases-package: com.learn.springbootmybatis.pojo  # 注意:对应实体类的路径

springboot启动文件

这里需要注意的两点是第一个注解MapperScan用于扫描dao层文件的位置。第二个注解是一个组合注解,其中包括一个自动扫描包的注解。

@MapperScan("com.learn.springbootmybatis.dao")
@SpringBootApplication
public class SpringbootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}

运行结果

在这里插入图片描述

项目源码

https://github.com/LuAju/springboot-mybatis

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值