MyBatis mapper.xml配置一对一关系映射的几种方式整理
2015-09-07 16:15
246 查看
所需的类:
mapper.xml配置:
第一种:嵌套(同个xml内)结果 ResultMap
在Student的ResultMap中使用association 标签引用Address的ResultMap的id
第二种:使用圆点记法为内嵌的对象的属性赋值
在resultMap中,Student 的address属性使用了圆点记法
被赋上了address对应列的值
第三种:使用association定义内联的 resultMap
需注意:association javaType=”Address”
第四种:嵌套select查询实现一对一关系映射
在此方式中,association 元素的 select属性被设置成了id为 findAddressById 的语句,然后查询结果赋值
参考:Java Persistence with MyBatis 3
public class Student { private Integer studId; private String name; private String email; private Date dob; private Address address; //get and set method ...... } public class Address { private Integer addrId; private String street; private String city; private String state; private String zip; private String country; //get and set method ...... }
mapper.xml配置:
第一种:嵌套(同个xml内)结果 ResultMap
在Student的ResultMap中使用association 标签引用Address的ResultMap的id
...... <resultMap type="Address" id="AddressResult"> <id property="addrId" column="addr_id" /> <result property="street" column="street" /> <result property="city" column="city" /> <result property="state" column="state" /> <result property="zip" column="zip" /> <result property="country" column="country" /> </resultMap> <resultMap type="Student" id="StudentWithAddressResult"> <id property="studId" column="stud_id" /> <result property="name" column="name" /> <result property="email" column="email" /> <association property="address" resultMap="AddressResult" /> </resultMap> <!-- 根据id查询--> <select id="findStudentWithAddress" parameterType="int" resultMap="StudentWithAddressResult"> SELECT STUD_ID, NAME, EMAIL, A.ADDR_ID, STREET, CITY, STATE, ZIP, COUNTRY FROM STUDENTS S LEFT OUTER JOIN ADDRESSES A ON S.ADDR_ID=A.ADDR_ID WHERE STUD_ID=#{Id} </select> .....
第二种:使用圆点记法为内嵌的对象的属性赋值
在resultMap中,Student 的address属性使用了圆点记法
被赋上了address对应列的值
...... <resultMap type="Student" id="StudentWithAddressResult"> <id property="studId" column="stud_id" /> <result property="name" column="name" /> <result property="email" column="email" /> <result property="phone" column="phone" /> <result property="address.addrId" column="addr_id" /> <result property="address.street" column="street" /> <result property="address.city" column="city" /> <result property="address.state" column="state" /> <result property="address.zip" column="zip" /> <result property="address.country" column="country" /> </resultMap> <select id="selectStudentWithAddress" parameterType="int" resultMap="StudentWithAddressResult"> SELECT STUD_ID, NAME, EMAIL, A.ADDR_ID, STREET, CITY, STATE, ZIP, COUNTRY FROM STUDENTS S LEFT OUTER JOIN ADDRESSES A ON S.ADDR_ID=A.ADDR_ID WHERE STUD_ID=#{Id} </select> ......
第三种:使用association定义内联的 resultMap
需注意:association javaType=”Address”
<resultMap type="Student" id="StudentWithAddressResult"> <id property="studId" column="stud_id" /> <result property="name" column="name" /> <result property="email" column="email" /> <association property="address" javaType="Address"> <id property="addrId" column="addr_id" /> <result property="street" column="street" /> <result property="city" column="city" /> <result property="state" column="state" /> <result property="zip" column="zip" /> <result property="country" column="country" /> </association> </resultMap> <select id="findStudentWithAddressByStuId" parameterType="int" resultMap="StudentResult"> SELECT STUD_ID, NAME, EMAIL, A.ADDR_ID, STREET, CITY, STATE, ZIP, COUNTRY FROM STUDENTS S LEFT OUTER JOIN ADDRESSES A ON S.ADDR_ID=A.addr_id WHERE STUD_ID=#{Id} </select>
第四种:嵌套select查询实现一对一关系映射
在此方式中,association 元素的 select属性被设置成了id为 findAddressById 的语句,然后查询结果赋值
...... <resultMap type="Address" id="AddressResult"> <id property="addrId" column="addr_id" /> <result property="street" column="street" /> <result property="city" column="city" /> <result property="state" column="state" /> <result property="zip" column="zip" /> <result property="country" column="country" /> </resultMap> <select id="findAddressById" parameterType="int" resultMap="AddressResult"> SELECT * FROM ADDRESSES WHERE ADDR_ID=#{id} </select> <resultMap type="Student" id="StudentWithAddressResult"> <id property="studId" column="stud_id" /> <result property="name" column="name" /> <result property="email" column="email" /> <association property="address" column="addr_id" select="findAddressById" /> </resultMap> <!-- 查询 --> <select id="findStudentWithAddress" parameterType="int" resultMap="StudentWithAddressResult"> SELECT * FROM STUDENTS WHERE STUD_ID=#{Id} </select> ......
参考:Java Persistence with MyBatis 3
相关文章推荐
- Android-onInterceptTouchEvent()和onTouchEvent()几张图说明一切
- IOS彩票第二天设置界面(2)
- Android自定义title bar(标题栏)
- 使用微信一次上传几张图片
- COCOS2D-X 3.0坐标系详解
- web移动端知识和资源链接
- android Activity launch mode
- HDU 1452 Happy 2004(积性函数 逆元)
- 黑马程序员——Objective-C Foundation框架中的NSMutableString对象
- Android Afinal使用与总结
- appium python api
- Android Activity生命周期详解
- android:ellipsize="end" 失效或者 相关的Bug
- HTML5移动端数据图表组件调研
- 黑马程序员——Objective-C Foundation框架中的NSString对象
- Android笔记(二十二) Android中的GridView
- [置顶] 微信开放平台的第三方平台、全网发布流程、组件API、返回普通文本消息
- Android studio dabao
- iOS开发 - 证书失效
- Unity 2D游戏开发教程之为游戏场景添加多个地面