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

个人主页♡喜欢做梦

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


目录

🌅前言

[🌅MyBatis XML配置文件的作用](#🌅MyBatis XML配置文件的作用)

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

🌄数据库配置

[application.yml 配置内容](#application.yml 配置内容)

[application.properties 配置内容](#application.properties 配置内容)

🌄MyBatis配置

[application.yml 配置内容](#application.yml 配置内容)

[application.properties 配置内容](#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 配置内容

javascript 复制代码
# 数据库连接配置
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

application.properties 配置内容

XML 复制代码
#驱动类名称
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 配置内容

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

application.properties 配置内容

XML 复制代码
# 配置 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固定格式

javascript 复制代码
<?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="对应的Mapper接口的全限定类名">
    
</mapper>
  • 示例对应的Mapper接口的全限定类名:
javascript 复制代码
<mapper namespace="com.mybatis.demo.mapper.UserInfoXMLMapper">
      <!-- SQL语句   -->
</mapper>

🌄文件创建

创建Mapper接口

java 复制代码
@Mapper
public interface UserInfoXMLMapper {
}

创建xml文件

🌅增删查改操作

🌄增(Insert)

UserInfoMapper.xml

XML 复制代码
 <insert id="insertUserInfo" parameterType="com.mybatis.demo.model.UserInfo">
        insert into user_info(username,password,age) values (#{username},#{password},#{age})
    </insert>
  • <insert>的id必须与UserInfoXMLMapper中的insertUserInfo一致;
  • parameType:入参类型(可省略),如果要写,要写全类名。

UserInfoXMLMapper接口

javascript 复制代码
@Mapper
public interface UserInfoXMLMapper {
       Integer insertUserInfo(UserInfo userInfo);
}

测试

javascript 复制代码
@SpringBootTest
class UserInfoXMLMapperTest {
     @Autowired
     private UserInfoXMLMapper userInfoXMLMapper;
    @Test
    void insertUserInfo() {
        UserInfo userInfo=new UserInfo();
        userInfo.setUsername("张三");
        userInfo.setPassword("123");
        userInfo.setAge(13);

        System.out.println(userInfoXMLMapper.insertUserInfo(userInfo));
    }
}

结果

添加自增主键

javascript 复制代码
 </insert>
    <insert id="insertUserInfo2" parameterType="com.mybatis.demo.model.UserInfo" useGeneratedKeys="true" keyProperty="id">
        insert into user_info(username,password,age) values (#{username},#{password},#{age})
    </insert>

🌄删除(delete)

UserInfoMapper.xml

javascript 复制代码
<delete id="deleteUser">
        delete from user_info where username=(#{username})
    </delete>

UserInfoXMLMapper接口

javascript 复制代码
Integer deleteUser(UserInfo userInfo);

测试

javascript 复制代码
@Test
    void deleteUser() {
        UserInfo userInfo=new UserInfo();
        userInfo.setUsername("张三");
        userInfoXMLMapper.deleteUser(userInfo);
    }

结果

🌄查(select)

UserInfoMapper.xml

javascript 复制代码
    <select id="selectUserById" resultType="com.mybatis.demo.model.UserInfo">
        select username,password,age,phone from user_info where id=(#{id})
    </select>
  • resultType:返回值类型,如果缺少,那么参数占位符写法不规范。

UserInfoXMLMapper接口

javascript 复制代码
UserInfo selectUserById(Integer id);

测试

javascript 复制代码
@Test
    void selectUserById() {
        userInfoXMLMapper.selectUserById(1);
    }

结果

🌄改(update)

UserInfoMapper.xml

javascript 复制代码
Integer updateUser(String username,Integer id);

UserInfoXMLMapper接口

javascript 复制代码
<update id="updateUser">
        update user_info set username=#{username} where id=#{id}
    </update>

测试

javascript 复制代码
 @Test
    void updateUser() {
        userInfoXMLMapper.updateUser("lisi",2);
    }

结果

相关推荐
Wang's Blog4 分钟前
Java 接入Redis: 服务启动停止与后台运行配置
java·服务器·redis
此时不提桶,更待何时20 分钟前
01-04-B-垃圾回收面试与生产事故实战
java·jvm·面试
鹿角片ljp2 小时前
KV Cache 解析
java·算法
captain3763 小时前
网络原理(9)-数据链路层
java·网络协议·java-ee
惊讶的猫3 小时前
意图识别置信度和检索重排的分数
java
caoerzhong3 小时前
仓库管理数字化趋势解读:JeeWMS 开源 Java WMS 视角下的五个演进方向
java·开源
程序员JerrySUN4 小时前
Jetson Edge AI 实战01:Nano、Xavier、Orin 怎么选?Jetson 硬件选型详解【视频讲解】
java·数据库·redis·安全·mybatis
Joy T5 小时前
Spring AI 2.0 Agent 进阶:Memory、State 与 Context Engineering 常见技术全景
java·人工智能·后端·spring·agent入门·agent state
估值探索者5 小时前
【Python量化系统工程化 #08】关了 SSH 就停?systemd 让脚本开机自启 + 异常自动拉起
java·c++·人工智能·分类·数据挖掘