1.sql语句标签
<!--查询语句-->
<@H_502_6@select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
</@H_502_6@select>
<!--插入语句-->
<@H_502_6@insert id="insert" parameterType="pojo.OrderTable" >
insert into ordertable (order_id,cid,address,create_date,orderitem_id)
values (#{orderId,jdbcType=VARCHAR},#{cid,#{address,#{createDate,jdbcType=TIMESTAMP},#{orderitemId,jdbcType=VARCHAR})
</@H_502_6@insert>
<!--删除语句-->
<@H_502_6@delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from ordertable
where order_id = #{orderId,jdbcType=VARCHAR}
</@H_502_6@delete>
<!--修改语句-->
<@H_502_6@update id="updateByPrimaryKey" parameterType="pojo.OrderTable" >
update ordertable
set cid = #{cid,address = #{address,create_date = #{createDate,orderitem_id = #{orderitemId,jdbcType=VARCHAR}
where order_id = #{orderId,jdbcType=VARCHAR}
</@H_502_6@update>
需要配置的属性:id=”xxxx” >>> 表示此段sql执行语句的唯一标识,也是接口的方法名称【必须一致才能找到】
parameterType=”” >>>表示该sql语句中需要传入的参数, 类型要与对应的接口方法的类型一致【可选】
resultMap=“ ”>>> 定义出参,调用已定义的映射管理器的id值
resultType=“ ”>>>定义出参,匹配普通java类型或自定义的pojo【出参类型若不指定,将为语句类型默认类型,如语句返回值为int】
传参和取值:mapper.xml 的灵活性还体现在sql执行语句可以传参,参数类型通过parameterType= “” 定义
2.sql片段标签
通过该标签可定义能复用的sql语句片段,在执行sql语句标签中直接引用即可。这样既可以提高编码效率,还能有效简化代码,提高可读性
<!--定义sql片段-->
<@H_502_6@sql id="orderAndItem">
o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count
</@H_502_6@sql>
<@H_502_6@select id="findOrderAndItemsByOid" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<!--引用sql片段-->
<@H_502_6@include refid="orderAndItem" />
from ordertable o
join orderitem i on o.orderitem_id = i.orderitem_id
where o.order_id = #{orderId}
</@H_502_6@select>
需要配置的属性:id=”” >>>表示需要改sql语句片段的唯一标识
引用:通过标签引用,refid=”” 中的值指向需要引用的sql 标签中的id=”“属性
3.映射管理器resultMap
映射管理器,是Mybatis中最强大的工具,使用其可以进行实体类之间的关系,并管理结果和实体类间的映射关系
需要配置的属性:<resultMap id="" type=" "></resutlMap> id=" " >>>表示这个映射管理器的唯一标识,外部通过该值引用; type = "">>> 表示需要映射的实体类; 需要配置的参数:<id column = " " property= " " /> <id>标签指的是:结果集中结果唯一的列【column】 和 实体属性【property】的映射关系,注意:<id>标签管理的列未必是主键列,需要根据具体需求指定; <result column= " " property=" " /> <result>标签指的是:结果集中普通【column】 和 实体属性【property】的映射关系; 需要维护的关系:所谓关系维护是值在主表查询时将其关联子表的结果也查询出来
一对一关系
<assocation property = " " javaType=" "> property = “ ” 被维护实体在宿主实体中的属性名,javaType = " " 被维护实体的类型
<@H_502_6@resultMap id="BaseResultMap" type="pojo.Orderitem" >
<@H_502_6@id column="orderitem_id" property="orderitemId" jdbcType="VARCHAR" />
<@H_502_6@result column="product_id" property="productId" jdbcType="VARCHAR" />
<@H_502_6@result column="count" property="count" jdbcType="INTEGER" />
<!-- 通过association 维护 一对一关系 -->
<@H_502_6@association property="product" javaType="pojo.Product">
<@H_502_6@id column="product_id" property="productId"/>
<@H_502_6@result column="product_factroy" property="productFactroy"/>
<@H_502_6@result column="product_store" property="productStore"/>
<@H_502_6@result column="product_descript" property="productDescript"/>
</@H_502_6@association>
</@H_502_6@resultMap>
一对多关系
<collection property=" " ofType=" "> property = “ ” 被维护实体在宿主实体中的属性名 ,ofType=“ ”是被维护方在宿主类中集合泛型限定类型
public class @H_502_6@OrderTable {
private String orderId;
private String cid;
private String address;
private Date createDate;
private String orderitemId;
private List<Orderitem> orderitemList;
<@H_502_6@resultMap id="BaseResultMap" type="pojo.OrderTable" >
<!-- WARNING - @mbggenerated This element is automatically generated by MyBatis Generator,do not modify. This element was generated on Fri May 06 15:49:42 CST 2016. -->
<@H_502_6@id column="order_id" property="orderId" jdbcType="VARCHAR" />
<@H_502_6@result column="cid" property="cid" jdbcType="VARCHAR" />
<@H_502_6@result column="address" property="address" jdbcType="VARCHAR" />
<@H_502_6@result column="create_date" property="createDate" jdbcType="TIMESTAMP" />
<@H_502_6@result column="orderitem_id" property="orderitemId" jdbcType="VARCHAR" />
<!--维护一对多的关系 -->
<@H_502_6@collection property="orderitemList" ofType="pojo.Orderitem">
<@H_502_6@id column="orderitem_id" property="orderitemId"/>
<@H_502_6@result column="product_id" property="productId"/>
<@H_502_6@result column="count" property="count"/>
</@H_502_6@collection>
</@H_502_6@resultMap>
4.动态语句标签
<where> : 使用其可以代替sql语句中的where关键字,一般防止在条件查询的最外层 <if >:条件判断标签,配置属性test=" 条件字符串 ",判断是否满足条件,满足则执行,不满足则跳过
<@H_502_6@select id="findOrderItemDetail" parameterType="pojo.Orderitem" resultMap="BaseResultMap">
select orderitem.orderitem_id,product.*
from orderitem,product
<@H_502_6@where>
<@H_502_6@if test="orderitemId!=null and orderitemId!=''">
and orderitem.orderitem_id = #{orderitemId}
</@H_502_6@if>
<@H_502_6@if test="productId!=null and productId!=''">
and orderitem.product_id = #{productId}
</@H_502_6@if>
<@H_502_6@if test="count!=null">
and orderitem.count = #{count}
</@H_502_6@if>
</@H_502_6@where>
</@H_502_6@select>
<set>:常用于<update>更新语句中,替代 sql中的“set”关键字,特别是在联合<if>进行判断是,可以有效方式当某个参数为空或者不合法是错误的更新到数据库中
<@H_502_6@update id="updateByPrimaryKeySelective" parameterType="pojo.Orderitem" >
update orderitem
<@H_502_6@set >
<@H_502_6@if test="productId != null" >
product_id = #{productId,</@H_502_6@if>
<@H_502_6@if test="count != null" >
count = #{count,jdbcType=INTEGER},</@H_502_6@if>
</@H_502_6@set>
where orderitem_id = #{orderitemId,jdbcType=VARCHAR}
</@H_502_6@update>
<choose><when></when><otherwise></otherwise></choose> 标签组:也是一个用于条件判断的标签组,和<if>的不同之处在于条件从<choose>进入,去匹配<when>中的添加,一旦匹配马上结束;若到找不到匹配项,将执行<other>中的语句;可以理解为<if>是 && 关系 <choose>是 || 关系
<!-- 查询学生list,like姓名、或=性别、或=生日、或=班级,使用choose -->
<@H_502_6@select id="getStudentListChooseEntity" parameterType="StudentEntity" resultMap="studentResultMap">
SELECT * from STUDENT_TBL ST
<@H_502_6@where>
<@H_502_6@choose>
<@H_502_6@when test="studentName!=null and studentName!='' ">
ST.STUDENT_NAME LIKE CONCAT(CONCAT('%',#{studentName}),'%')
</@H_502_6@when>
<@H_502_6@when test="studentSex!= null and studentSex!= '' ">
AND ST.STUDENT_SEX = #{studentSex}
</@H_502_6@when>
<@H_502_6@when test="studentBirthday!=null">
AND ST.STUDENT_BIRTHDAY = #{studentBirthday}
</@H_502_6@when>
<@H_502_6@when test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">
AND ST.CLASS_ID = #{classEntity.classID}
</@H_502_6@when>
<@H_502_6@otherwise>
</@H_502_6@otherwise>
</@H_502_6@choose>
</@H_502_6@where>
</@H_502_6@select>
<foreach>标签:该标签的作用是遍历集合类型的条件 属性:collection=“array” / collection = “list” ----->是数组类型,还是集合类型 item=“ productId ”------> 参数名 open="(" separator="," close=")" ------>开始符号,分隔符号,结束符号 index=“ ” ---->结束下标位置,不配置该参数时,默认为全部遍历
<@H_502_6@delete id="deleteByPriKeys" parameterType="java.lang.String">
delete from product where product_Id in
<@H_502_6@foreach collection="list" item="productId" open="(" separator="," close=")">
#{productId,jdbcType = VARCHAR}
</@H_502_6@foreach>
</@H_502_6@delete>