mybatis学习(三)
1.mybatis是一个ORM持久层框架的佼佼者,真正上实现了SQL语句与java代码分离。优秀的功能,动态分离,缓存,插件-pageHelper等
2.SQL语句使用in的时候,可以用数组封装in中的值。
3.批量操作数据的情况,可以把操作的数据封装在数组中
4.mybaits的入参处理:1.传递单个参数的形式(mybaits会自动进行参数的赋值) 2.传递多参数的时候(mybatis会自动封装在Map集合中)
3.Collection,List,Array作为参数,封装为Map,有一定规则的。
5.多参数传值的时候 #{名称}的名称是有规范的为[arg0,arg1,parm1,parm2] ,如果用#{username} and #{userpass} 会报错,用#{arg0} and #{arg1} 就不会报错。
可以用javabean处理:
代码如下:
public Person getPersonByNameAndGender(Person person)
可以用Map传递多参数:
代码如下:
public Person getPersonByNameAndGender(Map<String,Object> param);
利用注解@parm:
代码如下:
public Person getPersonByNameAndGender(@Param("username") String username,@Param("gender") String gender)
collection集合处理参数:
也可以用别名来代替key值:
public Person getPersonByCollection(@Param("test") int[] ids);
动态遍历foreach;
利用<trim>标签
<trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > ID, </if> <if test="regType != null" > regType, </if> <if test="lastLandIp != null" > lastLandIp, </if> <if test="pwd != null" > PWD, </if> <if test="userName != null" > USERNAME, </if> <if test="email != null" > EMAIL, </if> <if test="isActivation != null" > ISACTIVATION, </if> <if test="emailActiveDate != null" > EMAILACTIVEDATE, </if> <if test="mobilePhone != null" > MOBILEPHONE, </if> <if test="isBindPhone != null" > ISBINDPHONE, </if> <if test="bindPhoneDate != null" > BINDPHONEDATE, </if> <if test="nickName != null" > NICKNAME, </if> <if test="male != null" > MALE, </if> <if test="birth != null" > BIRTH, </if> <if test="identityCode != null" > IDENTITYCODE, </if> <if test="identityType != null" > IDENTITYTYPE, </if> <if test="province != null" > PROVINCE, </if> <if test="city != null" > CITY, </if> <if test="address != null" > ADDRESS, </if> <if test="postCode != null" > POSTCODE, </if> <if test="homePhone != null" > HOMEPHONE, </if> <if test="regTime != null" > REGTIME, </if> <if test="lastLandTime != null" > LASTLANDTIME, </if> <if test="tempLastTime != null" > TEMPLASTTIME, </if> <if test="status != null" > STATUS, </if> <if test="payPwd != null" > PAYPWD, </if> ZM_SITEID, <if test="addDate != null" > ADDDATE, </if> <if test="realName != null" > REALNAME, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=DECIMAL}, </if> <if test="regType != null" > #{regType,jdbcType=DECIMAL}, </if> <if test="lastLandIp != null" > #{lastLandIp,jdbcType=VARCHAR}, </if> <if test="pwd != null" > #{pwd,jdbcType=VARCHAR}, </if> <if test="userName != null" > #{userName,jdbcType=VARCHAR}, </if> <if test="email != null" > #{email,jdbcType=VARCHAR}, </if> <if test="isActivation != null" > #{isActivation,jdbcType=DECIMAL}, </if> <if test="emailActiveDate != null" > #{emailActiveDate,jdbcType=TIMESTAMP}, </if> <if test="mobilePhone != null" > #{mobilePhone,jdbcType=VARCHAR}, </if> <if test="isBindPhone != null" > #{isBindPhone,jdbcType=DECIMAL}, </if> <if test="bindPhoneDate != null" > #{bindPhoneDate,jdbcType=TIMESTAMP}, </if> <if test="nickName != null" > #{nickName,jdbcType=VARCHAR}, </if> <if test="male != null" > #{male,jdbcType=DECIMAL}, </if> <if test="birth != null" > #{birth,jdbcType=TIMESTAMP}, </if> <if test="identityCode != null" > #{identityCode,jdbcType=VARCHAR}, </if> <if test="identityType != null" > #{identityType,jdbcType=DECIMAL}, </if> <if test="province != null" > #{province,jdbcType=VARCHAR}, </if> <if test="city != null" > #{city,jdbcType=VARCHAR}, </if> <if test="address != null" > #{address,jdbcType=VARCHAR}, </if> <if test="postCode != null" > #{postCode,jdbcType=VARCHAR}, </if> <if test="homePhone != null" > #{homePhone,jdbcType=VARCHAR}, </if> <if test="regTime != null" > #{regTime,jdbcType=TIMESTAMP}, </if> <if test="lastLandTime != null" > #{lastLandTime,jdbcType=TIMESTAMP}, </if> <if test="tempLastTime != null" > #{tempLastTime,jdbcType=TIMESTAMP}, </if> <if test="status != null" > #{status,jdbcType=DECIMAL}, </if> <if test="payPwd != null" > #{payPwd,jdbcType=VARCHAR}, </if> 1, <if test="addDate != null" > #{addDate,jdbcType=TIMESTAMP}, </if> <if test="realName != null" > #{realName,jdbcType=VARCHAR}, </if> </trim>