MyBatis中的XML实现和动态SQL实现

文章目录

一、XML实现

先在新建的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.UserInfoMapper">

</mapper>

再在mapper标签里写入操作数据库的增删查改。

1.1增

mapper层声明的方法为:

java 复制代码
Integer insert(UserInfo userInfo);

XML文件中的实现为:

xml 复制代码
<insert id = "insert">
    insert into
    userinfo
    (username, password, age, gender, phone)
    values
    (#{username}, #{password}, #{age}, #{gender}, #{phone})
</insert> 

1.2删

mapper层声明的方法为:

java 复制代码
Integer delete(Integer id);

XML文件中的实现为:

xml 复制代码
<delete id="delete">
    delete from userinfo where id = #{id}
</delete>

1.3查

mapper层声明的方法为:

java 复制代码
List<UserInfo> queryUserList();

XML文件中的实现为:

xml 复制代码
<select id="queryUserList" resultType="com.example.demo.model.UserInfo">
    select * from userinfo
</select>

1.4改

mapper层声明的方法为:

java 复制代码
Integer update(UserInfo userInfo);

XML文件中的实现为:

xml 复制代码
<update id="update">
    update userinfo
    set password = #{password}
    where id = #{id}
</update>

二、XML方式实现动态SQL

2.1if标签

使用示例:

xml 复制代码
<update id = "updateBook">
    update book_info
    <set>
        <if test = "bookName != null">
            book_name = #{bookName},
        </if>
        <if test = "author != null">
            author = #{author},
        </if>
        <if test = "count != null">
            count = #{count},
        </if>
        <if test = "price != null">
            price = #{price},
        </if>
        <if test = "publish != null">
            publish = #{publish},
        </if>
        <if test = "status != null">
            status = #{status},
        </if>
    </set>
    where id = #{id}
</update>

如果满足bookName!=null这个条件,则会显示if标签里的内容。

2.2trim标签

使用示例:

xml 复制代码
<insert id="insert2" useGeneratedKeys="true" keyProperty="id">
    insert into
    userinfo
    <trim prefixOverrides="," prefix="(" suffix=")" suffixOverrides=",">
        <if test="username!=null">
            username,
        </if>
        <if test="password!=null">
            password,
        </if>
        <if test="age!=null">
            age,
        </if>
        <if test="gender!=null">
            gender,
        </if>
        <if test="phone!=null">
            phone,
        </if>
    </trim>
    values
    <trim prefixOverrides="," prefix="(" suffix=")" suffixOverrides=",">
        <if test="username!=null"> 
            #{username},
        </if>
        <if test="password!=null"> 
            #{password},
        </if>
        <if test="age!=null">
            #{age},
        </if>
        <if test="gender!=null">
            #{gender},
        </if>
        <if test="phone!=null">
            #{phone},
        </if>
     </trim>
</insert>

2.3where标签

使用示例:

xml 复制代码
<select id="queryUserByWhere" resultType="com.yixing.mybatis.model.UserInfo">
    select * from userinfo
    <where>
        <if test="userName!=null">
            username= #{userName}
        </if>
        <if test="age!=null">
            and age=#{age}
        </if>
    </where>
</select>

where标签的作用是删除代码块最前面的and;当查询条件为空时,会去掉where关键字。

2.4set标签

使用示例:

xml 复制代码
<update id="update2">
    update userinfo
    <set>
        <if test="username!=null">
            username = #{username},
        </if>
        <if test="password!=null">
            password = #{password},
        </if>
        <if test="age!=null">
            age = #{age}
        </if>
    </set>
    where id = #{id}
</update>

set标签会删除代码块最后面的逗号。

2.5foreach标签

使用示例:

xml 复制代码
<update id="batchDelete">
    update book_info
    set `status` = 0
    where id in
    <foreach collection="ids" open="(" close=")" separator="," item="id">
        #{id}
    </foreach>
</update>

默认情况下,如果mapper层声明方法的参数是List类型,则foreach标签里的collection会等于"list";如果mapper层声明方法的参数是数组类型,则foreach标签里的collection会等于"array",这时mybatis自动做的。我们可以在mapper层声明方法中用@Param注解对声明方法的参数进行重命名。

2.6include标签和sql标签

xml 复制代码
<sql id="cols">
    id, username,password,gender,age,phone,
</sql>
<select id="queryUserList" resultType="com.yixing.mybatis.model.UserInfo">
    select
    <include refid="cols"></include>
    delete_flag,
    create_time,
    update_time
    from userinfo
</select>

我们可以将XML中重复出现的内容提取出来放到sql标签中,当需要用到sql标签中的内容时,用include标签将sql标签中的内容引进来即可。

相关推荐
泡海椒2 小时前
从 XML 到 Excel:JQuick-Excel 代理与解析器如何协作
xml·excel
roman_日积跬步-终至千里3 小时前
【数据Agent论文·查询与语义|11】EntSQL:给了业务文档,模型为何还是写不对SQL?
数据库·人工智能·sql·数据分析
毕业设计7034 小时前
(免费领源码)SSM董家湾线上点餐系统04682- java、PHP、python、C#、小程序、大数据、单片机、网络工程等)
java·redis·mysql·tomcat·maven·mybatis·ssm
l1t5 小时前
修改两处,让SQLite求解数独SQL在datafusion上运行
服务器·数据库·sql
roman_日积跬步-终至千里6 小时前
【数据Agent论文·查询与语义|09】Spider 2.0:从生成SQL走向完成企业工作流
数据库·sql·数据分析
曦夜日长6 小时前
MySQL系统篇,数据库基础(二):数据库概念、SQL 分类与存储引擎
数据库·sql·mysql
泡海椒6 小时前
JQuick-SQL AST 解析:从 SQL 文本到强类型节点
数据库·sql
泡海椒7 小时前
JQuick-SQL 模块架构:从 SQL AST 到分布式执行
分布式·sql·架构
l1t7 小时前
duckdb、clickhouse、datafusion、cedardb、tableauhyperapi 5种分析型数据库执行递归求数独SQL比较
数据库·sql·clickhouse
泡海椒7 小时前
JQuick-SQL Worker 生命周期:端口、启动与关闭规范
数据库·sql