一、基础应用
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
</select>
<!--插入语句-->
<insert id="insert" parameterType="pojo.OrderTable" >
insert into ordertable (order_id, cid, address,
create_date, orderitem_id)
values (#{orderId,jdbcType=VARCHAR}, #{cid,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR},
#{createDate,jdbcType=TIMESTAMP}, #{orderitemId,jdbcType=VARCHAR})
</insert>
<!--删除语句-->
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from ordertable
where order_id = #{orderId,jdbcType=VARCHAR}
</delete>
<!--修改语句-->
<update id="updateByPrimaryKey" parameterType="pojo.OrderTable" >
update ordertable
set cid = #{cid,jdbcType=VARCHAR},
address = #{address,jdbcType=VARCHAR},
create_date = #{createDate,jdbcType=TIMESTAMP},
orderitem_id = #{orderitemId,jdbcType=VARCHAR}
where order_id = #{orderId,jdbcType=VARCHAR}
</update>
parameterType="" >>>表示该sql语句中需要传入的参数, 类型要与对应的接口方法的类型一致【可选】
resultMap=“ ”>>> 定义出参,调用已定义的<resultMap>映射管理器的id值
resultType=“ ”>>>定义出参,匹配普通Java类型或自定义的pojo【出参类型若不指定,将为语句类型默认类型,如<insert>语句返回值为int】
二、sql片段标签<sql>:
通过该标签可定义能复用的sql语句片段,在执行sql语句标签中直接引用即可。这样既可以提高编码效率,还能有效简化代码,提高可读性 。
<!--定义sql片段-->
o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count
</sql>
<include refid="orderAndItem" />
三、映射管理器 resultMap:
映射管理器,是Mybatis中最强大的工具,使用其可以进行实体类之间的关系,并管理结果和实体类间的映射关系 需要配置的属性
1)一对一关系<assocation property = " " javaType=" "> property = “ ” 被维护实体在宿主实体中的属性名,javaType = " " 被维护实体的类型
<association property="product" javaType="pojo.Product">
<id column="product_id" property="productId"/>
<result column="product_factroy" property="productFactroy"/>
<result column="product_store" property="productStore"/>
<result column="product_descript" property="productDescript"/>
</association>
<collection property="orderitemList" ofType="pojo.Orderitem">
<id column="orderitem_id" property="orderitemId"/>
<result column="product_id" property="productId"/>
<result column="count" property="count"/>
</collection>
四:常用的动态语句标签
<where> : 使用其可以代替sql语句中的where关键字,一般防止在条件查询的最外层
<if >:条件判断标签,配置属性test=" 条件字符串 ",判断是否满足条件,满足则执行,不满足则跳过
<set>:常用于<update>更新语句中,替代 sql中的“set”关键字,特别是在联合<if>进行判断是,可以有效方式当某个参数为空或者不合法是错误的更新到数据库中
<set >
<if test="productId != null" >
product_id = #{productId,jdbcType=VARCHAR},
</if>
<if test="count != null" >
count = #{count,jdbcType=INTEGER},
</if>
</set>
<choose><when></when><otherwise></otherwise></choose> 标签组:也是一个用于条件判断的标签组,和<if>的不同之处在于条件从<choose>进入,去匹配<when>中的添加,一旦匹配马上结束;若到找不到匹配项,将执行<other>中的语句;可以理解为<if>是 && 关系 <choose>是 || 关系
<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!='' ">
AND ST.CLASS_ID = #{classEntity.classID}
</when>
<otherwise>
</otherwise>
</choose>
</where>
<foreach>标签:该标签的作用是遍历集合类型的条件
<foreach collection="list" item="productId" open="(" separator="," close=")">
#{productId,jdbcType = VARCHAR}
</foreach>
属性:collection=“array” / collection = “list” ----->是数组类型,还是集合类型
item=“ productId ”------> 参数名
open="(" separator="," close=")" ------>开始符号,分隔符号,结束符号
index=“ ” ---->结束下标位置,不配置该参数时,默认为全部遍历
五、mabatis分页
1.内存分页,也就是假分页,使用RowBounds类 。原理是查出所有数据,然后根据游标的方式来截取需要的数据,如果数据量大,会出现开销大和内存溢出
2.自定义,使用limit的sql。