关于MyBatis的几种通用写法

一,用来循环容器的标签forEach
      forEach元素的属性主要有 : item(集合中元素迭代时的别名),index(元素迭代时的索引),collection,open(常用于where语句中,表示以什么开始,比如以" ("开始),separator(每次进行迭代时的分隔符),close(常用于where语句中,表示以什么结束)
      在使用forEach的时候,一定要注意collection属性是必须指定的,然而,在不同情况下,该属性的值是不同的,主要有以下三种情况:
1,传入的是单参数且参数类型是一个List的时候,collection属性值为list
2,传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3,传入的是多参数,我们就需要把它们封装成一个map了,当然,单参数也可以封装成map,实际上在传入参数的时候,MyBatis里面也会把它封装成一个Map的,map的key就是参数名,collection的值就是传入的List或array对象在自己封装的map里面的key。List实例将会以**“list"作为键,而数组的键将是"array”**

//mapper中我们要为这个方法传递的是一个容器,
//将容器中的元素一个一个的拼接到xml的方法中就要使用这个forEach这个标签了
public List<Entity> queryById(List<String userids);

//对应的xml如下
<select id="queryById" resultMap="BaseResultMap">
  select * from entity
  where id in
   <foreach collection="userids" item="userid" index="index" open="(" separator="," close=")">
        #{userid}
   </foreach>
</select>

二,concat模糊查询

//有时候我们要进行条件查询,但是有几个条件并不是每次都用到,我们可以先通过判断是否拼接到sql语句中
<select id="queryById" resultMap="BaseResultMap" parameterType="entity">
    select * from entity
      <where>
           <if test="name!=null">
                name like concat('%'.concat(#{name},'%'))
           </if>
      </where>
</select>

三,choose(when,otherwise)标签
      choose标签按顺序判断其内部when标签中的test条件是否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满足时,则执行otherwise中的sql。类似于java的switch语句,choose为switch,when为case,otherwise则为default

//choose(判断参数)-按顺序将实体类User第一个不为空的属性作为 where条件
<select id="getUserList_choose" resultMap="resultMap_user" parameterType="com.qgq.pojo.User">
   select * from User u
   <where>
      <choose>
         <when test="username!=null"> 
              u.username like concat(concat('%',#{username,jdbcType=VARCHAR}),'%')
         </when>
         <when test="sex!=null and sex!='' ">
            AND u.sex = #{sex,jdbcType=INTEGER}
         </when>
         <when test="birthday!=null">
            AND u.birthday=#{birthday,jdbcType=DATE}
         </when>
         <otherwise>
         </otherwise>
      </choose>
   <where>
</selcet>

四,selectKey标签
      insert语句,Oracle经常使用序列,而在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键。myBatis的selectKey标签可以实现这个效果。
      下面这个例子,先使用mysql数据库自定义函数nextval(‘student’),用来生成一个key,并把它设置到传入实体类的studentId属性上。所以执行完这个方法,便可以通过这个实体类获取生成的key

//自动插入学生主键
<insert id="createStudentAutoKey" parameterType="com.qgq.model.StudentEntity" keyProperty="studentId">
    <selectKey keyProperty="studentId" resultType="String" order="BEFORE">
      select nextval('student')
    </selectKey>
    INSERT INTO STUDENT_TBL(STUDENT_ID,
       STUDENT_NAME,
       STUDENT_SEX,
       STUDENT_BIRTHDAY,
       STUDENT_PHOTO,
       CLASS_ID,
       PLACE_ID)
   VALUES(#{studentId},
       #{studentName},
       #{studentSex},
       #{studentBirthday},
       #{studentPhoto,javaType=byte[],jdbcType=BLOB,typeHandler=org.apache.ibatis.type.BlobTypeHandler},
       #{classId},
       #{placeId}
</insert>

调用接口方法和获取自动生成的key

StudentEntity student = new StudentEntity();
student.setStudentName("无忌");
student.setStudentSex(1);
student.setStudentBirthday(DateUtil.parse("1999-05-26"))
student.setClassId("10001");
student.setPlaceId("101");
this.dynamicSqlMapper.createStudentAutoKey(student);
System.out.println("新增学生ID:"+student.getStudentId());

五,if + where的条件判断
      当where中的条件使用的if标签较多时,这样的组合可能会导致错误,比如:

<select id="getStudentList_if" resultMap="resultMap_studentEntity" parameterType="com.qgq.model.StudentEntity">
    SELECT ST.STUDENT_ID,
    ST.STUDENT_NAME.
    ST.STUDENT_SEX
    FROM STUDENT_TABL ST
    WHERE
    <if test="studentName!=null">
          ST.STUDENT_NAME LIKE CONCAT('%',#{studentName,jdbcType=VARCHAR}),'%')
    </if>
    <if test="studentSex!=null and studentSex!=''">
       AND ST.STUDENT_SEX = #{studentSex,jdbcType=INTEGER} 
    </if>
</select>

      如果上面这个例子的参数studentName为null,将不会进行STUDENT_NAME列的判断,则会直接导致**“WHERE AND” 关键字多余**的错误SQL,所以我们得用where标签解决,where标签如果知道有返回值的话,它就插入一个"where"。如果标签返回的内容是以AND或OR开头的,则它会剔除掉

//上面例子修改为如下
<select id="getStudentList_if" resultMap="resultMap_studentEntity" parameterType="com.qgq.model.StudentEntity">
    SELECT ST.STUDENT_ID,
    ST.STUDENT_NAME.
    ST.STUDENT_SEX
    FROM STUDENT_TABL ST
    <where>
    <if test="studentName!=null">
          ST.STUDENT_NAME LIKE CONCAT('%',#{studentName,jdbcType=VARCHAR}),'%')
    </if>
    <if test="studentSex!=null and studentSex!=''">
       AND ST.STUDENT_SEX = #{studentSex,jdbcType=INTEGER} 
    </if>
    </where>
</select>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值