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.ws.firestation.mapper.TradeRecordMapper"> <select id="listsome" resultType="com.ws.firestation.entity.po.TradeRecord"> select * from firecab.trade_record where 1=1 <if test= "goodsName !=null and goodsName !=''"> and goods_name like concat('%',#{goodsName},'%') </if> <if test= "serverId !=null and serverId !=''"> and server_id like concat('%',#{stationId},'%') </if> <if test= "cabintCatalogy !=null and cabintCatalogy !=''"> and cabint_catalogy=#{cabintCatalogy} </if> <if test= "Type !=null and Type !=''"> and type=#{Type} </if> <if test= "tradeStartTime !=null and tradeStartTime !=''"> and tradetime >= #{tradeStartTime} </if> <if test= "tradeEndTime !=null and tradeEndTime !=''"> and tradetime <= #{tradeStartTime} </if> </select> </mapper>
- 使用了if test的判断
- concat使用了MySQL的模糊查询
- 还使用了MySQL的大小与

还使用了MySQL的大小与
[外链图片转存中…(img-upsHOaP2-1623687371596)]
- 这样就能实现随意用户输入哪几个字段来进行查询。
该博客介绍了如何使用XML配置和MyBatis的动态SQL来实现用户输入任意字段进行查询的功能。示例展示了在`TradeRecordMapper`中定义的SQL查询,利用`if`标签进行条件判断,结合`concat`函数进行模糊匹配,以及比较运算符处理时间范围查询。这种方法提高了查询的灵活性。
710

被折叠的 条评论
为什么被折叠?



