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

相关推荐
猿究院_夏燊2 小时前
Spring Bean 生命周期
java·后端·spring
Wang's Blog3 小时前
Java 项目实战: 交友项目:登录链路回顾与今日任务地图
java·开发语言·状态模式·交友
信誓旦旦的程序猿3 小时前
【Python量化实战宝典 #08】可转债与债券接口手册:转股价·溢价率·纯债价值(Python 可跑)
java·python·股票数据api·股票数据api接口·股票api数据接口·股票量化数据api·股票量化数据接口
砚底藏山河3 小时前
python量化:行情数据回溯
java·开发语言·python·金融·maven
Jlzn88883 小时前
华东电芯分选通道数怎么配才不堵线
java·人工智能·架构·自动化·制造
香吧香4 小时前
GRPC连接机制---长连接分析
java·网络与传输协议
我是人✓4 小时前
SpringBoot项目的五种创建方式
java·spring boot·后端
Wang's Blog4 小时前
Java 项目实战: 交友项目 - 阿里云 OSS 入门案例:从创建 Bucket 到上传拿到可访问 URL
java·阿里云·交友
裕晟资质规划5 小时前
三类涉密资质延续时限差异分析:军工60日与集成印制3个月的边界
java·服务器·前端·网络·安全
念念清晰5 小时前
000-OpenJDK17 源码构建:MacOS环境(M芯片)
java·后端