ibatis和mybatis级联查询的操作

ibatis中的级联查询:一对多

 1         <typeAlias alias="one" type="com.test.domain.One" />
 2       <typeAlias alias="two" type="com.text.domain.Two" />
 3       
 4         <resultMap class="one" id="oneResult">
 5             <result property="ID" column="ID" />
 6             <result property="oneName" column="oneName" />
 7             <result property="twoList" column="{传递的参数1=ID, 传递的参数2=oneName}" select="namspaceName.selectTwo"/>
 8         </resultMap>
 9         <resultMap class="two" id="twoResult">
10             <result property="ID" column="ID" />
11             <result property="oneID" column="oneID" />
12             <result property="twoName" column="twoName" />
13         </resultMap>        
14              
15         <select id="selectOne"  resultClass="oneResult">
16         SELECT * FROM  table_one 
17     </select>
18         <select id="selectTwo"  resultClass="two">
19         SELECT * FROM  table_two where oneID= #传递的参数1# and oneName=#传递的参数2#
20     </select>
21         

 

mybatis中的级联查询:一对一和一对多

 1 <resultMap type="com.test.pojo.one" id="BaseResultMap">
 2         <id column="ID" property="id" jdbcType="INTEGER" />
 3         <result column="name" property="name" jdbcType="VARCHAR" />
 4 
 5   <!--一对一的级联查询-->
 6         <association property="two" column="ID"                       
 7             select="namespaceName.selectTwoByOneid" />
 8 
 9   <!--一对多的级联查询-->
10         <collection property="threeList" column="ID"                       
11             select="namespaceName.selectThreeByOneid" />
12     </resultMap>
13 
14     <resultMap id="twoResult" type="com.test.pojo.two" >
15         <id column="ID" property="id" jdbcType="INTEGER" />
16         <result column="oneID" property="oneID" jdbcType="INTEGER" />
17   <result column="twoName" property="twoName" jdbcType="VARCHAR" />
18     </resultMap>
19     
20     <resultMap id="threeResult" type="com.europen.pojo.NaSwiftSender" >
21         <id column="ID" property="id" jdbcType="INTEGER" />
22         <result column="oneID" property="oneID" jdbcType="INTEGER" />
23         <result column="threeName" property="threeName" jdbcType="VARCHAR" />
24     </resultMap>
25 
26     <select id="getOneList"  resultMap="BaseResultMap">
27         SELECT * FROM table_one WITH(NOLOCK)
28     </select>
29     <select id="selectTwoByOneid"  resultMap="twoResult">
30         SELECT * from table_two WITH(NOLOCK) where oneID = #{ID}
31     </select>
32     <select id="selectThreeByOneid"  resultMap="threeResult">
33         SELECT * from table_three WITH(NOLOCK) where oneID = #{ID}
34     </select>
35 
36 
37     

 

posted on 2018-03-22 17:36  魏先生的Weisir  阅读(684)  评论(0)    收藏  举报

导航