Ibatis多表查询

http://diwuningning.blog.163.com/blog/static/3426519520103155623642/

一:描述下:

使用2个表,book,user表。一对多关系,一本书有多个作者。

表语句:CREATE TABLE `book` (
`oid` int(10) NOT NULL ,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`oid`)
) ;

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL,
`name` varchar(50) DEFAULT NULL,
`book_oid` int(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_user_1` (`book_oid`),
CONSTRAINT `FK_user_1` FOREIGN KEY (`book_oid`) REFERENCES `book` (`oid`)
) ;

二:对应的Pojo类

package com.pojo;

public class User {
private Integer id;
private String name;
private Integer book_oid;

........一下是get set 注意在ibatis定义的pojo类中要有个空的构造函数哦~~,

package com.pojo;

imp<wbr>ort java.util.List;</wbr>

public class Book {
private Integer oid;
private String name;
private List users;

.......get set 注意:必须定义个list ,ibatis在实现多表关联查询时。

配置文件:对应pojo的 book.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="test">
<typeAlias alias="User" type="com.pojo.User"/>
<typeAlias alias="Book" type="com.pojo.Book"/>

<resultMap id="BookResult" class="Book">
<result property="oid" column="oid"/>
<result property="name" column="name"/>
<result property="users" column="oid" select="getUsersByBookId"/>
</resultMap>
<!-- 通过book的oid实现的一对多关联,ibatis会使用getKeysByLockId(id)得到的List填充users属性 -->


<select id="selectAllBooks" resultMap="BookResult">
<![CDATA[
select oid,b.name from book b
]]>
</select>

<select id="getUsersByBookId" parameterClass="int" resultClass="User">
<![CDATA[
select id,book_oid,u.name from user u where book_oid = #value#
]]>
</select>
</sqlMap>

在定义个配置文件调用上面的配置文件:如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
errorTracingEnabled="true"
maxRequests="132"
maxSessions="110"
maxTransactions="15"
useStatementNamespaces="false" />
<transactionManager type="JDBC">
<dataSource type="DBCP">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/test" />
<property name="JDBC.Username" value="root" />
<property name="JDBC.Password" value="root" />

</dataSource>
</transactionManager>
<sqlMap resource="com/crfss/book.xml" />
</sqlMapConfig>

三:测试类:



package com.test;

imp<wbr>ort java.io.IOException;<br> imp<wbr>ort java.io.Reader;<br> imp<wbr>ort java.util.Iterator;<br> imp<wbr>ort java.util.List;</wbr></wbr></wbr></wbr>

imp<wbr>ort com.crfss.MainDb;<br> imp<wbr>ort com.crfss.Un;<br> imp<wbr>ort com.ibatis.common.resources.Resources;<br> imp<wbr>ort com.ibatis.sqlmap.client.SqlMapClient;<br> imp<wbr>ort com.ibatis.sqlmap.client.SqlMapClientBuilder;<br> imp<wbr>ort com.pojo.Book;<br> imp<wbr>ort com.pojo.User;</wbr></wbr></wbr></wbr></wbr></wbr></wbr>


public class TestDB {

public static void main(String[] args) {
String resource = "sql-map-config.xml";
try {
Reader reader = Resources.getResourceAsReader(resource);
SqlMapClient mapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
List<Book> books=mapClient.queryForList("selectAllBooks");
Book book=(Book)books.get(0);
//System.out.println(book.getName());
List users=(List) book.getUsers();

for (Iterator iterator = users.iterator(); iterator.hasNext();) {
User user = (User) iterator.next();
System.out.println(user.getBook_oid());
System.out.println(user.getName());
System.out.println(user.getId());
}
} catch (IOException e) {
e.printStackTrace();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值