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希望对你有所帮助。

相关推荐
427724001 小时前
IDEA使用git不提示账号密码登录,而是输入token问题解决
java·git·intellij-idea
chengooooooo2 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
李长渊哦2 小时前
常用的 JVM 参数:配置与优化指南
java·jvm
计算机小白一个2 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
南宫生4 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
计算机毕设定制辅导-无忧学长5 小时前
Maven 基础环境搭建与配置(一)
java·maven
天上掉下来个程小白6 小时前
案例-14.文件上传-简介
数据库·spring boot·后端·mybatis·状态模式
风与沙的较量丶6 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言
m0_748251726 小时前
SpringBoot3 升级介绍
java
极客先躯7 小时前
说说高级java每日一道面试题-2025年2月13日-数据库篇-请说说 MySQL 数据库的锁 ?
java·数据库·mysql·数据库的锁·模式分·粒度分·属性分