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标签中的内容引进来即可。

相关推荐
Sagittarius_A*26 分钟前
【好靶场】SQL注入-时间盲注
数据库·sql
崖边看雾2 小时前
MySQL——SQL 经典练习题:学生选课成绩查询(附详细注释)
大数据·数据库·sql·mysql
2601_960356383 小时前
库存分析岗位秋招准备:SQL、Excel、WMS与供应链指标
数据库·sql·excel
山峰哥4 小时前
造价算量系统SQL优化,慢查询从9秒压到35毫秒‌
大数据·数据库·sql·编辑器·深度优先
StarRocks_labs4 小时前
StarRocks 如何查询 Paimon 半结构化数据?Variant、Shredding 与 SQL 实践
starrocks·sql·paimon·variant·shredding
计算机学姐5 小时前
基于SpringBoot的高校爱心慈善管理系统
java·vue.js·spring boot·后端·spring·tomcat·mybatis
大梦想家a6 小时前
基于 Spring Boot 的物流运单管理系统后端设计与实现(JWT + MyBatis-Plus + MySQL)
spring boot·mysql·mybatis
姜太小白1 天前
【Oracle】记排查sh脚本调用SQL执行卡顿的排查总结
数据库·sql·oracle
镜舟科技1 天前
为什么 Text-to-SQL 总是停在 Demo?
数据库·sql·demo·text-to-sql·镜舟科技·mip·语义视图
大牧师1 天前
TypeORM 学习教程
数据库·sql·学习·node.js·orm·nest.js·typeorm