Mybatis接口绑定与动态SQL

一、Mybatis接口绑定方法及多参数传递

1.作用:

       实现创建一个接口后把mapper.xml生成接口的实现类,通过接口对象直接可以获取xml中的sql

2.实现方式:

     定义一个接口,使得mapper.xml文件中的<mapper>标签中的namespace属性为接口的全限定名  sql语句标签的id和接口中的方法名相同。参数类型的parameterType可以不写,直接在方法中传递即可,参数取值时基本类型可以直接用#{0}、#{1}取或param1,param2取。也可以在接口的形参前加@Param("sname")的形式,取值时直接用#{sname}取。对于对象还是用#{属性名}取值。详细见代码:

package com.test.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.test.pojo.PageInfo;
import com.test.pojo.Student;

/**
 * @author QuLei
 *定义处理学生信息表的接口
 */
public interface StudentMapper {
	List<Student> selePage(PageInfo pageInfo);
	long selCountByPageInfo(@Param("sname")String sname, @Param("tname")String tname);
}
<?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.test.mapper.StudentMapper">
	<select id="selePage" resultType="Student" >
		select * from student 
		<where>
			<if test="sname != null and sname != ''">
				<bind name="sname" value="'%'+sname+'%'"/>
				name like #{sname}
			</if>
			<if test="tname!=null and tname !=''">
				<bind name="tname" value="'%'+tname+'%'"/>
				and tid in (select id from teacher where name like #{tname})
			</if>
		</where>
		limit #{startNum},#{pageSize}
	</select>
	
	<select id="selCountByPageInfo" resultType="long">
		select count(*) from student
		<where>
			<if test="sname != null and sname != ''">
				<bind name="sname" value="'%'+sname+'%'"/>
				name like #{sname}
			</if>
			<if test="tname!=null and tname !=''">
				<bind name="tname" value="'%'+tname+'%'"/>
				and tid in (select id from teacher where name like #{tname})
			</if>
		</where> 
	</select>
</mapper>

二、动态SQL

1.根据不同的条件要执行不同的SQL命令,称为动态SQL.

2.Mybatis中的动态sql标签:

  • if  加条件判断,如果条件为真则执行(注意:里面的提哦案件是直接判断  使用的是OGNL表达式 另外&&  和 ||要换成  and   和 or)如代码:
<mapper namespace="com.test.mapper.StudentMapper">
	<select id="selePage" resultType="Student" >
		select * from student 
		<where>
			<if test="sname != null and sname != ''">
				<bind name="sname" value="'%'+sname+'%'"/>
				name like #{sname}
			</if>
			<if test="tname!=null and tname !=''">
				<bind name="tname" value="'%'+tname+'%'"/>
				and tid in (select id from teacher where name like #{tname})
			</if>
		</where>
		limit #{startNum},#{pageSize}
	</select>
  • choose (when, otherwise)  和java中的switch一样只执行一个官方解释为:(Sometimes we don’t want all of the conditionals to apply, instead we want to choose only one case among many options. Similar to a switch statement in Java, MyBatis offers a choose element.)实例代码:
<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>
  • where 一般会和<if> 一起使用,如果结果中有内容就会自动加上where关键字,并且去掉第一个and用于条件部分。实例代码:
<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG
  <where>
    <if test="state != null">
         state = #{state}
    </if>
    <if test="title != null">
        AND title like #{title}
    </if>
    <if test="author != null and author.name != null">
        AND author_name like #{author.name}
    </if>
  </where>
</select>
  •  set   用于更新语句中在前面添加  set关键字,并且去掉结尾的","   实例代码:
<update id="updateAuthorIfNecessary">
  update Author
    <set>
      <if test="username != null">username=#{username},</if>
      <if test="password != null">password=#{password},</if>
      <if test="email != null">email=#{email},</if>
      <if test="bio != null">bio=#{bio}</if>
    </set>
  where id=#{id}
</update>
  • trim   添加或删除  有在前面添加和在后面添加以及去掉多余的部分,在前面去掉和在后面去掉  有四个属性 prefix前面添加内容 prefixOverrides去掉前面多余的内容 suffix 尾部添加内容  suffixOverrides 去掉尾部多余的内容
  • foreach  用于遍历集合  collection属性要遍历的集合    item迭代变量,#{迭代变量}获取内容  open 循环左边添加的内容    close循环右边添加的内容   separator每次循环时,元素之间的分隔符   实例代码:                                                                 注意:可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象传递给 foreach 作为集合参数。当使用可迭代对象或者数组时,index 是当前迭代的次数,item 的值是本次迭代获取的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。
<select id="selIn" parameterType="list" resultType="log">
    select * from log where id in
    <foreach collection="list" item="abc" open="(" close=")" separator=",">
	    #{abc}
    </foreach>
</select>
  • bind  给某个值绑定上一些内容(一般用于模糊查询或处理数据库中存储的钱前面加上符号)两个属性  name  要绑定的变量(如果为基本类型要用@Param注解来绑定) value中拼接字符串  将值拼接成想要的形式  这里注意使用字符串拼接的形式具体实例代码:
<if test="sname != null and sname != ''">
    <bind name="sname" value="'%'+sname+'%'"/>
    name like #{sname}
</if>
  • <sql>和<include>某些SQL片段如果希望复用时,可以使用<sql>定义这个片段,然后在需要的地方引入即可  只需要 <include>中的refid属性引用sql标签的id即可,见实例代码:
<sql id="mysql">
    id,accin,accout,money
<sql>


<select id="">
    select <include refid="mysql"></include>
    from log
<select>

三、Mybatis缓存

1.一级缓存

是session级别的缓存

2.二级缓存

为sqlsessionFactory级别的缓存,二级缓存一般缓存访问量大,不易被修改的数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值