mybatis的增删改查动态sql语句虽然可以看⽂档来操作,但是还是记点
查询
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="studentNamespace">
<!--实体和表之间的映射 type:表⽰实体全路径 id为标识-->
<resultMap type="batis.Student" id="studentMap">
<id property="id" column="students_id"/>
<result property="name" column="students_name"/>
<result property="sal" column="students_sal"/>
</resultMap>
<!-- 动态SQL操作之查询 parameterType:表⽰参数类型 resultMap:表⽰返回类型-->
<select id="findAll" parameterType="batis.Student" resultMap="studentMap">
select * from students
<where>
<if test="id!=null">
and students_id = #{id}
</if>
<if test="name!=null">
and students_name = #{name}
</if>
<if test="sal!=null">
基本的增删改查语句and students_sal = #{sal}
</if>
</where>
</select>
<!--动态SQL操作之更新-->
<!-- set标签⾃动判断哪个是最后⼀个字段,会⾃动去掉最后⼀个,号 -->
<update id="dynaUpdate" parameterType="batis.Student">
update students
<set>
<if test="name!=null">
students_name = #{name},
</if>
<if test="sal!=null">
students_sal = #{sal},
</if>
</set>
where students_id = #{id}
</update>
<!--动态SQL操作之删除,多选删除和单选删除都可以-->
<delete id="dynaDeleteArray">
delete from students where students_id in
<!-- foreach⽤于迭代数组元素
open表⽰开始符号
close表⽰结束符合
separator表⽰元素间的分隔符
item表⽰迭代的数组,属性值可以任意,但提倡与⽅法的数组名相同
#{ids}表⽰数组中的每个元素值
-->
<foreach collection="array" open="(" close=")" separator="," item="ids">
#{ids}
</foreach>
</delete>
<!--动态SQL操作之插⼊ -->
<!-- sql⽚段对应字段名,id属性值任意 -->
<sql id="key">
<!-- 去掉最后⼀个, -->
<trim suffixOverrides=",">
<if test="id!=null">
students_id,
</if>
<if test="name!=null">
students_name,
</if>
<if test="sal!=null">
students_sal,
</if>
</trim>
</sql>
<!-- sql⽚段对应?,id属性值任意 -->
<sql id="value" >
<!-- 去掉最后⼀个, -->
<trim suffixOverrides=",">
<if test="id!=null">
#{id},
</if>
<if test="name!=null">
#{name},
</if>
<if test="sal!=null">
#{sal},
</if>
</trim>
</sql>
<!-- <include refid="key"/>和<include refid="value"/>表⽰引⽤上⾯定义的sql⽚段 --> <insert id="dynaInsert" parameterType="batis.Student">
insert into students(<include refid="key"/>) values(<include refid="value"/>)
</insert>
</mapper>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。