一、輸入映射類型
parameterType定義輸入到sql中的映射類型,可以是 簡單類型 、po類對象(可自動生成 或 手動定義)、 pojo包裝對象(用於綜合查詢,UserCustom用戶自定義對象 、UserQueryvo視圖層對象包)、hashMap對象、集合對象以及數組(使用foreach拼接動態sql)等,建議使用pojo包裝對象或map對象,以保證Dao的通用性。
1、基本數據類型
//基本類型 <select id="findEmpByName" parameterType="int” > select * from emp where empno=#{empno} </select> //類型打包器 <select id="findEmpByName" parameterType="java.lang.Integer” > select * from emp where empno=#{empno} </select>
2、pojo類型
//pojo類對象 <select id="findEmpByName" parameterType="Po.Emp” > select * from emp where empno=#{empno} </select> //pojo包裝類對象 <select id="findEmpByName" parameterType="Po.EmpVo” > select * from emp where otherMsg = #{Emp.otherMsg} </select>
3、hashMap對象
<select id="findUserByHashmap" parameterType="map" resultType="user"> select * from user where id=#{id} and username like '%${username}%' </select> //構造查詢條件Hashmap對象 HashMap<String, Object> map = new HashMap<String, Object>(); map.put("id", 1); map.put("username", "管理員"); //傳遞Hashmap對象查詢用戶列表 List<User>list = userMapper.findUserByHashmap(map);
若傳遞的map中的key和sql中解析的key不一致。Eclipse不會報錯,只是通過key獲取值為空。
4、占位字符
(1)#{}占位符號
SQLl語句中【 #{} 】示一個占位符即【?】,通過#{}可以實現向preparedStatement中的預處理語句中設置占位符號,mybatis底層通過ognl從pojo中獲取屬性值,后再將輸入變量id傳到sql。
<select id="findEmpById" parameterType="int" resultType="Emp"> select * from emp where empno = #{對象.屬性值} </select>
使用占位符#{}可以有效防止sql注入,在使用時不需要關心參數值的類型,mybatis會自動進行java類型和jdbc類型的轉換。#{}可以接收簡單類型、pojo、HashMap。如果parameterType傳輸單個簡單類型值,#{}括號中可以是value或其它名稱;如果${}接收pojo對象值,通過【屬性.對象屬性】獲取對象屬性值。特別的:String、Array、類型打包器都獲取屬性值用value。
(2)拼接字符
【${}】表示拼接sql串,通過${}可以將parameterType 傳入的內容拼接在sql中且不進行jdbc類型轉換。${}可以接收簡單類型值、pojo、HashMap。如果parameterType傳輸單個簡單類型值,如果接收簡單類型,${}括號中只能是value;如果${}接收pojo對象值,通過【屬性.對象屬性】獲取對象屬性值。
① 用於使用通配符進行模糊查找(可能會引起sql注入)
<select id="selectEmpByName" parameterType="string" resultType="Emp"> select * from emp where ename like '%${value}%' </select> //如果使用占位符號則必須人為在傳參數中加%,較為麻煩 List<User> list = userMapper.selectUserByName("%管理員%"); //如果使用${}拼接符號則不用人為在參數中加%,建議使用 List<User>list = userMapper.selectUserByName("管理員");
//如果輸入的是 【OR 1=1 OR】,會返回表中的全部記錄(SQL注入)
//SQL語句為:select * from emp where ename like '%’ OR 1=1 OR '%’'
② 用於在SQL語句中拼接【列名 或 表名】
如果將列名通過參數傳入sql,根據傳的列名進行排序,應該使用拼接字符,使用#{}將無法實現此功能。
//計算記錄數,拼接表名 <select id="findTableCount" parameterType ="java.lang.String" resultType="int"> select count(*) from ${value} </select> //可以根據傳入的表名,返回不同表中的記錄數 int i = sqlSession.selectOne("EmpDao.findTableCount", "dept"); //order by排序,拼接列名 ORDER BY ${columnName}
二、ResultType
使用resultType可以將查詢結果映射為pojo類型,但需要pojo的屬性名和sql查詢的列名一致方可映射成功。如果查詢出來的列名和pojo中的屬性名全部不一致,沒有創建pojo對象。只要查詢出來的列名和pojo中的屬性有一個一致,就會創建pojo對象。
1、基本數據類型
<select id="findEmpCount" resultType="int"> select count(1) from emp </select> //原始Dao開發方式 int i = sqlSession.selectOne("EmpDao.findEmpCount", null); //Mapper代理的方式 int count = empMapper.findEmpCount(emp);
輸出簡單類型必須查詢出來的結果集只有一條記錄,最終將第一個字段的值轉換為輸出類型。
2、pojo類對象
//輸出pojo對象 <select id="findUserById" parameterType="int" resultType="user"> select * from user where id = #{id} </select> //輸出pojo列表 <select id="findUserByUsername" parameterType="string" resultType="user"> select * from user where username like '%${value}%' </select>
輸出pojo對象和輸出pojo列表在sql中定義的resultType是一樣的,但在mapper.java指定的方法返回值類型不一樣。生成的動態代理對象中是根據mapper方法的返回值類型確定是調用selectOne(返回單個對象調用)還是selectList (返回集合對象調用 )。
返回單個pojo對象,要保證sql查詢出來的結果集為單條,內部使用session.selectOne方法調用,mapper接口使用pojo對象作為方法返回值。
返回pojo列表,表示查詢出來的結果集可能為多條,內部使用session.selectList方法,mapper接口使用List<pojo>對象作為方法返回值。
3、hashMap
輸出pojo對象可以改用hashmap輸出類型,將輸出的字段名稱作為map的key,value為字段值。
<select id="findEmpById" parameterType="int" resultType="map"> select * from emp where empno = #{empno} </select> //查詢記錄 HashMap<String, Object> emp = sqlSession.selectOne("EmpDao.findEmpById", 7369); //獲取鍵值對 System.out.println(""+emp.get("EMPNO")+ emp.get("ename"));
三、ResultMap結果集
如果sql查詢字段名和pojo的屬性名不一致,可以通過resultMap將字段名和屬性名作一個對應關系 ,但實質上還需要將查詢結果映射到pojo對象中。
resultMap還可以實現將查詢結果映射為復雜類型的pojo,完成高級輸出結果映射。比如在查詢結果映射對象中包括pojo和list實現關聯查詢。
//定義ResultMap
<resultMap type="po.Emp" id="empResultMap"> <id column="empno" property="id"/> <result column="ename" property="name"/> </resultMap>
//引用ResultMap
<select id="findEmpById" parameterType="int" resultType="empResultMap"> select * from emp where empno = #{empno} </select
(1)定義resultMap的參數含義
type:resultMap最終映射的java對象類型的 全名 或 別名
id:對resultMap的唯一標識。
(2) 定義映射關系的參數含義
<id />:查詢結果集的唯一標識。如果是多個字段為復合唯一約束則定義多個<id/>。
<result />:對普通名映射定義。
Column:表示sql查詢出來的字段名 或 別名。
property:type指定的pojo類型中的屬性名或 別名。