mybatis笔记【处理多个字段的问题】|mybatis输出日志到文件|不等于

本文通过两个示例介绍了MyBatis中动态SQL的使用,包括根据条件拼接查询语句以及使用`<foreach>`标签进行批量插入。同时,展示了如何配置MyBatis-Plus的日志,以便在SpringBoot应用中输出SQL语句和参数到日志文件或控制台。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例子1:
mapper层:

    List<Map> queryBookCondition(@Param("params") Map<String, Object> params);

对应的mapper实现层:

public PageInfo<Map> queryBookCondition(Map<String, Object> params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        List<Map> maps = bookMapper.queryBookCondition(params);
        PageInfo<Map> mapPageInfo = new PageInfo<>(maps);
        return mapPageInfo;
    }

对应的mybatis写法:

<select id="queryBookCondition" resultType="map">
        select
        book_id bookId, book_name bookName, book_number bookNumber, book_picture bookPicture, price, type_name typeName, deleted
        from A.book b LEFT JOIN A.book_type t ON b.type_id = t.type_id
        where 1 = 1
        <if test="params.searchKey == 'bookName'">
            AND b.book_name like CONCAT("%",#{params.searchValue},"%")
        </if>
        <if test="params.searchKey == 'typeName'">
            AND type_name like CONCAT("%",#{params.searchValue},"%")
        </if>
        <if test="params.searchKey == 'price'">
            AND price like CONCAT("%",#{params.searchValue},"%")
        </if>
        <if test="params.searchKey == 'bookNumber'">
            AND book_number like CONCAT("%",#{params.searchValuer},"%")
        </if>
        order by b.book_id DESC
    </select>

例子2:

int insertBatch(@Param("book") List<Book> entities);
<insert id="insertBatch" keyProperty="bookId" useGeneratedKeys="true">
        insert into A.book(book_name, book_number, book_picture, description, book_press, price,
        book_type)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.bookName}, #{entity.bookNumber}, #{entity.bookPicture}, #{entity.description},
            #{entity.bookPress}, #{entity.price}, #{entity.bookType})
        </foreach>
    </insert>

关于keyProperty可以参考:关于Mybatis中keyProperty属性


springboot整合mybatis-plus的sql输出到日志文件上——百度:mybatisplus输出到文件
springboot 下mybatis-plus 如何打印sql日志和参数到日志文件|来源
打印sql到日志–240528

#控制台方式(本地启动日志打印到控制台常用方式):
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

#文件方式,需结合logginglevel
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
logging:
  level:
    com.baomidou.mybatisplus: DEBUG
    #这个是配置的mapper路径,如:cn.example.mapper
    cn.example.mapper: DEBUG  
    

mybatis不等于
mybatis不等于–参考2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值