• SpringBoot整合Mybatis


    xml方式整合Mybatis

    xml方式在编写复杂SQL时,更适合

    1.导入依赖

    1. <dependency>
    2. <groupId>mysqlgroupId>
    3. <artifactId>mysql-connector-javaartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>com.alibabagroupId>
    7. <artifactId>druid-spring-boot-starterartifactId>
    8. <version>1.1.10version>
    9. dependency>
    10. <dependency>
    11. <groupId>org.mybatis.spring.bootgroupId>
    12. <artifactId>mybatis-spring-boot-starterartifactId>
    13. <version>1.3.2version>
    14. dependency>

    2.编写配置文件

    1. //编写实体类
    2. @Data
    3. public class User implements Serializable {
    4. private Integer id;
    5. private String name;
    6. private Integer age;
    7. private String gender;
    8. }

    3.准备Mybatis

               1.接口

                     

           2. 在启动类中添加直接,扫描Mapper接口所在的包

         3. 准备映射文件

     

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.qianfeng.day1124.dao.UserDao">
    6. <select id="getAll" resultType="User">
    7. select * from user
    8. select>
    9. mapper>

         4. yml文件

    1. server:
    2. port: 8080
    3. mybatis:
    4. mapper-locations: classpath:mappers/*.xml
    5. # 配置别名扫描的包
    6. type-aliases-package: com.qianfeng.day1124.entity
    7. spring:
    8. datasource:
    9. driver-class-name: com.mysql.cj.jdbc.Driver
    10. url: jdbc:mysql:///crm?serverTimezone=Asia/Shanghai
    11. username: root
    12. password: 123456

    测试启动类测试

     

     

    注解方式整合Mybatis

    注解方式在编写配置简单,简单SQL推荐使用

    创建的Mapper接口

     

    添加Mybatis注解

    针对增删改查:@Insert,@Delete,@Update,@Select

    还是需要在启动类中添加@MapperScan注解

     

    添加配置

    1. // yml文件
    2. logging:
    3. level:
    4. com.qf.firstspringboot.mapper: DEBUG

     application.yml

    1. server:
    2. port: 8080
    3. mybatis:
    4. mapper-locations: classpath:mappers/*.xml
    5. type-aliases-package: com.qianfeng.day1124.entity
    6. spring:
    7. datasource:
    8. driver-class-name: com.mysql.cj.jdbc.Driver
    9. url: jdbc:mysql:///crm?serverTimezone=Asia/Shanghai
    10. username: root
    11. password: 123456
    12. type: com.alibaba.druid.pool.DruidDataSource
    13. thymeleaf:
    14. mode: HTML #thymeleaf 的模板模型
    15. cache: false #不适用缓存
    16. encoding: UTF-8 #编码
    17. prefix: classpath:/templates/ #前缀
    18. suffix: .html #后面
    19. logging:
    20. level:
    21. com.qianfeng.day1124.dao: debug

     

     

  • 相关阅读:
    2023年MySQL实战核心技术第一篇
    10.selenium进阶
    js 字符串转数字
    Docker启动SRS流媒体服务器
    11. 盛最多水的容器
    jsonp是什么?原理是什么?
    Golang字符串分割、判断是否包含指定字符串
    java:springboot单元测试spring-boot-starter-test
    【Python 零基础入门】列表 & 字典
    基于opencv的缺陷检测
  • 原文地址:https://blog.csdn.net/weixin_60934893/article/details/128059859