• 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

     

     

  • 相关阅读:
    2022 需求工程综合论述题【太原理工大学】
    PHP数据类型和运算符
    【计算机毕设经典案例】基于微信小程序的图书管理系统
    大厂光环下的功能测试,出去面试自动化一问三不知
    K8S | Config应用配置
    【excel密码】为什么工作表不能移动、复制了?
    2021-07-15-2021年全球10大最佳单板计算机开发板(SBC)(第1-3名)
    各种实用航测遥感数据数据免费获取,速来领取!
    前端面试怎么总问watch和computed区别
    文本相似度算法对比分析,短文本相似度主流算法
  • 原文地址:https://blog.csdn.net/weixin_60934893/article/details/128059859