目录
一、mybatis动态sql
1、if
<update id="updateByPrimaryKeySelective" parameterType="com.cxy.model.User" >
update t_yh
<set >
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
sex = #{sex,jdbcType=VARCHAR},
</if>
<if test="age != null" >
age = #{age,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
自定义MVC
update t_oa_meeting_info set id=?,title=?,content=?,zhuchiren=?,author=?,..... where id=?
弊端:
会议编辑界面 inforEdit.jsp
<form>
<input name="title">
<input name="content">
</form>
意味着后台meetingInfo实体类 只有title、content属性值不为空,其他为空
进一步的
update t_oa_meeting_info set id=?,title=aa,content=cc,zhuchiren=null,author=null,..... where id=?
update t_oa_meeting_info
title= 纳新,
price = #{content,jdbcType=REAL},
where bid = #{bid,jdbcType=INTEGER}
2、foreach
@Test
public void test3() {
int[] ints={1,2,3,4,5};
//将数组变成字符串
StringBuffer sb = new StringBuffer();
for (int i:ints){
sb.append(i).append(",");
System.out.println(sb);
}
}
运行结果:
案例:
UserMapper.xml
<select id="selectByIn" resultMap="BaseResultMap" parameterType="java.util.List" >
select
<include refid="Base_Column_List" />
from t_yh
where id in
<foreach collection="userIds" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</select>
UserMapper.java
List<User> selectByIn(@Param("userIds") List userIds);
UserBiz.java
package com.cxy.biz;
import com.cxy.model.User;
import java.util.List;
public interface UserBiz {
int deleteByPrimaryKey(Integer id);
User selectByPrimaryKey(Integer id);
List<User> selectByIn(List userIds);
}
UserBizImpl .java
package com.cxy.biz.impl;
import com.cxy.biz.UserBiz;
import com.cxy.mapper.UserMapper;
import com.cxy.model.User;
import java.util.List;
public class Us