• SpringBoot 整合Mybatis


    一、整合Mybatis

    • 创建项目时勾选Mybatis技术,导入对应starter;

    • 在配置文件中配置数据源信息;

    • mapper类添加@Mapper才能被识别。

    1、创建新模块,选择需要的技术集:

    2、在application.yml 中配置数据源参数:

    1. # 配置数据源信息
    2. spring:
    3. datasource:
    4. driver-class-name: com.mysql.cj.jdbc.Driver
    5. url: jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
    6. username: root
    7. password: mypassword

    3、创建mapper接口:

    不同于传统Mybatis,SpringBoot无法在applicationContext.xml中实例化mapper接口,而需要用@mapper 声明接口

    1. @Mapper
    2. public interface BookDao {
    3. @Select("select * from boot_book where id = #{id}")
    4. public Book getById(Integer id);
    5. }

    4、测试

    1. @SpringBootTest
    2. class MybatisApplicationTests {
    3. @Autowired
    4. private BookDao bookDao;
    5. @Test
    6. void contextLoads() {
    7. System.out.println(bookDao.getById(1));
    8. }
    9. }

    二、整合Mybatis-Plus

    • 手工添加MyBatis-Plus对应的starter;

    • 配置数据源;

    • mapper接口继承BaseMapper简化开发;

    • 需要使用的第三方技术无法通过勾选确定时,需要手工添加坐标。

    1、创建新模块,但SpringBoot并没有提供官方的MP技术集:

    2、故需要手动添加starter依赖 :

    1. <dependency>
    2. <groupId>com.baomidougroupId>
    3. <artifactId>mybatis-plus-boot-starterartifactId>
    4. <version>3.5.1version>
    5. dependency>

    3、数据源参数与Mybatis相同;

    4、创建mapper接口:

    1. @Mapper
    2. public interface BookDao extends BaseMapper {
    3. }

    5、测试:

    1. @SpringBootTest
    2. class MybatisPlusApplicationTests {
    3. @Autowired
    4. private BookDao bookDao;
    5. @Test
    6. void contextLoads() {
    7. System.out.println(bookDao.selectById(2));
    8. }
    9. }

    三、整合Druid连接池

    1、导入starter:

    1. <dependency>
    2. <groupId>com.alibabagroupId>
    3. <artifactId>druid-spring-boot-starterartifactId>
    4. <version>1.2.6version>
    5. dependency>

    2、配置数据源:

    1. # 配置数据源信息
    2. #spring:
    3. # datasource:
    4. # driver-class-name: com.mysql.cj.jdbc.Driver
    5. # url: jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
    6. # username: root
    7. # password: mypassword
    8. # type: com.alibaba.druid.pool.DruidDataSource
    9. spring:
    10. datasource:
    11. druid:
    12. driver-class-name: com.mysql.cj.jdbc.Driver
    13. url: jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
    14. username: root
    15. password: mypassword

    其他相关

    pojo:

    1. @TableName("boot_book")
    2. public class Book {
    3. private Integer id;
    4. private String type;
    5. private String name;
    6. private String description;
    7. @Override
    8. public String toString() {
    9. return "Book{" +
    10. "id=" + id +
    11. ", type='" + type + '\'' +
    12. ", name='" + name + '\'' +
    13. ", description='" + description + '\'' +
    14. '}';
    15. }
    16. public Integer getId() {
    17. return id;
    18. }
    19. public void setId(Integer id) {
    20. this.id = id;
    21. }
    22. public String getType() {
    23. return type;
    24. }
    25. public void setType(String type) {
    26. this.type = type;
    27. }
    28. public String getName() {
    29. return name;
    30. }
    31. public void setName(String name) {
    32. this.name = name;
    33. }
    34. public String getDescription() {
    35. return description;
    36. }
    37. public void setDescription(String description) {
    38. this.description = description;
    39. }
    40. }

  • 相关阅读:
    Git学习笔记 - Git基本操作和GitHub
    《Head First HTML5 javascript》第1章 交互式网络 知识点总结
    0成本低代码入门指南,国产化+私有化的开源低代码平台如何获取?
    编制项目预算的方法和步骤
    【owt-server】RTC视频接收调用流程学习笔记1: Call::CreateVideoReceiveStream 前后
    MATLAB基础应用精讲-【数模应用】事后多重比较
    147. SAP UI5 SmartTable 控件的使用介绍
    二、CSS自制浏览器滑动条
    面试官说入职以后要参与重构Kafka内核,我吓的不敢接offer!
    作业-11.29
  • 原文地址:https://blog.csdn.net/qq_57389269/article/details/126146929