個人筆記,如有錯誤,懇請批評指正。
修改配置文件deptMapper.xml,添加
<!-- 動態Sql語句 --><!-- 根據多個條件生成動態的sql語句,查詢信息 --><!-- 弊端:當不存在條件時,會查詢所有的數據,改用choose可以解決問題 --><select id="selectDeptUseIf" parameterType="Dept" resultMap="deptResultMap"><!-- 若字段名與屬性名一致可以直接用resultType="Dept" --> select * from dept where 1=1 <!-- 直接寫屬性,不需要#{},and不能丟 --> <if test="deptId!=null">and dept_id = #{deptId}</if> <if test="deptAddress!=null">and dept_address = #{deptAddress}</if> <if test="deptName!=null">and dept_name = #{deptName}</if></select>修改DeptDaoImpl.java,添加selectListUseIf方法:
//根據參數使用配置文件的IF語句自動填充查詢的過濾條件 public List<Dept> selectDeptUseIf(Dept dept){ List<Dept> list = null; try { session = MybatisSessionFactory.getSession(); list = session.selectList("cn.ustb.entity.DeptMapper.selectDeptUseIf", dept); session.commit(); } catch (Exception e) { e.PRintStackTrace(); session.rollback(); } return list; }修改配置文件deptMapper.xml,添加
<!-- 動態Where條件 ,一般也需要與if結合使用,與純if比較,省略了where 1=1--><!-- 如果需要去掉1=1,可用where --><select id="selectDeptUseWhere" parameterType="Dept" resultMap="deptResultMap"> select * from dept <where> <if test="deptId!=null">and dept_id = #{deptId}</if> <if test="deptAddress!=null">and dept_address = #{deptAddress}</if> <if test="deptName!=null">and dept_name = #{deptName}</if> </where></select>修改配置文件deptMapper.xml,添加
<!-- choose 用choose時,前面的條件符合就不再執行之后的條件--><select id="selectDeptUseChoose" parameterType="Dept" resultMap="deptResultMap"> select * from dept <where> <choose> <when test="deptId != null">and dept_id = #{deptId}</when> <when test="deptAddress != null">and dept_address = #{deptAddress}</when> <when test="deptName != null">and dept_name = #{deptName}</when> <otherwise>1=2</otherwise> </choose> </where></select>修改配置文件deptMapper.xml,添加
<!-- 動態set語句可以用來更新數據 --><update id="updateDeptUseSet" parameterType="Dept"> update dept <set> <if test="deptAddress!=null">dept_address = #{deptAddress}</if> <if test="deptName!=null">dept_name = #{deptName}</if> </set> where dept_id = #{deptId}</update>修改配置文件deptMapper.xml,添加
<!-- 若要查詢多個id的數據 --><!-- 定義根據多個部門ID查詢部門相關部門信息的SQL語句 ,resultMap的值是指集合里元素的類型,parameterType不用指定 --><!-- foreach --><select id="selectDeptUseForeach" resultMap="deptResultMap"> select * from dept where dept_id in (<!-- collection="array或list",array用來對應參數為數組,list對應參數為 集合 --> <foreach collection="array" item="deptId" separator=","> #{deptId} </foreach> )</select>修改配置文件deptMapper.xml,添加
<insert id="insertDeptUseInclude" parameterType="Dept"> insert into dept <include refid="key"></include> values <include refid="value"></include></insert><sql id="key"> <trim suffixOverrides="," prefix="(" suffix=")"> <if test="deptName!=null">dept_name,</if> <if test="deptAddress!=null">dept_address,</if> </trim></sql><sql id="value"> <trim suffixOverrides="," prefix="(" suffix=")"> <if test="deptName!=null">#{deptName},</if> <if test="deptAddress!=null">#{deptAddress},</if><!-- #{} necessary --> </trim></sql>(重點加入級聯的查詢語句),并映射文件信息到mybatis-config.xml中: DeptMapper.xml
<?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="cn.ustb.entity.DeptMapper"> <resultMap type="Dept" id="deptResultMap"> <id column="dept_id" property="deptId" /> <result column="dept_name" property="deptName" /> <result column="dept_address" property="deptAddress" /> </resultMap></mapper>EmpMapper.xml,配置多對一的關聯
<?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="cn.ustb.entity.EmpMapper"> <resultMap type="Emp" id="empResultMap"> <id column="emp_id" property="empId"/> <result column="emp_name" property="empName"/> <result column="emp_age" property="empAge"/> <result column="emp_gender" property="empGender"/> <association property="dept" column="dept_id" resultMap="cn.ustb.entity.DeptMapper.deptResultMap"></association> </resultMap> <select id="selectEmp" parameterType="string" resultMap="empResultMap"> select e.*,d.* from emp e inner join dept d on e.dept_id = d.dept_id where e.emp_name = #{empName} </select></mapper>DeptMapper.xml文件,配置resultMap(重點是collection配置)和查詢SQL語句:
<?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="cn.ustb.entity.DeptMapper"> <resultMap type="Dept" id="deptResultMap"> <id column="dept_id" property="deptId" /> <result column="dept_name" property="deptName" /> <result column="dept_address" property="deptAddress" /> <collection property="emps" resultMap="cn.ustb.entity.EmpMapper.empResultMap"></collection><!-- 沒有column --> </resultMap> <select id="selectDept" parameterType="String" resultMap="deptResultMap"> select d.*,e.* from dept d inner join emp e on d.dept_id = e.dept_id where d.dept_name = #{deptName} </select></mapper>不用配置對一關聯
<?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="cn.ustb.entity.EmpMapper"> <resultMap type="Emp" id="empResultMap"> <id column="emp_id" property="empId"/> <result column="emp_name" property="empName"/> <result column="emp_age" property="empAge"/> <result column="emp_gender" property="empGender"/></resultMap></mapper>DeptMapper.xml *值得注意的是,如果在各自的resultMap相互配置了引用,在查詢時會導致堆棧溢出的異常。解決的辦法是在映射文件中分割resultMap,在查詢時查詢一方的同時避免迭代。*
<?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="cn.ustb.entity.DeptMapper"> <!-- 緩存的第二級開關,寫上即開啟 --> <!-- 一級默認開啟,二級默認關閉,三級默認開啟 --> <cache eviction="LRU" size="2" readOnly="false"></cache> <resultMap type="Dept" id="deptResultMap"> <id column="dept_id" property="deptId" /> <result column="dept_name" property="deptName" /> <result column="dept_address" property="deptAddress" /> </resultMap> <resultMap type="Dept" id="deptExtResultMap" extends="deptResultMap"> <collection property="emps" resultMap="cn.ustb.entity.EmpMapper.empResultMap"></collection><!-- resultMap是empResultMap而不是empExtResultMap,即沒有配置多的屬性,避免了相互查詢時的內存溢出 --> </resultMap> <select id="selectDept" parameterType="String" resultMap="deptExtResultMap" useCache="true"><!-- 緩存的三級開關 語句級,默認開啟 --> select d.*,e.* from dept d inner join emp e on d.dept_id = e.dept_id where d.dept_name = #{deptName} </select> <select id="selectDeptById" parameterType="string" resultMap="deptExtResultMap"> select d.*,e.* from dept d inner join emp e on d.dept_id = e.dept_id where d.dept_id = #{deptId} </select></mapper>EmpMapper.xml
<?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="cn.ustb.entity.EmpMapper"> <resultMap type="Emp" id="empResultMap"> <id column="emp_id" property="empId" /> <result column="emp_name" property="empName" /> <result column="emp_age" property="empAge" /> <result column="emp_gender" property="empGender" /> </resultMap> <resultMap type="Emp" id="empExtResultMap" extends="empResultMap"> <association property="dept" column="dept_id" resultMap="cn.ustb.entity.DeptMapper.deptResultMap"></association> <!-- 同樣,引用的是deprResultMap,沒有配置多的屬性 --> </resultMap> <select id="selectEmp" parameterType="string" resultMap="empExtResultMap"> select e.*,d.* from emp e inner join dept d on e.dept_id = d.dept_id where e.emp_name = #{empName} </select></mapper>DeptDaoImpl.java,查詢部門員工信息,返回類型為List,關鍵代碼:
public List<Dept> selectDeptEmpList(Dept dept){ SqlSession session=null; List<Dept> deps=null; try{ session=MyBatisUtil.getSession(); deps=session.selectList("cn.ustb.entity.DeptMapper.selectDeptEmpList",dept); session.commit(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); session.rollback(); }finally{ MyBatisUtil.closeSession(); } return deps; }EmpDaoImpl.java查詢員工及其所在部門信息,返回類型為List< Emp >,關鍵代碼
public List<Emp> selectEmpDeptList(Emp emp){ SqlSession session=null; List<Emp> emps=null; try{ session=MyBatisUtil.getSession(); emps=session.selectList("cn.ustb.entity.EmpMapper.selectEmpDeptList",emp); session.commit(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); session.rollback(); }finally{ MyBatisUtil.closeSession(); } return emps; }Mybatis和hibernate一樣,也使用緩存;緩存分為一級緩存和二級緩存,一級緩存指在SqlSession內共享數據;二級緩存能被所有的SqlSession共享。
在mybatis-config.xml的主配置文件中進行配置:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <settings> <!-- 默認有啟用全局緩存的,禁用可以把value設為false,如果這里設為false,Mapper.xml或SQL語句級的緩存配置不再起作用 --> <setting name="cacheEnabled" value="true"/> </settings><!—省略其它配置信息 --></configuration>使用二級緩存機制:需要開啟全局緩存,文件級緩存 ,語句級緩存,才能使用二級緩存。默認情況下文件級緩存沒有開啟。
<!-- 緩存的第二級開關,寫上即開啟 --> <!-- 一級默認開啟,二級默認關閉,三級默認開啟 --> <cache eviction="LRU" size="2" readOnly="false"></cache>關于二級緩存的配置,在使用者手冊中有詳細說明,摘取如下內容:
可用的回收算法如下: ? LRU – 最近最少使用:移出最近最長時間內都沒有被使用的對象。 ? FIFO – 先進先出:移除最先進入緩存的對象。 ? SOFT – 軟引用: 基于垃圾回收機制和軟引用規則來移除對象(空間內存不足時才進行回收)。 ? WEAK – 弱引用: 基于垃圾回收機制和弱引用規則(垃圾回收器掃描到時即進行回收)。 默認使用LRU。
flushInterval :設置任何正整數,代表一個以毫秒為單位的合理時間。默認是沒有設置,因此 沒有刷新間隔時間被使用,在語句每次調用時才進行刷新。 Size:屬性可以設置為一個正整數,您需要留意您要緩存對象的大小和環境中可用的內存空間。 默認是1024。 readOnly:屬性可以被設置為true 或false。只讀緩存將對所有調用者返回同一個實例。因此這些對象都不能被修改,這可以極大的提高性能。可寫的緩存將通過序列化來返回一個緩存對象的拷貝。這會比較慢,但是比較安全。所以默認值是false。
在相關的Mapper.xml文件中配置SQL查詢,關鍵代碼如下(示例):
<!-- useCache默認值為true,設為false時緩存不起作用 --> <select id="selectOne" parameterType="int" resultMap="deptResultMap" useCache="true" > select * from dept where dept_id=#{id} </select>如果 MyBatis 使用 XML 配置,那不可避免地會遇到一些對 XML 來說是特殊的字符。如小于號 “<”,因此要進行轉義。主要有兩個方式:
下面是五個在 XML 文檔中預定義好的轉義實體: - <; < 小于號 - >; > 大于號 - &; & - &apos; ’ 單引號 - "; ” 雙引號 - 小于等于“<=”,其轉義為:<;= - 大小等于“>=”,轉義為:>;=
CDATA 部件以”
<select id= "selectBlog_use_collection" resultMap= "blogResult" ><![CDATA[ SELECT id , title, author_id as authored FROM BLOG WHERE ID > 0 and ID < 10 ]]> </select>“
新聞熱點
疑難解答