
目录一、MyBatis XML 配置文件1.1 配置连接字符串和 MyBatis1.2 添加 Mapper 接口1.3 XML 的配置1.4 使用技巧二、CRUD 具体实现2.1 增加Insert2.2 删除Delete2.4 查询Select在 Spring Boot 项目中MyBatis 有两种常见的开发方式注解和XML。注解适合简单的 SQL而 XML 更适合编写复杂的动态 SQL。本文将带你从零开始完整梳理 MyBatis XML 的开发流程涵盖配置、Mapper 接口编写、XML 映射文件以及增删改查的具体实现。一、MyBatis XML 配置文件MyBatis 的开发有两种方式注解和XML。本文重点讲解 XML 方式。1.1 配置连接字符串和 MyBatis这一步需要进行两项设置数据库连接字符串和MyBatis 的 XML 文件配置。方式一application.propertiesspring.datasource.urljdbc:mysql://127.0.0.1:3306/lottery_system?characterEncodingutf8useSSLfalse # 连接数据库的⽤⼾名 spring.datasource.usernameroot # 连接数据库的密码 spring.datasource.password123456 ## MyBatis ## # 配置驼峰⾃动转换 mybatis.configuration.map-underscore-to-camel-casetrue # 配置mybatis xml的⽂件路径在 resources/mapper 创建所有表的 xml ⽂件 mybatis.mapper-locationsclasspath:mapper/*.xml方式二application.yml# 数据库连接配置 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncodingutf8useSSLfalse username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver # 配置 mybatis xml 的⽂件路径在 resources/mapper 创建所有表的 xml ⽂件 mybatis: mapper-locations: classpath:mapper/**Mapper.xml1.2 添加 Mapper 接口创建一个实体接口并加上Mapper 接口声明是数据库的接口Mapper public interface BookInfoMapper { Integer updateBook(Param(bookInfo) BookInfo bookInfo); }1.3 XML 的配置给 xml 开头添加配置文件只需要修改里面 namespace 里面的路径改为和自己 Mapper 声明的接口的路径就可以:?xml version1.0 encodingUTF-8? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom_yuyuan_book.mapper.BookInfoMapper /mapper1.4 使用技巧直接在接口使用 alt enter 调出 Generate statement然后点击 Generate statement会跳出 4 个选项自己选择自己要写的类型就好是修改还是查询还是删除还是插入。我选择 update Statement 在对应的 xml 文件里面就会有这一行就可以写入我们的数据库语句还有我们的动态sql了。二、CRUD 具体实现2.1 增加InsertUserInfoMapper 接口Integer insertUser(UserInfo userInfo);UserInfoMapper.xml实现insert idinsertUser insert into userinfo (username, password, age, gender, phone) values (#{username}, #{password}, #{age},#{gender},#{phone}) /insert也可以使用 Param 设置参数名称是放在 Mapper 接口下的函数的参数那里Integer insertUser(Param(userInfo) UserInfo userInfo);返回自增 id返回定义不变Mapper.xml实现设置 userGeneratedKeys 和 keyProperty 属性useGeneratedKeystrue 作用开启获取数据库自增主键keyPropertyid作用拿到主键赋值给实体类哪个属性。insert idinsertUser useGeneratedKeystrue keyPropertyid insert into user_info (username, password, age, gender, phone) values (#{userInfo.username},#{userInfo.password},#{userInfo.age},# {userInfo.gender},#{userInfo.phone}) /insert2.2 删除DeleteUserInfoMapper 接口Integer updateUser(UserInfo userInfo);UserInfoMapper.xml 实现update idupdateUser update user_info set username#{username} where id#{id} /update2.4 查询Select使用 XML 的方式查询也存在那三个问题的解决方法起别名结果映射开启驼峰自动转换1. 起别名的解决方法select idqueryAllUser resultMapBaseMap select id, username,password, age, gender, phone, delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from user_info /select2. 结果映射Mapper.xmlresultMap idBaseMap typecom.example.demo.model.UserInfo id columnid propertyid/id result columndelete_flag propertydeleteFlag/result result columncreate_time propertycreateTime/result result columnupdate_time propertyupdateTime/result /resultMap select idqueryAllUser resultMapBaseMap select id, username,password, age, gender, phone, delete_flag, create_time, update_time from user_info /select