Mybatis的XML实现方法

Mybatis的开发有两种方式:

1、注解

2、XML

使用Mybatis的注解方式,主要是来完成一些简单的增删改查功能。如果需要实现复杂的SQL功能,建议使用XML来配置映射语句,也就是将SQL语句写在XML配置文件中。

Mybatis的XML的实现需要以下两步:

1、配置数据库连接字符串和Mybatis

2、写持久层代码

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

此步骤需要进⾏两项设置,数据库连接字符串设置和 MyBatis 的 XML ⽂件配置。

如果是application.yml⽂件, 配置内容如下:

XML 复制代码
# 数据库连接配置
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
# 配置 mybatis xml 的⽂件路径,在 resources/mapper 创建所有表的 xml ⽂件
mybatis:
  mapper-locations: classpath:mapper/**Mapper.xml

如果是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
# 配置 mybatis xml 的⽂件路径,在 resources/mapper 创建所有表的 xml ⽂件
mybatis.mapper-locations=classpath:mapper/**Mapper.xml

写持久层代码

添加Mapper接口

功能为查询所有用户:

java 复制代码
import com.example.demo.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserInfoXMlMapper {
    List<UserInfo> queryAllUser();
}

添加 UserInfoXMLMapper.xml

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="com.example.demo.mapper.UserInfoXMlMapper">
   <select id="queryAllUser" resultType="com.example.demo.model.UserInfo">
       select username,`password`, age, gender, phone from userinfo
   </select>
</mapper>

对以上标签的说明:

<mapper> 标签:需要指定 namespace 属性,表⽰命名空间,值为 mapper 接⼝的全限定

名,包括全包名.类名。

• <select> 查询标签:是⽤来执⾏数据库的查询操作的:

◦ id :是和 Interface (接⼝)中定义的⽅法名称⼀样的,表⽰对接⼝的具体实现⽅法。

◦ resultType :是返回的数据类型,也就是开头我们定义的实体类.

返回⾃增 id

接⼝定义不变, Mapper.xml 实现 设置useGeneratedKeys 和keyProperty属性。

XML 复制代码
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
       insert into userinfo (username, `password`, age, gender, phone) values
       (#{userinfo.username},#{userinfo.password},#{userinfo.age},#
       {userinfo.gender},#{userinfo.phone})
</insert>

:使⽤XML 的⽅式进⾏查询, 也存在数据封装的问题。

如:

XML 复制代码
<select id="queryAllUser" resultType="com.example.demo.model.UserInfo">
    select id, username,`password`, age, gender, phone, delete_flag,
    create_time, update_time from userinfo
</select>

查询结果:

接下来看下xml如何来写结果映射:

定义resultMap:

XML 复制代码
<resultMap id="BaseMap" type="com.example.demo.model.UserInfo">
     <id column="id" property="id"></id>
     <result column="delete_flag" property="deleteFlag"></result>
     <result column="create_time" property="createTime"></result>
      <result column="update_time" property="updateTime"></result>
</resultMap>
<select id="queryAllUser" resultMap="BaseMap">
     select id, username,`password`, age, gender, phone, delete_flag,
      create_time, update_time from userinfo
</select>

以上,关于Mybatis希望对你有所帮助。

相关推荐
野生技术架构师7 分钟前
2025年Java面试八股文大全(附PDF版)
java·面试·pdf
Coder_Boy_8 分钟前
SpringAI与LangChain4j的智能应用-(实践篇4)
java·人工智能·spring boot·langchain
CC.GG9 分钟前
【Qt】常用控件----QWidget属性
java·数据库·qt
资生算法程序员_畅想家_剑魔26 分钟前
Java常见技术分享-13-多线程安全-锁机制-底层核心实现机制
java·开发语言
萤丰信息29 分钟前
数智重构生态:智慧园区引领城市高质量发展新范式
java·大数据·人工智能·安全·智慧城市
悟空码字35 分钟前
MySQL分库分表,从“一室一厅”到“豪华别墅区”的数据库升级之旅
java·后端·mysql
Lisonseekpan36 分钟前
RBAC 基于角色的访问控制模型详解与实践指南
java·服务器·网络·后端·spring·log4j
奔跑的小十一1 小时前
ShardingSphere-JDBC 开发手册
java·数据库
lkbhua莱克瓦241 小时前
基础-MySQL概述
java·开发语言·数据库·笔记·mysql