扣丁学堂Java培训之详解MyBatis常用写法

2018-12-04 13:39:14 443浏览

今天扣丁学堂Java培训老师给大家介绍一下关于MyBatis常用写法的详细介绍,首先MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用简单的XML或注解来配置和映射原生信息,将接口和Java的POJOs(PlainOldJavaObjects,普通的Java对象)映射成数据库中的记录。



1、forEach循环

forEach元素的属性主要有item,idnex,collection,open,separator,close。

1.collection:传入的List或Array或自己封装的Map。

2.item:集合中元素迭代时的别名。

3.idnex:集合中元素迭代是的索引。

4.open:where后面表示以什么开始,如以‘('开始。

5.separator:表示在每次进行迭代是的分隔符。

6.close:where后面表示以什么结束,如以‘)'结束。

//mapper中需要传递一个容器
public List<User> queryByIdList(List<Integer> userIdList);
 
<select id="queryByIdList" resultMap="BaseResultMap" parameterType="map">
  SELECT * FROM user
  WHERE userId IN
  <foreach collection="userIdList" item="id" index="index" open="(" close=")" separator=",">
    #{id}
  </foreach>
</select>
2、concat模糊查询

//模糊查询使用concat拼接sql
<select id="queryByName" resultMap="BaseResultMap" paramterType"string">
  SELECT * FROM user
  <where>
    <if test="name != null">
      name like concat('%', concat(#{name}, '%'))
    </if>
  </where>
</select>
3、if+where标签

用if标签判断参数是否有效来进行条件查询。

<select id="getUserList" resultMap="BaseResultMap" paramterType="com.demo.User">
  SELECT * FROM user
  <where>
    <if test="userId !=null and userId!= ''">
      userId= #{userId}
    </if>
    <if test="name !=null and name!= ''">
      AND name= #{name}
    </if>
    <if phone="userId !=null and phone!= ''">
      AND phone= #{phone}
    </if>
  </where>
</select>
where动态语句中,where标签会自动去掉AND或OR。防止WHEREAND错误。

4、if+set

使用set标签可以动态的配置SET关键字,使用if+set标签,如果某项为null则不进行更新。

<update id="updateUser" paramterType="com.demo.User">
    UPDATE user
    <set>
        <if test=" name != null and name != ''">
            name = #{name},
        </if>
        <if test=" phone != null and phone != ''">
            phone = #{phone},
        </if>
    </set>
    WHERE userId = #{userId}
</update>

5、if+trim代替where/set标签

trim可以更灵活的去处多余关键字的标签,可以实现where和set的效果。

<select id="getUserList" resultMap="BaseResultMap" paramterType="com.demo.User">
  SELECT * FROM user
  <trim prefix="WHERE" prefixOverrides="AND|OR">
    <if test="userId !=null and userId!= ''">
      userId= #{userId}
    </if>
    <if test="name !=null and name!= ''">
      AND name= #{name}
    </if>
    <if phone="userId !=null and phone!= ''">
      AND phone= #{phone}
    </if>
  </trim>
</select>
 
<update id="updateUser" paramterType="com.demo.User">
  UPDATE user
  <trim prefix="SET" suffixOverrides=",">
    <if test=" name != null and name != ''">
      name = #{name},
    </if>
    <if test=" phone != null and phone != ''">
      phone = #{phone},
    </if>
  </trim>
  WHERE userId = #{userId}
</update>

5、choose(when,otherwise)标签

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

<select id="selectCustomerByCustNameAndType" parameterType="map" resultMap="BaseResultMap">
  SELECT * FROM user
  <choose>
    <when test="Utype == 'name'">
      WHERE name = #{name} 
    </when>
    <when test="Utype == 'phone'">
      WHERE phone= #{phone}
    </when>
    <when test="Utype == 'email'">
      WHERE email= #{email}
    </when>
    <otherwise>
      WHERE name = #{name}
    </otherwise>
  </choose>
</select>

以上扣丁学堂Java培训之详解MyBatis常用写法的详细介绍,希望对大家有所帮助,扣丁学堂有专业老师制定的Java学习路线图辅助学员学习,此外还有与时俱进的Java课程体系和Java视频直播课供大家学习,想要学好Java开发技术的小伙伴快快行动吧。扣丁学堂Java技术交流群:670348138。



 


关注微信公众号获取更多学习资料 


查看更多关于“Java开发资讯”的相关文章>>


标签: Java培训 Java视频教程 Java多线程 Java面试题 Java学习视频 Java开发

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号