经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
mybatis 一对一 一对多 配置详解
来源:cnblogs  作者:zhangheliang  时间:2019/2/19 9:36:42  对本文有异议

javabean:

  1. package com.me.model;import java.io.Serializable;import java.util.Date;import java.util.List;public class User implements Serializable {  
  2.       /** * 
  3.      */private static final long serialVersionUID = 1L;private int id;  private String username;  private Date birthday;  private String sex;  private String address; //一對一 放入對象private Morder morder;//一對多 放入對象集合private List<Home> homeList;    
  4.     
  5.       
  6.    public List<Home> getHomeList() {return homeList;
  7.     }public void setHomeList(List<Home> homeList) {this.homeList = homeList;
  8.     }public Morder getMorder() {return morder;
  9.     }public void setMorder(Morder morder) {this.morder = morder;
  10.     }public static long getSerialversionuid() {return serialVersionUID;
  11.     }public int getId() {  return id;  
  12.     }  public void setId(int id) {  this.id = id;  
  13.     }  public String getUsername() {  return username;  
  14.     }  public void setUsername(String username) {  this.username = username;  
  15.     }  public Date getBirthday() {  return birthday;  
  16.     }  public void setBirthday(Date birthday) {  this.birthday = birthday;  
  17.     }  public String getSex() {  return sex;  
  18.     }  public void setSex(String sex) {  this.sex = sex;  
  19.     }  public String getAddress() {  return address;  
  20.     }  public void setAddress(String address) {  this.address = address;  
  21.     }
  22.     @Overridepublic String toString() {return "User [id=" + id + ", username=" + username + ", birthday="
  23.                 + birthday + ", sex=" + sex + ", address=" + address+ ", morder=" + morder + ", homeList=" + homeList + "]";
  24.     }
  25.     
  26.     
  27.       
  28. }
  1. package com.me.model;public class Morder {    private int orderId;private String orderName;private String orderMessage;public int getOrderId() {return orderId;
  2.     }public void setOrderId(int orderId) {this.orderId = orderId;
  3.     }public String getOrderName() {return orderName;
  4.     }public void setOrderName(String orderName) {this.orderName = orderName;
  5.     }public String getOrderMessage() {return orderMessage;
  6.     }public void setOrderMessage(String orderMessage) {this.orderMessage = orderMessage;
  7.     }
  8.     
  9.     
  10.  
  11. }
  1. package com.me.model;
  2.  
  3. public class Home {
  4. private int homeId;
  5. private String homeName;
  6. public int getHomeId() {
  7. return homeId;
  8. }
  9. public void setHomeId(int homeId) {
  10. this.homeId = homeId;
  11. }
  12. public String getHomeName() {
  13. return homeName;
  14. }
  15. public void setHomeName(String homeName) {
  16. this.homeName = homeName;
  17. }
  18.  
  19. }

  mapper.xml 代码

  1. <!-- collection :collection属性的值有三个分别是list、array、map三种, 分别对应的参数类型为:List、数组、map集合,我在上面传的参数为数组,所以值为array 
  2.         item : 表示在迭代过程中每一个元素的别名 index :表示在迭代过程中每次迭代到的位置(下标) open :前缀 close :后缀 separator 
  3.         :分隔符,表示迭代时每个元素之间以什么分隔 --><delete id="deleteSome">delete from user where id in<foreach collection="list" item="id" index="index" open="("close=")" separator=",">#{id}</foreach></delete><!-- 关联查询 -->    <!-- 關聯查詢 1對1 --><select id="selectGL" resultMap="userRsultMap">select * from user u,morder m
  4.         WHERE u.oid=m.order_id</select><resultMap type="com.me.model.User" id="userRsultMap"><id property="id" column="id" /><result column="username" property="username" /><result column="birthday" property="birthday" /><result column="sex" property="sex" /><result column="address" property="address" /><association property="morder" javaType="com.me.model.Morder"><id column="order_id" property="orderId" /><result column="order_name" property="orderName" /><result column="order_message" property="orderMessage" /></association></resultMap><!-- 關聯查詢 1對多 --><select id="selectGL2" resultMap="userRsultMap2">select * from user u,home h where u.hid=h.home_id;    </select><resultMap type="com.me.model.User" id="userRsultMap2"><id property="id" column="id" /><result column="username" property="username" /><result column="birthday" property="birthday" /><result column="sex" property="sex" /><result column="address" property="address" /><collection property="homeList" ofType="com.me.model.Home"><id property="homeId" column="home_id" /><result property="homeName" column="home_name" /></collection></resultMap>

图文解释:

测试:

  1. //關聯查詢 1 to 多    @Testpublic void selectGL2(){try {
  2.             inputStream = Resources.getResourceAsStream(resource);// 创建会话工厂,传入MyBatis的配置文件信息SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
  3.                     .build(inputStream);// 通过工厂得到SqlSessionsqlSession = sqlSessionFactory.openSession();
  4.             List<User> list = sqlSession.selectList("test.selectGL2");for (User u : list) {
  5.                 System.err.println(u.getHomeList().get(0).getHomeName());
  6.             }
  7.  
  8.         } catch (IOException e) {
  9.             e.printStackTrace();
  10.         } finally {// 释放资源            sqlSession.close();
  11.         }
  12.     }

结果:

22:47:17.005 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.140 [main] DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
22:47:17.215 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Opening JDBC Connection
22:47:17.420 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Created connection 518522822.
22:47:17.420 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1ee807c6]
22:47:17.421 [main] DEBUG test.selectGL2 - ==>  Preparing: select * from user u,home h where u.hid=h.home_id;
22:47:17.444 [main] DEBUG test.selectGL2 - ==> Parameters:
22:47:17.461 [main] DEBUG test.selectGL2 - <==      Total: 4
sasadasd
22:47:17.462 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1ee807c6]
22:47:17.462 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1ee807c6]
22:47:17.463 [main] DEBUG o.a.i.d.pooled.PooledDataSource - Returned connection 518522822 to pool.

 

更多可以参考:https://www.cnblogs.com/xdp-gacl/p/4264440.html

原文链接:http://www.cnblogs.com/zhangheliang/p/10398431.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号