Java_ResultMap映_查询-多条件-动态条件查询-增删改-SQ

本文详细介绍了MyBatis中的ResultMap映射,包括如何在映射文件中定义ResultMap,用于处理数据库字段与实体类属性名称不一致的情况。通过ResultMap可以实现字段的别名设置,提高代码的可读性和灵活性。文中给出了查询、添加、修改、删除等操作的示例,展示了ResultMap在不同场景下的应用。

ResultMap映射

<mapper namespace="com.it.mapper.BrandMapper">
    <!-- id, brand_name as brandName, company_name as companyName, ordered, description, status -->
    <!--
            数据库表的字段名称 和 实体类的属性名称不一样,则不能自动封装数据
                *起别名:对不一样的列名起别名,让别名和实体类的属性名一样
                    *缺点:每次查询都要定义一次别名
                        * sqL片段
                            *缺点:不灵活
                * resultMap:
                    1·定义<resuLtMap>标签
                    2·在<select>标签中,使用resultMap属性替换resultType属性
    -->

<!--     注意:只需要定义字段名和属性名不一样的映射,而一样的则不需要专门定义出来。-->
    <resultMap id="brandResultMap" type="brand">
        <result column="brand_name" property="brandName"/>
        <result column="company_name" property="companyName"/>
    </resultMap>

查询全部SQL

    <select id="selectAll" resultMap="brandResultMap">
        select *
        from tb_brand;
    </select>

根据id查询

    <select id="selectById" resultMap="brandResultMap">
        select *
        from tb_brand
        where id = #{id};
    </select>

多条件查询

    <select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        <where>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="companyName != null and companyName !='' ">
                and company_name like #{companyName}
            </if>
            <if test="brandName != null and brandName !='' ">
                and brand_name like #{brandName};
            </if>
        </where>
    </select>

单条件查询

<select id="selectByConditionSingle" resultMap="brandResultMap">
        select *
        from tb_brand
        <where>
            <choose>
                <when test="status != null">
                    status = #{status}
                </when>
                <when test="companyName != null and companyName !=''">
                    company_name like #{companyName}
                </when>
                <when test="brandName != null and brandName !=''">
                    brand_name like #{brandName}
                </when>
            </choose>
        </where>
    </select>

添加

    <insert id="add" useGeneratedKeys="true" keyProperty="id">
        insert into tb_brand (brand_name, company_name, ordered, description, status)
        values (#{brandName},#{companyName},#{ordered},#{description},#{status});
    </insert>

根据id修改

   <update id="updateId">
        update tb_brand
        <set>
            <if test="status != null">
                status= #{status}
            </if>
            <if test="companyName != null and companyName !='' ">
                company_name = #{companyName}
            </if>
            <if test="brandName != null and brandName !='' ">
                brand_name = #{brandName}
            </if>
            <if test="description != null and description !='' ">
                description = #{description}
            </if>
            <if test="aordered != null ">
                aordered = #{ordered}
            </if>
        </set>
        where id = #{id};
    </update>

根据id删除(删除单个)

    <delete id="deleteById">
        delete
        from tb_brand
        where id = #{id};
    </delete>

根据id删除(批量删除)

<!--
        mybatis会将数组参数,封装为一个Map集合。
            *默认: array=数组
            *使用@Param注解改变map集合的默认key的名称
-->
    <delete id="deleteById">
        delete from tb_brand where id in
        <foreach collection="array" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>

### 使用 MyBatis-Plus 实现多条件查询 在 MyBatis-Plus 中,可以通过 `QueryWrapper` 或 `LambdaQueryWrapper` 构建复杂的查询条件。以下是一个完整的示例,展示如何实现多条件查询[^1]。 #### 示例代码:使用 QueryWrapper 进行多条件查询 ```java // 创建 QueryWrapper 对象 QueryWrapper<User> queryWrapper = new QueryWrapper<>(); // 添加条件:根据年龄等于 18 查询 queryWrapper.eq("age", 18); // 添加条件:用户名包含 "John" queryWrapper.like("name", "John"); // 添加条件:状态为 "active" queryWrapper.eq("status", "active"); // 添加条件:创建时间大于某个值 queryWrapper.gt("created_at", "2023-01-01 00:00:00"); // 执行查询 List<User> userList = userMapper.selectList(queryWrapper); ``` #### 示例代码:使用 LambdaQueryWrapper 进行多条件查询 LambdaQueryWrapper 提供了类型安全的查询条件构建方式[^1]。 ```java // 创建 LambdaQueryWrapper 对象 LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>(); // 添加条件:根据年龄等于 18 查询 lambdaQueryWrapper.eq(User::getAge, 18); // 添加条件:用户名包含 "John" lambdaQueryWrapper.like(User::getName, "John"); // 添加条件:状态为 "active" lambdaQueryWrapper.eq(User::getStatus, "active"); // 添加条件:创建时间大于某个值 lambdaQueryWrapper.gt(User::getCreatedAt, LocalDateTime.of(2023, 1, 1, 0, 0)); // 执行查询 List<User> userList = userMapper.selectList(lambdaQueryWrapper); ``` #### 在 XML 配置中实现多条件查询 如果需要通过 XML 文件定义查询逻辑,可以参考以下示例[^4]: ```xml <select id="getUserList" parameterType="java.util.Map" resultMap="UserResultMap"> SELECT * FROM user u <where> <if test="userId != null"> and u.user_id = #{userId} </if> <if test="code != null"> and u.code = #{code} </if> <if test="status != null"> and u.status in <foreach item="status" collection="statusList" separator="," open="(" close=")"> #{status} </foreach> </if> <if test="startTime != null"> <![CDATA[ and DATE_FORMAT(u.created_at,'%Y-%m-%d') >= DATE_FORMAT(#{startTime},'%Y-%m-%d') ]]> </if> <if test="endTime != null"> <![CDATA[ and DATE_FORMAT(u.created_at,'%Y-%m-%d') <= DATE_FORMAT(#{endTime},'%Y-%m-%d') ]]> </if> </where> ORDER BY u.created_at DESC </select> ``` ### 注意事项 - 如果需要处理枚举类型的数据,可以在 `application.yaml` 中配置默认的枚举处理器[^2]。 - 使用 `mybatis-plus-generator-ui` 工具可以快速生成基础的 Mapper 和实体类代码,减少开发工作量[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七@归七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值