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

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 {@Beanpublic ConfigurationCustomizer getCustomizer(){return new ConfigurationCustomizer() {@Overridepublic 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 configurationPUBLIC "-//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.xmlmapper-locations: classpath:mybatis/mapper/*.xmltype-aliases-package: cn.tx.springboot.jdbc_demo1
http://www.lryc.cn/news/531610.html

相关文章:

  • 18.[前端开发]Day18-王者荣耀项目实战(一)
  • Kafka 使用说明(kafka官方文档中文)
  • 基于多智能体强化学习的医疗AI中RAG系统程序架构优化研究
  • Airflow:深入理解Apache Airflow Task
  • multisim入门学习设计电路
  • 【算法精练】二分查找算法总结
  • 从零开始实现一个双向循环链表:C语言实战
  • MYSQL面试题总结(题目来源JavaGuide)
  • visual studio安装
  • JVM执行引擎
  • C# 9.0记录类型:解锁开发效率的魔法密码
  • 搭建自己的专属AI——使用Ollama+AnythingLLM+Python实现DeepSeek本地部署
  • 『 C++ 』中理解回调类型在 C++ 中的使用方式。
  • git多人协作
  • CTFSHOW-WEB入门-命令执行71-77
  • 浅谈《图解HTTP》
  • LLMs瞬间获得视觉与听觉感知,无需专门训练:Meta的创新——在图像、音频和视频任务上实现最优性能。
  • 自研有限元软件与ANSYS精度对比-Bar3D2Node三维杆单元模型-央视大裤衩实例
  • kubernetes 高可用集群搭建
  • 【C++】STL——vector底层实现
  • 数据结构初探:链表之单链表篇
  • 介绍一下Mybatis的底层原理(包括一二级缓存)
  • Linux基础 ——tmux vim 以及基本的shell语法
  • 64位的谷歌浏览器Chrome/Google Chrome
  • jetson编译torchvision出现 No such file or directory: ‘:/usr/local/cuda/bin/nvcc‘
  • 多线程创建方式三:实现Callable接口
  • Linux下的编辑器 —— vim
  • Docker技术相关学习二
  • 【人工智能】多模态学习在Python中的应用:结合图像与文本数据的深度探索
  • 【MySQL】常用语句