当前位置: 首页 > article >正文

SpringBoot 整合 Mybatis:注解版

第一章:注解版

  1. 导入配置:
    <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.1</version>
    </dependency>
  2. 步骤:
    1. 配置数据源见 Druid 配置
    2. 创建表
    3. 创建实体类:
      public class Txperson {
      
          public class TxPerson {
      
      
              private int pid;
      
              private String pname;
      
              private String addr;
      
              private int gender;
      
              private Date birth;
      
          }
      }
    4. 创建 Mapper 层:
      @Mapper
      public interface TxPersonMapper {
      
      
          @Select("select * from tx_person")
          public List<TxPerson> getPersons();
      
      
          @Select("select * from tx_person t where t.pid = #{id}")
          public TxPerson getPersonById(int id);
      
          @Options(useGeneratedKeys =true, keyProperty = "pid")
          @Insert("insert into tx_person(pid, pname, addr,gender, birth)" +
                  " values(#{pid}, #{pname}, #{addr},#{gender}, #{birth})")
          public void insert(TxPerson person);
      
          @Delete("delete from tx_person where pid = #{id}")
          public void update(int id);
      
      }
    5. 编写配置类解决驼峰模式和数据库中下划线不能映射的问题
      @Configuration
      public class MybatisConfig {
          @Bean
          public ConfigurationCustomizer getCustomizer(){
              return new ConfigurationCustomizer() {
                  @Override
                  public void customize(org.apache.ibatis.session.Configuration configuration) {
                      configuration.setMapUnderscoreToCamelCase(true);
                  }
              };
          }
      }
      
    6. 进行测试:

第二章:SpringBoot 整合 MyBatis 配置文件

  1. 创建 sqlMapConfig.xml 配置文件
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
         "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
    <configuration>
    
    </configuration>
  2. 映射文件 PersonMapper.xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="cn.tx.mapper.TxPersonMapper">
        <select id="getPersons" resultType="TxPerson">
            select * from tx_person
        </select>
    </mapper>

  3. 在 application.yaml 中配置 mybatis 的信息
    mybatis:
      config-location: classpath:mybatis/sqlMapConfig.xml
      mapper-locations: classpath:mybatis/mapper/*.xml
      type-aliases-package: cn.tx.springboot.jdbc_demo1

http://www.kler.cn/a/534607.html

相关文章:

  • 何为运行时(Runtime)
  • 【优先算法】专题——位运算
  • Java进阶(JVM调优)——阿里云的Arthas的使用 安装和使用 死锁查找案例,重新加载案例,慢调用分析
  • vue2-为啥data属性是一个函数而不是对象
  • 简单本地部署deepseek(软件版)
  • RabbitMQ深度探索:消息幂等性问题
  • 深度学习|表示学习|卷积神经网络|NIN 相比普通 CNN|17
  • mysql慢查询工具explain
  • MySQL 的 binlog 作用
  • 11.10 LangChain对话记忆管理实战:从入门到生产级ConversationBufferMemory应用指南
  • 查看设备uuid
  • 【EdgeAI实战】(2)STM32 AI 扩展包的安装与使用
  • 【技术追踪】DiffMIC:用于医学图像分类的双引导扩散网络(MICCAI-2024)
  • 了解linux-5.4.31/drivers/gpio/gpiolib-devres.c中的devm_gpiod_get_optional()函数
  • MD5 简介 以及 C# 和 js 实现【加密知多少系列_1】
  • 新版AndroidStudio 修改 jdk版本
  • 8. k8s二进制集群之Kubectl部署
  • http状态码:504 Gateway Timeout(网关超时)的原有以及排查问题的思路
  • 【Uniapp-Vue3】创建DB schema数据表结构
  • VMware下Linux和macOS遇到的一些问题总结
  • android 自定义通话录音
  • 【PostgreSQL内核学习 —— (WindowAgg(二))】
  • Docker 国内最新可用镜像源20250205
  • Solidity08 Solidity 函数
  • 机器学习8-卷积和卷积核1
  • 【AI 语音】实时语音交互优化全解析:从 RTC 技术到双讲处理