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=#{goodsName} </if> <if test= "serverId !=null and serverId !=''"> and server_id=#{serverId} </if> <if test= "cabintCatalogy !=null and cabintCatalogy !=''"> and cabint_catalogy=#{cabintCatalogy} </if> <if test= "Type !=null and Type !=''"> and type=#{Type} </if> <if test= "Tradetime !=null and Tradetime !=''"> and tradetime </if> </select> </mapper>
- 这样就能实现随意用户输入哪几个字段来进行查询。
这篇博客介绍了如何在MyBatis中通过XML映射文件实现动态查询。根据用户输入的goodsName、serverId、cabintCatalogy、Type等字段,动态构造SQL查询语句,灵活匹配不同条件,实现数据库查询功能。这种方法提高了代码的可读性和复用性。
214





