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

MyBatis XML 配置文件:从配置规范到 CRUD 开发实践 - 详解

个人主页:♡喜欢做梦

欢迎  点赞  ➕关注  ❤️收藏  评论


目录

前言

MyBatis XML配置文件的作用

配置数据库连接字符串和MyBatis

数据库配置

application.yml 配置内容

application.properties 配置内容

MyBatis配置

application.yml 配置内容

application.properties 配置内容

XML文件的核心组成成分

MyBatis的xml固定格式

文件创建

创建Mapper接口

创建xml文件

增删查改操作

增(Insert)

删除(delete)

查(select)

改(update)


前言

MyBatis的开发方式有两种:注解、XML。下来要将的就是XML,如果想要看MyBatis注解的,可以看我上一篇文章

MyBatis XML配置文件的作用

MyBatis是一款持久层框架,他支持将SQL语句与Java代码分离,XML文件就是用来编写SQL语句,配置结果映射(ResultMap)等信息载体,让代码等清晰、维护更方便。

配置数据库连接字符串和MyBatis

数据库配置

application.yml 配置内容

# 数据库连接配置
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=falseusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driver

application.properties 配置内容

#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false
#连接数据库的用户名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=root
  • datasource.url: 数据库连接地址;
  • datasource.username:数据库登入用户名(例root);
  • datasource.password: 数据库密码(例root);
  • datasource.driver-class-name:MySqL驱动类。

MyBatis配置

application.yml 配置内容

mybatis:# 配置 mybatis xml 的文件路径,在 resources/mapper 创建所有表的 xml 文件mapper-locations: classpath:mapper/**Mapper.xmlconfiguration: # 配置打印 MyBatis日志log-impl: org.apache.ibatis.logging.stdout.StdOutImplmap-underscore-to-camel-case: true #配置驼峰自动转换

application.properties 配置内容

# 配置 mybatis xml 的文件路径,在 resources/mapper 创建所有表的 xml 文件
mybatis.mapper-locations=classpath:mapper/**Mapper.xml
# 配置打印 MyBatis 日志
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
# 配置驼峰自动转换
mybatis.configuration.map-underscore-to-camel-case=true
  • mybatis.mapper-locations:指定映射文件(XML)位置。classpath:mapper/**Mapper.xml表示resource/mapper目录以下所有以Mapper.xml为结尾的文件;
  • mybatis.configuration.log-impl:配置MyBatis的日志实现;
  • mybatis.configuration.map-underscore-to-camel-case:开启驼峰命名。

XML文件的核心组成成分

以映射文件(UserInfoMapper.xml)为例

MyBatis的xml固定格式


  • 示例对应的Mapper接口的全限定类名:

文件创建

创建Mapper接口

@Mapper
public interface UserInfoXMLMapper {
}

创建xml文件

增删查改操作

增(Insert)

UserInfoMapper.xml

 insert into user_info(username,password,age) values (#{username},#{password},#{age})
  • <insert>的id必须与UserInfoXMLMapper中的insertUserInfo一致;
  • parameType:入参类型(可省略),如果要写,要写全类名。

UserInfoXMLMapper接口

@Mapper
public interface UserInfoXMLMapper {Integer insertUserInfo(UserInfo userInfo);
}

测试

@SpringBootTest
class UserInfoXMLMapperTest {@Autowiredprivate UserInfoXMLMapper userInfoXMLMapper;@Testvoid insertUserInfo() {UserInfo userInfo=new UserInfo();userInfo.setUsername("张三");userInfo.setPassword("123");userInfo.setAge(13);System.out.println(userInfoXMLMapper.insertUserInfo(userInfo));}
}

结果

添加自增主键

 insert into user_info(username,password,age) values (#{username},#{password},#{age})

删除(delete)

UserInfoMapper.xml

delete from user_info where username=(#{username})

UserInfoXMLMapper接口

Integer deleteUser(UserInfo userInfo);

测试

@Testvoid deleteUser() {UserInfo userInfo=new UserInfo();userInfo.setUsername("张三");userInfoXMLMapper.deleteUser(userInfo);}

结果

查(select)

UserInfoMapper.xml

    
  • resultType:返回值类型,如果缺少,那么参数占位符写法不规范。

UserInfoXMLMapper接口

UserInfo selectUserById(Integer id);

测试

@Testvoid selectUserById() {userInfoXMLMapper.selectUserById(1);}

结果

改(update)

UserInfoMapper.xml

Integer updateUser(String username,Integer id);

UserInfoXMLMapper接口

update user_info set username=#{username} where id=#{id}

测试

 @Testvoid updateUser() {userInfoXMLMapper.updateUser("lisi",2);}

结果

http://www.jsqmd.com/news/118969/

相关文章:

  • Excalidraw支持时间轴视图展示
  • Open-AutoGLM表情库构建核心机密,掌握这4个环节就赢在起跑线
  • Open-AutoGLM多用户环境搭建全解析,避开这6个常见坑位
  • Shell Daily 2025-12-21: 管道防隐患 (Pipefail)
  • 12.3 LoRA模型实战(一):快速上手图像风格定制
  • CF1810G The Maximum Prefix
  • Excalidraw与Keda弹性伸缩策略图解
  • 【深度拆解智能体技术底层逻辑】从架构到实现的完整解析
  • 还在手动配置Open-AutoGLM?掌握这7步自动化协作方案秒变专家
  • 高并发场景下等待时间失控?Open-AutoGLM动态调节机制来了,稳了!
  • 还在手动收集表情包?Open-AutoGLM智能采集系统让你领先同行3年
  • 从混乱到统一:Open-AutoGLM团队共享方案如何缩短80%协作成本
  • Verl 如何增加配置参数?
  • FCKEditor教学案例Word图片粘贴转存经验交流
  • 掌握这4个技巧,轻松实现Open-AutoGLM无缝版本切换
  • Excalidraw图形版本对比功能设想
  • Excalidraw与Nuxt.js服务端渲染适配
  • Excalidraw图形水印添加方法
  • Open-AutoGLM视频号推荐引擎解析(稀缺算法模型首次公开)
  • 2025年权威盘点:国内十大四通球阀制造厂家排行榜,行业内四通球阀有哪些优选实力品牌 - 品牌推荐师
  • Excalidraw支持网络拓扑自动发现
  • 2025年北京家庭搬家公司联系方式汇总: 核心城区专业服务商联系通道与高效搬迁指引 - 十大品牌推荐
  • Open-AutoGLM如何颠覆内容创作?:3步打造高转化朋友圈文案的AI秘技
  • 为什么顶级公司都在用Open-AutoGLM做智能回复?真相令人震惊
  • 2025年深圳小型搬家公司联系方式汇总: 本地资深企业官方联系方式与一站式搬迁指南 - 十大品牌推荐
  • Excalidraw支持二维码嵌入生成
  • 【Open-AutoGLM高效运维必修课】:从入门到精通的5个核心步骤
  • not every Es know geography/map
  • not every Es know geography/map
  • 2025年杭州管道疏通联系方式汇总:全市专业服务商官方联系渠道与高效合作指引 - 十大品牌推荐