springboot配置mybatis

文章介绍了如何在SpringBoot项目中引入MyBatis-Spring-Boot-Starter依赖,并配置Druid数据源来连接MySQL数据库。同时,对MyBatis的配置进行了详细说明,包括设置类型别名包、启用二级缓存和懒加载等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步,在pom.xml文件中引入依赖

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>


第二步,在application.yml中配置 数据库连接信息 和 mybatis配置

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

                        

#配置mybatis
mybatis:
  type-aliases-package: zchao.com.springboot.generator.model
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    #开启MyBatis的二级缓存
    cache-enabled: true
    #开启懒加载
    lazy-loading-enabled: true
    #false 为按需加载
    aggressive-lazy-loading: false
  mapper-locations: classpath:mapper/*.xml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

稻草人想看远方

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值