Mybatis映射配置文件<select>的學習
阿新 • • 發佈:2017-11-16
har ssi cti ava 映射 step char 實體類 jdb
resultMap的使用
當查詢的表的列名與實體類的屬性名不同時,有三種方式來處理:
1、使用SQL的別名 如:select user_name userName from user
2、如果符合駝峰命名,在setting中開啟mapUnderscoreToCamelCase
3、設置resultMap,自定義結果集映射規則,不能和resultType同時使用
例:
Type指定映射實體類
<resultMap id="BaseResultMap" type="com.cky.pojo.User" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="user_name" property="userName" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> <result column="age" property="age" jdbcType="INTEGER" /> </resultMap>
<select>返回集合
返回List
如果返回的是一個集合,ResultType返回值類型依然要寫集合中元素的類型
返回Map
<select>返回一條記錄的map,key就是列名,值就是對應的值
一條記錄時 設置 resultType=”map”
多條記錄時 設置 resultType=”Entity”一條記錄的實體類,方法添加@MapKey(“id”)指定哪個屬性作為map的key
<select>級聯查詢1:1
Sql使用多表關聯查詢,然後指定resultMap 如下:
方法一、
<resultMap id="BaseResultMap" type="com.cky.pojo.User" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="user_name" property="userName" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> <result column="age" property="age" jdbcType="INTEGER" /> <result column=”c_id” property=”course.id”> <result column=”c_name” property=”course.name”> </resultMap>
方法二、
<resultMap type="DifResultMap" id="com.cky.pojo.User"> <id column="id" property="id" jdbcType="INTEGER" /> <result column="user_name" property="userName" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> <result column="age" property="age" jdbcType="INTEGER" /> <association property="course" javaType="com.cky.pojo.Course"> <id column="c_id" property="cId" /> <result column="c_name" property="cName"/> </association> </resultMap>
方法三、分步查詢:使用select指定的方法(傳入column指定的這列參數的值)查出對象,並封裝給property
<resultMap type="StepResultMap" id="com.cky.pojo.User"> <id column="id" property="id" jdbcType="INTEGER" /> <result column="user_name" property="userName" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> <result column="age" property="age" jdbcType="INTEGER" /> <!-- association中指定某一select的查詢結果作為property指定的對象 --> <association property="course" select="com.cky.dao.getCourse" column="c_id"> </association> </resultMap>
優點,可以使用延遲加載,開啟方式:設置setting的
lazyLoadingEnabled true
aggressiveLazyLoading false
顯式的配置可以防止版本更新帶來的問題
<select>級聯查詢1:n
方法一、嵌套結果集,sql使用連接查詢
<mapper namespace="com.cky.dao.CourseDao"> <resultMap type="com.cky.pojo.Course" id="myCourse"> <id column="c_id" property="cId"/> <result column="c_name" property="cName"/> <collection property="students" ofType="com.cky.pojo.Student"> <id column="id" property="id"/> <result column="user_name" property="userName"/> <result column="password" property="password"/> <result column="age" property="age"/> </collection> </resultMap> </mapper>
方法二、分步查詢
<mapper namespace="com.cky.dao.CourseDao"> <resultMap type="com.cky.pojo.Course" id="myCourse"> <id column="c_id" property="cId"/> <result column="c_name" property="cName"/> <collection perperty=”students” Select=”com.cky.dao.Student.getStudentByCId” Column=”c_id” </collection> </resultMap> </mapper>
Mybatis映射配置文件<select>的學習