mapper.xml特殊SQL语句汇总(持续更新中)

1.通用查询结果列

<sql id="Base_Column_List">
    user_id, user_name, user_password
</sql>
用法:
<select id="selectUser" resultType="com.example.YourUser">
    SELECT
    <include refid="Base_Column_List"/>
	FROM your_table_name
</select>

2.动态sql语句更新表

//mapper:
int updateUser(List<UserPo> userList);
<!--不需要进行条件判断时-->
    <update id="updateUser">
        <foreach collection="list" separator=";" item="item" index="index">
            update user_table set
            user_password=#{item.userPassword}, user_name=#{item.userName}, user_level=#{item.userLevel} where id=#{item.id}
        </foreach>
    </update>
<!--需要进行条件判断时-->
    <update id="updateUser">
        <foreach collection="list" separator=";" item="item" index="index">
            update user_table
            <set>
                <if test="item.userPassword!=null">user_password=#{item.userPassword},</if>
                <if test="item.userName!=null">user_name=#{item.userName},</if>
                <if test="item.userLevel!=null">user_level=#{item.userLevel},</if>
            </set>
            where id=#{item.id}
        </foreach>
    </update>
<update id="updateUser">: 定义了一个 id 为 updateUser 的更新操作。

<foreach collection="list" separator=";" item="item" index="index">: 这里使用了 MyBatis 的 <foreach> 标签,它会遍历集合 list 中的元素,并对每个元素执行一次 SQL 操作。separator=";" 表示在每次循环之间用分号分隔,item="item" 和 index="index" 分别定义了在循环过程中当前元素和索引的别名。

<set>: 这里表示设置需要更新的字段部分。

<if test="item.userPassword!=null">user_password=#{item.userPassword},</if>: 使用 MyBatis 的 <if> 标签来进行条件判断,如果 item 对象中的 userPassword 字段不为 null,则将 user_password 字段赋值为 item.userPassword。

where id=#{item.id}: 这部分指定了更新操作的条件,根据 item 对象中的 id 字段的值来确定更新哪条具体记录。

3.动态sql语句新增表

//mapper:
int insertUser(List<UserPo> userList);
    <insert id="insertUser" parameterType="com.bai.bean.req.UserReq"
            keyProperty="id" useGeneratedKeys="true">
        insert into user_table (user_no, user_name, user_level)
        values
        <foreach collection="list" separator="," item="item">
            (#{item.userNo},#{item.userName}, #{item.userLevel})
        </foreach>
    </insert>
<insert id="insertUser" parameterType="com.bai.bean.req.UserReq" keyProperty="id" useGeneratedKeys="true">: 定义了一个 id 为 insertUser 的插入操作,指定了参数类型为 com.bai.bean.req.UserReq,keyProperty="id" 指定了插入后自动生成的主键值要设置到对象的 id 属性上,useGeneratedKeys="true" 表示使用数据库自动生成的主键。

insert into user_table ... values ...: 这里是插入语句的开始部分,指定了需要插入的表名和字段列表。

<foreach collection="list" separator="," item="item">: 使用 MyBatis 的 <foreach> 标签来遍历集合 list 中的元素,并对每个元素执行一次插入操作。separator="," 表示在每次循环之间用逗号分隔,item="item" 定义了在循环过程中当前元素的别名。

(#{item.userNo},#{item.userName}, #{item.userLevel}): 在 values 子句中,根据 item 对象的属性值将数据插入相应的字段中。
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`mapper.xml`文件是MyBatis框架用于配置SQL语句的部分。它是处理数据库操作的主要场所,通过XML语法描述各种数据库查询、更新等操作。在实际应用,它结合了`Mapper`接口来完成对数据库的操作,使得DAO层的代码更简洁、易于维护。 ### `mapper.xml`的基本结构: `mapper.xml`文件通常包含以下几部分: 1. **命名空间** (`namespace`):这是`mapper`接口的全限定名。每个`mapper`接口对应一个唯一的命名空间,在调用方法时需要引用这个命名空间。 2. **选择器表达式** (`<choose>`、`<when>`、`<otherwise>`):允许基于某个条件选择性的执行某个操作,这类似于SQL查询的CASE语句。 3. **嵌套选择器表达式** (`<foreach>`、`<choose>`、`<when>`、`<otherwise>`):用于循环执行一系列相似的操作,比如遍历集合并执行相同的查询。 4. **SQL映射语句** (`<select>`、`<insert>`、`<update>`、`<delete>`):直接编写SQL语句来进行特定的数据操作。 5. **动态SQL标签** (`<if>`、`<where>`、`<bind>`): 根据某些条件来动态生成SQL语句的一部分内容,提高SQL语句的灵活性。 ### 示例: ```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.service.UserMapper"> <!-- 查询所有用户 --> <select id="selectAllUsers" resultType="com.example.entity.User"> SELECT * FROM user </select> <!-- 根据ID获取用户信息 --> <select id="getUserById" parameterType="int" resultType="com.example.entity.User"> SELECT * FROM user WHERE id = #{id} </select> </mapper> ``` 在这个例子: - `<select id="selectAllUsers">` 表示定义了一个用于查询所有用户的SQL语句。 - `<select id="getUserById" parameterType="int" resultType="com.example.entity.User">` 定义了一个用于根据ID获取特定用户的SQL语句,并指定了输入参数类型及结果集返回的类型。 --- ### 相关问题: 1. `mapper.xml`的作用是什么? 2. 怎样在MyBatis编写动态SQL语句? 3. 如何在项目引入并配置MyBatis与`mapper.xml`?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值