3、insert实现:注意每个字段之间一一对应就行了。
<insert id="addRum" parameterType="RegRum"> insert into regnum(patientname,cardtype,cardid,socalnum,phone,sex, age,position,firstdiagnose,doctorid,status,remark,date) VALUES(#{regnum.patientname},#{regnum.cardtype},#{regnum.cardid} </insert>
4、update:可以使用set-if或者trim-if
set-if实例:
<update id="updateregnum"> update regnum <set > <if test="phone!=null and phone!=''"> phone=#{phone}, </if> <if test="position!=null and position!=''"> position=#{position}, </if> <if test="firstdiagnose!=null and firstdiagnose!=''"> firstdiagnose=#{firstdiagnose}, </if> <if test="doctorid!=0"> doctorid=#{doctorid}, </if> <if test="remark!=null and remark!=''"> remark=#{remark}, </if> <if test="date!=null and date!=''"> date=#{date}, </if> <if test="age!=0"> age=#{age}, </if> <if test="status!=null and status !=''"> status=#{status}, </if> <if test="sex==0 or sex==1"> sex=#{sex}, </if> </set> where patientid=#{patientid} </update>
if的判断test必须有添加条件,否则会报错。
trim-if组合
<update id="updateregnum"> update regnum <trim prefix="set" suffixOverrides="," suffix=" where patientid=#{patientid}"> <if test="phone!=null and phone!=''"> phone=#{phone}, </if> <if test="position!=null and position!=''"> position=#{position}, </if> <if test="firstdiagnose!=null and firstdiagnose!=''"> firstdiagnose=#{firstdiagnose}, </if> <if test="doctorid!=0"> doctorid=#{doctorid}, </if> <if test="remark!=null and remark!=''"> remark=#{remark}, </if> <if test="date!=null and date!=''"> date=#{date}, </if> <if test="age!=0"> age=#{age}, </if> <if test="status!=null and status !=''"> status=#{status}, </if> <if test="sex!=0"> sex=#{sex}, </if> </trim> </update>
5、delete
1. <delete id="delversion" parameterType="Integer"> 2. delete from app_version where appid=#{appid}; 3. </delete>
6、foreach的实例:迭代一个集合,通常用于in条件。
(1)只传入一个参数,集合类型的。
<select id="findByids2" resultType="User"> select * from user where userid in <foreach collection="ids" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
collection表示传入的集合名字,item的值表示集合中每一个值的别名,open以什么开头,close以什么结尾,separator表示值之间以什么分隔。
(2)传入多个参数,我们可以用map集合传入。当然也可以用@param注解。
使用map时:dao层的map值一定要是Object,否则集合类型传不进去。
public Collection<User> findbymap(Map<String,Object> map);
dao.xml的#{phone}以及collection的值是map所对应的键。
<select id="findbymap" resultType="User"> select * from user where userid in <foreach collection="idslist" index="index" item="id" open="(" separator="," close=")"> #{id} </foreach> and phone=#{phone}; </select>
使用@param注解的方法:dao层这样写:
public Collection<User> findByids3(@Param("ids")int [] ids,@Param("phone") String phone);
dao.xml的#{phone}以及collection的值是注解名。