springboot配置mybatis

第一步,在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot 配置 MyBatis 的步骤如下: 1. 添加 MyBatisMyBatis-Spring 依赖 在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ``` 2. 配置数据源 在 application.properties 文件中添加以下配置: ``` spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3. 配置 MyBatis 在 application.properties 文件中添加以下配置: ``` mybatis.mapper-locations=classpath:mapper/*.xml ``` 4. 创建 Mapper 接口和 XML 文件 在 src/main/resources/mapper 目录下创建 Mapper 接口和 XML 文件,例如: UserMapper.java: ``` public interface UserMapper { User getUserById(int id); } ``` UserMapper.xml: ``` <mapper namespace="com.example.mapper.UserMapper"> <select id="getUserById" parameterType="int" resultType="com.example.entity.User"> select * from user where id = #{id} </select> </mapper> ``` 5. 注册 Mapper 接口 在启动类中添加 @MapperScan 注解,例如: ``` @SpringBootApplication @MapperScan("com.example.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 以上就是 Spring Boot 配置 MyBatis 的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

稻草人想看远方

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

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

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

打赏作者

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

抵扣说明:

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

余额充值