ibatis2.3中,怎样实现多张表联合查询
1个回答
展开全部
我这里用的是mybaits的,两表查询在xml配置
?
1
2
3
4
5
6
<resultMap type="com.szy.bean.User" id="user_book" >
<id property="id" column="id"/>
<result property="userName" column="username"/>
<result property="userAddress" column="useraddress"/>
<association property="book" column="book_id" resultMap="book"></association>
</resultMap>
?
1
在上面用association 关联了book的resultMap
?
1
2
3
4
<!-- book -->
<resultMap type="com.szy.bean.Book" id="book">
<result property="bookName" column="book_name"/>
</resultMap>
<!-- 多表查询 -->
<select id="selectUserAndBooks" resultMap="user_book" >
select u.id,u.username ,u.useraddress,b.book_name from user u right outer join book b on u.id= b.book_id
</select>
?
1
实体bean为
public class User {
private int id;
private String userName;
private String userAge;
private String userAddress;
private Book book;
?
1
我不知道ibatis怎么弄的,没实践过,但mybatis这样写是可以联表查的
?
1
2
3
4
5
6
<resultMap type="com.szy.bean.User" id="user_book" >
<id property="id" column="id"/>
<result property="userName" column="username"/>
<result property="userAddress" column="useraddress"/>
<association property="book" column="book_id" resultMap="book"></association>
</resultMap>
?
1
在上面用association 关联了book的resultMap
?
1
2
3
4
<!-- book -->
<resultMap type="com.szy.bean.Book" id="book">
<result property="bookName" column="book_name"/>
</resultMap>
<!-- 多表查询 -->
<select id="selectUserAndBooks" resultMap="user_book" >
select u.id,u.username ,u.useraddress,b.book_name from user u right outer join book b on u.id= b.book_id
</select>
?
1
实体bean为
public class User {
private int id;
private String userName;
private String userAge;
private String userAddress;
private Book book;
?
1
我不知道ibatis怎么弄的,没实践过,但mybatis这样写是可以联表查的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询