這篇博文介紹的是多表中的一對多表關聯查詢
建立兩張表:一張是用戶,一張是用戶所對應的移動手機,一戶用戶能夠有部移動手機。
這是用戶t_user表 html
![](http://static.javashuo.com/static/loading.gif)
這是移動電話t_mobile表
![](http://static.javashuo.com/static/loading.gif)
在Java實體對象對中,一對多能夠根據List和Set來實現,二者在mybitis中都是經過collection標籤來配合使用,稍後會作詳細配置介紹 java
建立表對應的JavaBean對象 sql
Mobile Bean session
- public class Mobile {
- private int id;
- private String telnumber;
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getTelnumber() {
- return telnumber;
- }
-
- public void setTelnumber(String telnumber) {
- this.telnumber = telnumber;
- }
-
- }
User Bean mybatis
- package com.jefry;
-
- import java.util.List;
-
- public class User {
- private int id;
- private String userName;
- private String password;
- private List<Mobile> mobiles; //這裏也能夠是Set集合
-
- public List<Mobile> getMobiles() {
- return mobiles;
- }
-
- public void setMobiles(List<Mobile> mobiles) {
- this.mobiles = mobiles;
- }
-
- public String getUserName() {
- return userName;
- }
-
- public void setUserName(String userName) {
- this.userName = userName;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- }
在上一篇的基礎上改寫映射文件: app
- <?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="user">
- <resultMap id="userResultMap" type="User">
- <id property="id" column="id" javaType="int" jdbcType="INTEGER" />
- <result property="userName" column="name" javaType="string" jdbcType="VARCHAR"/>
- <result property="password" column="pass" javaType="string" jdbcType="VARCHAR"/>
- <collection property="mobiles" column="userid" ofType="Mobile">
- <id property="id" column="id" javaType="int" jdbcType="INTEGER"/>
- <result property="telnumber" column="telnumber" javaType="string" jdbcType="VARCHAR"/>
- </collection>
- </resultMap>
-
- <!--多表查詢操做-->
- <select id="selectUser" parameterType="int" resultMap="userResultMap" >
- <!--分別爲mobile的主鍵id與user的主鍵id賦值別名,避免由於兩個表字段名稱相同而注入到對應對象名稱衝突-->
- select m.id m_id,m.telnumber,u.id u_id,u.name,u.pass from t_mobile m,t_user u where m.userid = u.id and u.id = #{id}
- </select>
- </mapper>
最後,經過測試OK 測試
- public class Test {
- static String resource = "mybatis-config.xml";
-
- public static void main(String[] args) throws IOException {
- InputStream inputStream = Resources.getResourceAsStream(resource);
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- SqlSession session = sqlSessionFactory.openSession();
- try {
- User user = session.selectOne("user.selectUser", 1);
- List<Mobile> mobiles = user.getMobiles();
- for(Mobile mobile : mobiles) {
- System.out.println("user:" + user.getUserName() + ",tel:" + mobile.getTelnumber());
- }
-
-
- } finally {
- session.close();
- }
- }
-
- }