您的位置:首页 > 其它

初识MyBatis

2015-12-15 16:44 197 查看
由于接触后端开发,所以对MyBatis稍微做了些学习,所以内容接触的比较浅显

MyBatis 本是apache的一个开源项目iBatis,
2010年这个项目由apache software foundation 迁移到了googlecode,并且改名为MyBatis 。2013年11月迁移到Github。

 iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL
Maps和Data Access Objects(DAO)

MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain
Old Java Objects,普通的 Java对象)映射成数据库中的记录。



下面介绍几个参数:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"
>

<mapper namespace="com.codemonkey.****.dao.****">

  <resultMap id="BaseResultMap" type="com.****.****.model.****">

  <id column="ID" property="id" jdbcType="BIGINT" />

  <result column="USERNAME" property="userName" jdbcType="VARCHAR"
/> 

  </resultMap>

  <sql id="Base_Column_List">

  ID,

  USERNAME, 

  </sql>

<insert id="insert" parameterType="com.codemonkey.****.model.****” useGeneratedKeys="true"keyProperty="id">

  insert into TABLE_NAME 

    <trim prefix="(" suffix=")" suffixOverrides=",">

  <if test="userName != null">USERNAME,</if> 

  </trim>

  <trim prefix="values (" suffix=")" suffixOverrides=",">

  <if test="userName!=null”>#{userName,jdbcType=VARCHER}</if> 

  </trim>

  </insert>

 

</mapper>

1.parameterType  parameterMap

2.resultMap  resultType 

sql语句where条件中,需要一些安全判断,例如按性别检索,如果传入的参数是空的,

此时查询出的结果很可能是空的,也许我们需要参数为空 时,是查出全部的信息。

这是我们可以使用动态sql,增加一个判断,当参数不符合要求的时候,

我们可以不去判断此查询条件。

1、<if></if> 当写sql语句的时候,传入的参数不满足if的条件将会直接过滤这些判断

2、<where></where> 当写sql的查询语句的时候,查询的条件可以通过where语句选择条件

3. <Set></set>当在update语句中使用if标签时,如果前面的if没有执行,则或导致逗号多余错误。使用set标签可以将动态的配置SET 关键字,和剔除追加到条件末尾的任何不相关的逗号。

4. Trim  trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果。

5.choose (when, otherwise)

         有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。

         if是与(and)的关系,而choose是或(or)的关系。

 

FOR EXAMPLE:

<select id="getStudentListChooseEntity" parameterType="StudentEntity"
resultMap="studentResultMap">  

    SELECT * from STUDENT_TBL ST   

    <where>  

        <choose>  

            <when test="studentName!=null and studentName!='' ”> 
ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') </when>  

            <when test="studentSex!= null and studentSex!= '' ">   AND ST.STUDENT_SEX
= #{studentSex}       </when>  

            <when test="studentBirthday!=null">   AND ST.STUDENT_BIRTHDAY
= #{studentBirthday}         </when>  

            <when test="classEntity!=null and classEntity.classID !=null
and classEntity.classID!='' ">   ANDST.CLASS_ID = #{classEntity.classID}   

            </when>  

            <otherwise>                   

            </otherwise>  

        </choose>  

    </where>  

</select> 

6 foreach

对于动态SQL 非常必须的,主是要迭代一个集合,通常是用于IN 条件。

List 实例将使用“list”做为键,数组实例以“array” 做为键。

 

 参数为list实例的写法:

SQL写法:

Xml代码 

<select id="getStudentListByClassIDs" resultMap="studentResultMap">  

    SELECT * FROM STUDENT_TBL ST   

     WHERE ST.CLASS_ID IN    

     <foreach collection="list" item="classList"  open="(" separator=","
close=")">  

        #{classList}   

     </foreach>      

</select> 

来几个常用的代码操作:

一:插入

<insert id="batchInsertB2B" parameterType="ArrayList">

insert into xxxxtable(hkgs,hkgsjsda,office,asdf,ddd,ffff,supfullName,classtype,agent_type,remark)

<foreach collection="list" item="item" index="index" separator="union all">

select #{item.hkgs,jdbcType=VARCHAR},

#{item.hkgsjsda,jdbcType=VARCHAR},

#{item.office,jdbcType=VARCHAR},

#{item.asdf,jdbcType=VARCHAR},

#{item.ddd,jdbcType=VARCHAR},

#{item.ffff,jdbcType=VARCHAR},

#{item.supfullName,jdbcType=VARCHAR},0,0,

#{item.remark,jdbcType=VARCHAR} from dual

</foreach>

</insert>

二:删除

<!-- 通过主键集合批量删除记录 --> 

<delete id="batchRemoveUserByPks" parameterType="java.util.List"> 

DELETE FROM LD_USER WHERE ID in 

<foreach item="item" index="index" collection="list" open="(" separator="," close=")"> 

#{item} 

</foreach> 

</delete>

 

三:更新

  

 <update id="updateOrders" parameterType="java.util.List">

 update orders set state = '0' where no in

 <foreach collection="list" item="nos" open="(" separator="," close=")">

   #{nos}

 </foreach>

 </update>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis