mybatis之动态sql

查询(where+if或choose)

假如有需求就是传入user根据属性动态拼接sql语句。

<select id="getTypeByIdAndUsers" resultMap="getTypeByIdAndUsers">
    select * from user
    <where>
    	<if test="id!=null">
    		id=#{id}
    	</if>
    	<if test="name!=null">
    		and name=#{name}
    	</if>
	</where>
</select>
<select id="getTypeByIdAndUsers" resultMap="getTypeByIdAndUsers">
    select * from user
    <where>
    	<choose>
			<when test="id!=null">
				id=#{id}
			</when>
			<when test="name!=null">
				id=#{id}
			</when>
			<otherwise>
				1=1
			</otherwise>
		</choose>
	</where>
</select>

更新(set)

<update id="getTypeByIdAndUsers" resultMap="getTypeByIdAndUsers">
    update tabel
    <set>
    	<if test="id!=null">
    		id=#{id},
    	</if>
    	<if test="name!=null">
    		name=#{name}
    	</if>
    </set>
    where id=#{id}
</update>

插入

批量插入foreach

<!-- public void addUser(@Param("users") List<User> users)-->
<update id="addUser">
    insert into user(id,name,password,type_id)
    values
    <foreach collection="users" item="user" separator=",">
		(#{user.id},#{user.name},#{user.password},#{user.type.typeId})
	</foreach>
</update>
<!-- public void addUser(@Param("users") List<User> users)-->
<update id="addUser">
    <foreach collection="users" item="user" separator=","
    open="insert into user(id,name,password,type_id) values"
    close="">
		(#{user.id},#{user.name},#{user.password},#{user.type.typeId})
	</foreach>
</update>

动态sql插入

<!-- 动态sql插入方法二   start -->
  <!-- 对应的插入字段的名字 -->
  <sql id="key">
   <trim suffixOverrides=",">
    <if test="userCode!=null and userCode!=''">
     userCode,
    </if>
    <if test="userName!=null and userName!=''">
     userName,
    </if>
    <if test="userPassword!=null and userPassword!=''">
     userPassword,
    </if>
    <if test="gender!=null and gender!=''">
     gender,
    </if>
    <if test="address!=null and address!=''">
     address,
    </if>
    <if test="phone!=null and phone!=''">
     phone,
    </if>
   </trim>
  </sql>
  
  <!-- 对应的插入字段的值 -->
  <sql id="values">
   <trim suffixOverrides=",">
    <if test="userCode!=null and userCode!=''">
     #{userCode},
    </if>
    <if test="userName!=null and userName!=''">
     #{userName},
    </if>
    <if test="userPassword!=null and userPassword!=''">
     #{userPassword},
    </if>
    <if test="gender!=null and gender!=''">
     #{gender},
    </if>
    <if test="address!=null and address!=''">
     #{address},
    </if>
    <if test="phone!=null and phone!=''">
     #{phone},
    </if>
   </trim>
  </sql>
  <insert id="addUser2" parameterType="user">
   insert into smbms_user(<include refid="key"/>) 
    values(<include refid="values"/>)
  </insert>

重复字段的抽取(sql和include标签)

在配置文件中有许多重复字段怎么办呢?用sql标签抽取重复字段。

<select id="selectUser">
    select id,name,password from user
</select>

等价于:

<select id="selectUser">
    select <include refid="selectColumn"></include> from user
</select>
<sql id="selectColumn">
	id,name,password
</sql>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值