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

相关推荐
AskHarries4 分钟前
Spring Boot集成Akka Cluster快速入门Demo
java·spring boot·后端·akka
2402_857589364 分钟前
Java免税商品购物商城:Spring Boot实现详解
java·开发语言·spring boot
Armyyyyy丶14 分钟前
spring-boot-maven-plugin插件打包和java -jar命令执行原理
java·maven·插件打包
罗政33 分钟前
[附源码]SpringBoot+VUE+Java实现人脸识别系统
java·vue.js·spring boot
-XWB-39 分钟前
【Java】将一个List拆分使用线程池多线程运行
java·windows·list
陈大爷(有低保)1 小时前
题目:基于TCP/IP用DOM4j修改XML
java·服务器·tcp/ip
m0_738755921 小时前
.ideavimrc在idea打不开
java·vim·intellij-idea
超级小的大杯柠檬水1 小时前
IDEA中实现springboot热部署
java·spring boot·intellij-idea
攀小黑1 小时前
IDEA 24.1 could not autowire. No beans of ‘***‘ type found. 取消 某个类型的 警告
java·ide·intellij-idea
腊笔不小新xingo1 小时前
READONLY You can‘t write against a read only replica
java·redis·bootstrap