mybatis实现条件查询
程序员文章站
2022-03-11 21:43:01
...
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.trustlife.qec.operation.core.dal.mapper.oms.TemplateElementMapper">
<resultMap id="BaseResultMap" type="com.trustlife.qec.operation.core.dal.dto.oms.TemplateElementDO">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="template_id" jdbcType="VARCHAR" property="templateId" />
<result column="element_name" jdbcType="VARCHAR" property="elementName" />
<result column="element_jsonpath" jdbcType="VARCHAR" property="elementJsonpath" />
</resultMap>
<sql id="Base_Column_List">
id, template_id, element_name, element_jsonpath
</sql>
<select id="selectByCond" parameterType="com.trustlife.qec.operation.core.dal.dto.oms.TemplateElementDO" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"></include>
from oms.t_template_element
<where>
<if test="null != id and '' != id">
and id=#{id}
</if>
<if test="null != templateId and '' != templateId">
and template_id=#{templateId}
</if>
<if test="null != elementName and '' != elementName">
and element_name =#{elementName}
</if>
<if test="null != elementJsonpath and '' != elementJsonpath">
and element_jsonpath =#{elementJsonpath}
</if>
</where>
</select>
</mapper>
DO类
public class TemplateElementDO implements Serializable {
private Integer id;
private String templateId;
private String elementName;
private String elementJsonpath;
private static final long serialVersionUID = 1L;
//省略gettersetter
在XML里面,设置parameterType为对应的DO,通过where标签和if标签来判断对应的DO里面是否有值,如果有值的话,就进行拼接。如果DO里面属性都为null,则查询所有的行。