mapper映射文件配置之select、resultMap
原文:http://www.cnblogs.com/dongying/p/4073259.html
mybatis通過resultMap能幫助我們很好地進行高級映射。
下面就開始看看select 以及 resultMap的用法:
<select
<!-- 1. id (必須配置)
id是命名空間中的唯一標識符,可被用來代表這條語句。
一個命名空間(namespace) 對應一個dao接口,
這個id也應該對應dao裏面的某個方法(相當於方法的實現),因此id 應該與方法名一致 -->
id="selectUser"
<!-- 2. parameterType (可選配置, 默認爲mybatis自動選擇處理)
將要傳入語句的參數的完全限定類名或別名, 如果不配置,mybatis會通過ParameterHandler 根據參數類型默認選擇合適的typeHandler進行處理
parameterType 主要指定參數類型,可以是int, short, long, string等類型,也可以是複雜類型(如對象) -->
parameterType="int"
<!-- 3. resultType (resultType 與 resultMap 二選一配置)
resultType用以指定返回類型,指定的類型可以是基本類型,可以是java容器,也可以是javabean -->
resultType="hashmap"
<!-- 4. resultMap (resultType 與 resultMap 二選一配置)
resultMap用於引用我們通過 resultMap標籤定義的映射類型,這也是mybatis組件高級複雜映射的關鍵 -->
resultMap="userResultMap"
<!-- 5. flushCache (可選配置)
將其設置爲 true,任何時候只要語句被調用,都會導致本地緩存和二級緩存都會被清空,默認值:false -->
flushCache="false"
<!-- 6. useCache (可選配置)
將其設置爲 true,將會導致本條語句的結果被二級緩存,默認值:對 select 元素爲 true -->
useCache="true"
<!-- 7. timeout (可選配置)
這個設置是在拋出異常之前,驅動程序等待數據庫返回請求結果的秒數。默認值爲 unset(依賴驅動)-->
timeout="10000"
<!-- 8. fetchSize (可選配置)
這是嘗試影響驅動程序每次批量返回的結果行數和這個設置值相等。默認值爲 unset(依賴驅動)-->
fetchSize="256"
<!-- 9. statementType (可選配置)
STATEMENT,PREPARED 或 CALLABLE 的一個。這會讓 MyBatis 分別使用 Statement,PreparedStatement 或 CallableStatement,默認值:PREPARED-->
statementType="PREPARED"
<!-- 10. resultSetType (可選配置)
FORWARD_ONLY,SCROLL_SENSITIVE 或 SCROLL_INSENSITIVE 中的一個,默認值爲 unset (依賴驅動)-->
resultSetType="FORWARD_ONLY">
新建兩個表:
t_course:
t_student:
新建對應的實體類:
Course.java:
public class Course {
private int id;
private String name;
private int deleteFlag;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDeleteFlag() {
return deleteFlag;
}
public void setDeleteFlag(int deleteFlag) {
this.deleteFlag = deleteFlag;
}
}
Student.java:
public class Student {
private int id;
private String idCard;
private String name;
//這裏表示一對多 一個學生可以學多門課程
private List<Course> courseList;
private int deleteFlag;
//因爲這個實體裏面包含了對象courseList 所以mybatis用resultMap指定時,默認情況下會調用這個類裏面的無參構造函數用來初始化student對象,然後再把courseList set進去 但是我們寫了一個有參的構造函數,所以就不會去調構造器中默認的無參構造,所以在這裏我們要寫一個無參的構造函數,否則會報mybatis初始化錯誤。 當然也可以在resultMap標籤中使用<constructor>標籤,指定構造函數初始化
public Student(){
}
private Student(int id ,String idCard ,String name ,List<Course> courseList ,int deleteFlag){
this.id = id;
this.idCard = idCard;
this.name = name;
this.courseList = courseList;
this.deleteFlag = deleteFlag;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Course> getCourseList() {
return courseList;
}
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
public int getDeleteFlag() {
return deleteFlag;
}
public void setDeleteFlag(int deleteFlag) {
this.deleteFlag = deleteFlag;
}
}
新建對應的dao層:
CourseDao:
public interface CourseDao {
Course findCourseById(int courseId);
}
StudentDao:
public interface StudentDao {
Student findStudentById(String idCard);
}
新建對應的映射文件:
courseDao-mapping:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.example.demo.dao.CourseDao">
<!--
1.此處直接將resultType 設置爲courseEntity(別名) ,如果沒有設置別名,那麼resultType = com.example.demo.dao.CourseDao。
2.Course.java中的屬性名與數據庫字段名不一致,下面,我就在sql語句中用了as, 使之匹配,當然方法不止一種,
在學習了resultMap之後,能更直觀的方式去將javabean中的屬性與數據庫字段名保持一致
3.findCourseById 與CourseDao中findCourseById方法對應, 那麼傳入的參數名稱以及類型也應該保持對應關係。
4.可以看到,在sql語句中,通過#{}表達式可以獲取參數。
5.下面這條sql語句,實際上的形式是怎麼樣的?還記得之前說過,mybatis默認爲preparedStatement吧,那麼,用我們jdbc代碼來看,它其實就是:
select course_id as id, course_name as name, course_delete_flg as deleteFlag from t_course where course_id=?
-->
<select id="findCourseById" resultType="CourseEntity" >
select course_id as id, course_name as name, course_delete_flg as deleteFlag from t_course where course_id=#{courseId}
</select>
</mapper>
測試:
@Test
public void findCourseById() {
SqlSessionFactory sqlSessionFactory = getSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
CourseDao courseDao = sqlSession.getMapper(CourseDao.class);
Course course = courseDao.findCourseById(1);
}
//Mybatis 通過SqlSessionFactory獲取SqlSession, 然後才能通過SqlSession與數據庫進行交互
private static SqlSessionFactory getSessionFactory() {
SqlSessionFactory sessionFactory = null;
String resource = "xml/configuration.xml";
try {
sessionFactory = new SqlSessionFactoryBuilder().build(Resources
.getResourceAsReader(resource));
} catch (IOException e) {
e.printStackTrace();
}
return sessionFactory;
}
mybatis通過resultMap配置來處理一對多, 甚至於多對多,一對一的關係:
resultMap中的配置:
<!--
1.type 對應類型,可以是javabean, 也可以是其它
2.id 必須唯一, 用於標示這個resultMap的唯一性,在使用resultMap的時候,就是通過id指定
-->
<resultMap type="" id="">
<!-- id, 唯一性,注意啦,這個id用於標示這個javabean對象的唯一性, 不一定會是數據庫的主鍵(不要把它理解爲數據庫對應表的主鍵)
property屬性對應javabean的屬性名,column對應數據庫表的列名
(這樣,當javabean的屬性與數據庫對應表的列名不一致的時候,就能通過指定這個保持正常映射了)
-->
<id property="" column=""/>
<!-- result與id相比, 對應普通屬性 -->
<result property="" column=""/>
<!--
constructor對應javabean中的構造方法
-->
<constructor>
<!-- idArg 對應構造方法中的id參數 -->
<idArg column=""/>
<!-- arg 對應構造方法中的普通參數 -->
<arg column=""/>
</constructor>
<!--
collection,對應javabean中容器類型, 是實現一對多的關鍵
property 爲javabean中容器對應字段名
column 爲體現在數據庫中列名
ofType 就是指定javabean中容器指定的類型
-->
<collection property="" column="" ofType=""></collection>
<!--
association 爲關聯關係,是實現N對一的關鍵。
property 爲javabean中容器對應字段名
column 爲體現在數據庫中列名
javaType 指定關聯的類型
-->
<association property="" column="" javaType=""></association>
</resultMap>
studentDao-mapping:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.example.demo.dao.StudentDao">
<!-- 這兒定義一個resultMap -->
<resultMap type="StudentEntity" id="studentMap">
<!--
數據庫中主鍵是id, 但是我這兒卻是指定idCard爲主鍵,爲什麼?
剛剛講了,id用來表示唯一性, 我們可以認爲只要idCard一樣,那麼他就是同一個學生。
如果此處用數據庫中id, 那麼mybatis將會認爲數據庫中每條記錄都是一個student, 這顯然不符合邏輯
-->
<id property="idCard" column="stu_id_card"/>
<result property="id" column="stu_id"/>
<result property="name" column="stu_name"/>
<result property="deleteFlag" column="stu_delete_flg"/>
<!--
這兒就是實現一對多的關鍵。
在Student中,courseList爲List<Course>, 因此,ofType也應該與之對應(當然,我用了別名,不然要蛋疼的寫全名了)。
collection的子標籤是在指定Course的映射關係(由於Course的javabean的屬性名與數據庫的列名不一致)
-->
<collection property="courseList" column="stu_course_id" ofType="CourseEntity">
<id property="id" column="course_id"/>
<result property="name" column="course_name"/>
<result property="deleteFlag" column="course_delete_flg"/>
</collection>
</resultMap>
<!-- 這兒將返回類型設置成了上面指定的studentMap -->
<select id="findStudentById" resultMap="studentMap">
SELECT s.*, c.* FROM t_student s LEFT JOIN t_course c ON s.stu_course_id=c.course_id WHERE s.stu_id_card=#{idCard}
</select>
</mapper>
測試:
@Test
public void findStudentById() {
SqlSessionFactory sqlSessionFactory = getSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
Student student = studentDao.findStudentById("20140101");
List<Course> courseList = student.getCourseList();
for (Course course: courseList) {
System.out.println(course.getId() + " " + course.getName());
}
}
//Mybatis 通過SqlSessionFactory獲取SqlSession, 然後才能通過SqlSession與數據庫進行交互
private static SqlSessionFactory getSessionFactory() {
SqlSessionFactory sessionFactory = null;
String resource = "xml/configuration.xml";
try {
sessionFactory = new SqlSessionFactoryBuilder().build(Resources
.getResourceAsReader(resource));
} catch (IOException e) {
e.printStackTrace();
}
return sessionFactory;
}