Java整理22

1、动态sql

多条件查询	
	.xml配置文件中sql语句书写
	<select id="getEmpByCondition",resultType="Emp">
		select * from t_emp where 
		<if test="empName != null and empName!=' ' ">
			empName=#{empName}
		</if>
		<if test="age != null and age != ' ' "
			and age=#{age}
		</if>
	</select>

2、where标签

<where>
	<if                >
		and1 sex=#{sex} and2
	</if>
</where>
若where中无内容则where标签也不会生成where并且where标签会自动调节语句中的and是否出现,不过只能调节and1不能调节and2

3、Trim、Choose、When标签

当trim标签中有数据才会生效
属性prefix/suffix:将trim标签内容前/后添加指定内容
prefixOverrides/suffixOverrides:将trim标签内容前/后删除指定内容
<select id=" " resultTypde=" ">
	select * from t_user
	<where>
		<choose>
			<when> sex != null and sex != ' '
				sex=#{sex}
			</when>
			......
			<othewise>
				id=1
			</otherwise>
		</choose>
	</where>
</select>

4、Foreach标签——批量删除

foreach标签
(1)in关键字
	<delete id=" ">
		delete from t_emp where eid in
		(_1
			<foreach collection="eids_集合或数组名" item="eid_元素" separator="," open="(_2",close=")_2" 
				#{eid}
			</foreach>
		)_1
	</delete>
	(_1与(_2两个留一个即可
(2)where关键字
	<delete id=" ">
		delete from t_emp where 
			<foreach collection=" " item=" " seperate="or" open="(" close=")" >
				eid=#{eid}
			</foreach>
	</delete>

5、批量插入——sql标签

<insert id=" ">
	insert into t_emp values
		<foreach collection="emps" item="emp" separator=",">
			(null,#{emp.empName},#{emp.empSex})
		</foreach>
</insert>
sql标签
	<sql id="xxx">字段名1,字段名2</sql>
	<select id="  " resultType=" ">
		select <include refid="xxx"></include> from t_emp 
		<
			动态sql格式
		>
	</select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值