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

相关推荐
YA3334 小时前
java基础(九)sql基础及索引
java·开发语言·sql
码出未来8577 小时前
浅谈DDL、DSL、DCL、DML、DQL
sql
AI 嗯啦7 小时前
SQL详细语法教程(四)约束和多表查询
数据库·人工智能·sql
阿里云大数据AI技术7 小时前
【跨国数仓迁移最佳实践6】MaxCompute SQL语法及函数功能增强,10万条SQL转写顺利迁移
python·sql
柯南二号9 小时前
【Java后端】MyBatis-Plus 原理解析
java·开发语言·mybatis
Easocen10 小时前
Mybatis学习笔记(五)
笔记·学习·mybatis
喂完待续14 小时前
【Tech Arch】Hive技术解析:大数据仓库的SQL桥梁
大数据·数据仓库·hive·hadoop·sql·apache
路多辛15 小时前
Golang database/sql 包深度解析(二):连接池实现原理
数据库·sql·golang
阿华的代码王国15 小时前
【Android】适配器与外部事件的交互
android·xml·java·前端·后端·交互
qq_三哥啊16 小时前
【IDEA】设置Debug调试时调试器不进入特定类(Spring框架、Mybatis框架)
spring·intellij-idea·mybatis