mybatis中多对多一对多实例,maven+springMvc+mybatis


    原文链接:http://blog.csdn.net/laiwanwanyihao/article/details/72943421,是本博主写的,

附源码,免费下载

 

前言

好久没用过mybatis了,现在复习一下,顺带着整理一下东西,

本人maven用的不好,架构参照来源http://www.cnblogs.com/GarfieldEr007/p/5746263.html,

本片文章例子为简单的小例子,参考的文章也是介绍mybatis,不过我没看,我就下了maven架构就开始搞自个的例子了,因为原博主的文章写得太长了,懒得看了

maven架构

maven构架springMvc+mybatis这里我就不一一展示了,想必各位都知道,不清楚的看源码就行了,

数据库

数据库很简单 三个表 A   AB        B     其中AB为中间表,A  与 B 为多对多关系,,,今天就整理下A与B多对多和A与AB一对多的两种关联映射处理,(注意数据库中表字段不要重名,比如A表主键与B表主键不要重名,不然查出来结果会不全,陷这个坑好久才明白过来)

 

多对多

 


 
  1. <!-- 多对多 -->  
  2.     <resultMap id="BaseResultMap2" type="cn.test.model.A">  
  3.         <id column="aid" property="aid" jdbcType="INTEGER" />  
  4.         <result column="a_name" property="aname" jdbcType="VARCHAR" />  
  5.         <collection property="bLists" ofType="cn.test.model.B">  
  6.             <id property="bid" column="bid" />  
  7.             <result property="bname" column="b_name" />  
  8.         </collection>  
  9.     </resultMap>  
  10.   
  11.     <select id="selectAByIdUnionB" resultMap="BaseResultMap2"  
  12.         parameterType="java.lang.Integer">  
  13.         select  
  14.         *  
  15.         from a inner join a_b on a.aid = a_b.a_id inner  
  16.         join b on a_b.b_id = b.bid where a.aid=#{id jdbcType="INTEGER"}  
  17.     </select>  

 

测试

 


 
  1. //多对多  
  2.     @org.junit.Test  
  3.     public void seectAByIdUnionB() {  
  4.       
  5.         for(A a:aService.selectAByIdUnionB(1)){  
  6.             System.out.print(a.getAname()+">>>>>>>");  
  7.             for(B b :a.getbLists() ){   
  8.                 System.out.print(b.getBname());  
  9.             }  
  10.             System.out.println("\n");  
  11.         }   
  12.           
  13.     }  
  14.       

结果

 

 

 

一对多

 


 
  1. <!-- 一对多 -->  
  2.   
  3.     <resultMap type="cn.test.model.A" id="OneToManay">  
  4. <id column="aid" property="aid" jdbcType="INTEGER" />  
  5.         <result column="a_name" property="aname" jdbcType="VARCHAR" />  
  6.         <collection property="abLists" ofType="cn.test.model.AB">  
  7.             <id property="id" column="id" />  
  8.             <result property="a" column="a_id" />  
  9.             <result property="b" column="b_id" />  
  10.         </collection>  
  11.     </resultMap>  
  12.   
  13.     <select id="selectAtoAB" resultMap="OneToManay" parameterType="INTEGER">  
  14.     select * from a inner join a_b on a.aid=a_b.a_id where a.aid=#{id jdbcType="INTEGER"}  
  15.     </select>  


测试

 
  1. //一对多  
  2.     @org.junit.Test  
  3.     public void onetomany(){  
  4.         for(A a :aService.selectAtoAB(1)){  
  5.             System.out.print(a.getAname()+">>>>>>>");  
  6.             for (AB ab : a.getAbLists()) {  
  7.                 System.out.print(ab.getId());  
  8.                 //System.out.print(ab.getA());  
  9.                 //System.out.print(ab.getB());  
  10.             }  
  11.             System.out.println("\n");  
  12.         }  
  13.           
  14.     }  
  15.       

结果

 

 

 

 


好了 希望给各位需要的同志们有所帮助,

源码下载地址:http://download.csdn.NET/detail/laiwanwanyihao/9865160

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM