mybatis中的Criteria及对应xml的解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Andrew_Yuan/article/details/89236484

Criteria的and和or进行联合查询

DemoExample example=new DemoExample ();
DemoExample.Criteria criteria=example.createCriteria();
criteria.andidEqualTo(id);
criteria.andStatusEqualTo("0");
        
DemoExample.Criteria criteria2=example.createCriteria();
criteria2.andidEqualTo(id);
criteria2.andstatusEqualTo("1");
example.or(criteria2);
dao.countByExample(example);

sql:select count(*) from demo WHERE ( ID = ? and STATUS = ? ) or( ID = ? and STATUS = ? )

xml中关于criteria的一些解读:

<!-- 查询条件 方法名 -->
<sql id="Example_Where_Clause" >
<!-- 条件 -->
<where >
<!-- 条件集合 -->
<!-- 传入集合类型是oredCriteria 循环变量是criteria 关系是或者-->
<foreach collection="oredCriteria" item="criteria" separator="or" >
<!-- 如果传入的条件有效 单词的意思标准(相当于条件).有效 -->
<if test="criteria.valid" >
<!-- <修剪前缀= "("后缀= ")"前缀覆盖 = "和" > -->
<trim prefix="(" suffix=")" prefixOverrides="and" >
<!-- 条件集合 -->
<!-- 集合类型是oredCriteria 循环变量是criteria -->
<foreach collection="criteria.criteria" item="criterion" >
<!-- 通过判断选择查询条件条件-->
<choose >
<!-- 通过判断标准来执行对应的查询条件 -->
<!-- 当条件不符合标准的时候 -->
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<!-- 条件是一个值的时候 调用此条件 相当与id查询 name查询-->
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<!-- 条件是两个值的时候 调用此条件 -->
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<!-- 如果条件是一个集合的话调用此条件 -->
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<!-- 批量更新 -->
<sql id="Update_By_Example_Where_Clause" >
<where >
<!-- 传入集合类型是example.oredCriteria 循环变量是criteria -->
<foreach collection="example.oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<!-- 结构是条件判断如果满足就追加 -->
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<!-- 表中的字段值 -->
<sql id="Base_Column_List" >
id, name, price, color, cima, imgPath
</sql>
<!-- 查询的方法 -->
<!-- 方法名 返回值类型 参数类型 -->
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.baidu.bean.GGoodExample" >
<!-- 查询 -->
select
<!-- 条件满足 这个字段在exa实体类中 是一个布尔值 -->
<if test="distinct" >
distinct
</if>
<!-- 正常查询 -->
<include refid="Base_Column_List" />
from g_good
<!-- 如果参数列表不为空 调用下面的方法 这个方法返回的是参数列表 也可以是条件查询-->
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<!-- 如果有分组字段 根据该字段进行分组查询 -->
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
<!-- 这个是分页查询要在exa实体类中添加两个字段 offset limit -->
<if test="offset != null && limit != null">
limit ${offset},${limit}
</if>
</select>
<!-- 通过id查询 -->
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<!-- 要查询的字段 -->
<include refid="Base_Column_List" />
from g_good
where id = #{id,jdbcType=INTEGER}
</select>
<!-- 通过id删除 -->
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from g_good
where id = #{id,jdbcType=INTEGER}
</delete>
<!-- 条件删除 -->
<delete id="deleteByExample" parameterType="com.baidu.bean.GGoodExample" >
delete from g_good
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</delete>
<!-- 插入数据 -->
<insert id="insert" parameterType="com.baidu.bean.GGood" >
insert into g_good (id, name, price,
color, cima, imgPath
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{price,jdbcType=INTEGER},
#{color,jdbcType=VARCHAR}, #{cima,jdbcType=VARCHAR}, #{imgpath,jdbcType=VARCHAR}
)
</insert>
<!-- 插入方法自动判断有没有参数 没有参数也不会报错 -->
<insert id="insertSelective" parameterType="com.baidu.bean.GGood" >
insert into g_good
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="name != null" >
name,
</if>
<if test="price != null" >
price,
</if>
<if test="color != null" >
color,
</if>
<if test="cima != null" >
cima,
</if>
<if test="imgpath != null" >
imgPath,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="price != null" >
#{price,jdbcType=INTEGER},
</if>
<if test="color != null" >
#{color,jdbcType=VARCHAR},
</if>
<if test="cima != null" >
#{cima,jdbcType=VARCHAR},
</if>
<if test="imgpath != null" >
#{imgpath,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<!-- 查询表中的记录数 -->
<select id="countByExample" parameterType="com.baidu.bean.GGoodExample" resultType="java.lang.Integer" >
select count(*) from g_good
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</select>
<!-- 普通修改 -->
<update id="updateByExampleSelective" parameterType="map" >
update g_good
<set >
<if test="record.id != null" >
id = #{record.id,jdbcType=INTEGER},
</if>
<if test="record.name != null" >
name = #{record.name,jdbcType=VARCHAR},
</if>
<if test="record.price != null" >
price = #{record.price,jdbcType=INTEGER},
</if>
<if test="record.color != null" >
color = #{record.color,jdbcType=VARCHAR},
</if>
<if test="record.cima != null" >
cima = #{record.cima,jdbcType=VARCHAR},
</if>
<if test="record.imgpath != null" >
imgPath = #{record.imgpath,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<!-- 高级修改 带条件修改 -->
<update id="updateByExample" parameterType="map" >
update g_good
set id = #{record.id,jdbcType=INTEGER},
name = #{record.name,jdbcType=VARCHAR},
price = #{record.price,jdbcType=INTEGER},
color = #{record.color,jdbcType=VARCHAR},
cima = #{record.cima,jdbcType=VARCHAR},
imgPath = #{record.imgpath,jdbcType=VARCHAR}
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<!-- 通过id修改 允许非空修改 -->
<update id="updateByPrimaryKeySelective" parameterType="com.baidu.bean.GGood" >
update g_good
<set >
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="price != null" >
price = #{price,jdbcType=INTEGER},
</if>
<if test="color != null" >
color = #{color,jdbcType=VARCHAR},
</if>
<if test="cima != null" >
cima = #{cima,jdbcType=VARCHAR},
</if>
<if test="imgpath != null" >
imgPath = #{imgpath,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<!-- 通过id修改不允许有空值 -->
<update id="updateByPrimaryKey" parameterType="com.baidu.bean.GGood" >
update g_good
set name = #{name,jdbcType=VARCHAR},
price = #{price,jdbcType=INTEGER},
color = #{color,jdbcType=VARCHAR},
cima = #{cima,jdbcType=VARCHAR},
imgPath = #{imgpath,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>

猜你喜欢

转载自blog.csdn.net/Andrew_Yuan/article/details/89236484