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

相关推荐
小徐敲java12 分钟前
通用mybatis-plus查询封装(QueryGenerator)
mybatis
OEC小胖胖2 小时前
Spring Boot + MyBatis 项目中常用注解详解(万字长篇解读)
java·spring boot·后端·spring·mybatis·web
计算机学姐2 小时前
基于SpringBoot+Vue的在线投票系统
java·vue.js·spring boot·后端·学习·intellij-idea·mybatis
落落落sss2 小时前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis
海绵波波1075 小时前
Qt操作主/从视图及XML——实例:汽车管理系统
xml·qt·汽车
罗曼蒂克在消亡5 小时前
2.3MyBatis——插件机制
java·mybatis·源码学习
coderWangbuer5 小时前
基于springboot的高校招生系统(含源码+sql+视频导入教程+文档+PPT)
spring boot·后端·sql
数据龙傲天6 小时前
1688商品API接口:电商数据自动化的新引擎
java·大数据·sql·mysql
cyt涛7 小时前
MyBatis 学习总结
数据库·sql·学习·mysql·mybatis·jdbc·lombok
与衫8 小时前
掌握嵌套子查询:复杂 SQL 中 * 列的准确表列关系
android·javascript·sql