iBatis的多表关联。
ibatis的表关联,和数据库语句无关,是在程序中,把若干语句的结果关联到一起。
这种关联形式,虽然在大数据量时是很奢侈的行为,但是看起来很干净,用起来也很方便。
这里用表lock和key为例,一个lock可以配多个key。
CREATE
TABLE
lock(
id int ,
lockName varchar
);
CREATE TABLE key (
id int ,
lockId int ,
keyName varchar
);
id int ,
lockName varchar
);
CREATE TABLE key (
id int ,
lockId int ,
keyName varchar
);
定义映射类
package
zzcv.dao.domain;
public class Lock
{
private int id;
private String lockName;
private Object keys; // 这里可以存放一个查询结果List。
public void getId(){
return ..
...
// 省略
}
package zzcv.dao.domain;
public class Key
{
private int id;
private int lockId;
private String keyName;
private Object lock; // 这里可以存放key匹配的lock。
...
// 省略
}
public class Lock
{
private int id;
private String lockName;
private Object keys; // 这里可以存放一个查询结果List。
public void getId(){
return ..
...
// 省略
}
package zzcv.dao.domain;
public class Key
{
private int id;
private int lockId;
private String keyName;
private Object lock; // 这里可以存放key匹配的lock。
...
// 省略
}
表关联通过配置文件的resultMap实现,不需要关联的查询仍可用resultClass
<
sqlMap
namespace
="test"
>
< typeAlias alias ="Key" type ="zzcv.dao.domain.Key" />
< typeAlias alias ="Lock" type ="zzcv.dao.domain.Lock" />
< resultMap id ="KeyResult" class ="Key" >
< result property ="id" column ="id" />
< result property ="keyName" column ="keyName" />
< result property ="lock" column ="lockId" select ="getLockById" />
</ resultMap >
<!-- 通过key表中保存的lock的id实现的一对一关联,ibatis会使用getLockById(lockId)的结果填充lock属性 -->
< resultMap id ="LockResult" class ="Lock" >
< result property ="id" column ="id" />
< result property ="lockName" column ="lockName" />
< result property ="keys" column ="id" select ="getKeysByLockId" />
</ resultMap >
<!-- 通过lock的id实现的一对多关联,ibatis会使用getKeysByLockId(id)得到的List填充keys属性 -->
<!-- 多对多可以通过嵌套实现,这里就不列出了 -->
< select id ="selectAllkeys" resultMap ="KeyResult" >
<![CDATA[
select id,lockId,keyName from key
]]>
</ select >
< select id ="getLockById" parameterClass ="int" resultClass ="Lock" >
<![CDATA[
select id,lockName from lock where id = #value#
]]>
</ select >
< select id ="selectAllLocks" resultMap ="LockResult" >
<![CDATA[
select id,lockName from lock
]]>
</ select >
< select id ="getKeyByLockId" parameterClass ="int" resultClass ="key" >
<![CDATA[
select id,lockId,keyName from lock where lockId = #value#
]]>
</ select >
</ sqlMap >
< typeAlias alias ="Key" type ="zzcv.dao.domain.Key" />
< typeAlias alias ="Lock" type ="zzcv.dao.domain.Lock" />
< resultMap id ="KeyResult" class ="Key" >
< result property ="id" column ="id" />
< result property ="keyName" column ="keyName" />
< result property ="lock" column ="lockId" select ="getLockById" />
</ resultMap >
<!-- 通过key表中保存的lock的id实现的一对一关联,ibatis会使用getLockById(lockId)的结果填充lock属性 -->
< resultMap id ="LockResult" class ="Lock" >
< result property ="id" column ="id" />
< result property ="lockName" column ="lockName" />
< result property ="keys" column ="id" select ="getKeysByLockId" />
</ resultMap >
<!-- 通过lock的id实现的一对多关联,ibatis会使用getKeysByLockId(id)得到的List填充keys属性 -->
<!-- 多对多可以通过嵌套实现,这里就不列出了 -->
< select id ="selectAllkeys" resultMap ="KeyResult" >
<![CDATA[
select id,lockId,keyName from key
]]>
</ select >
< select id ="getLockById" parameterClass ="int" resultClass ="Lock" >
<![CDATA[
select id,lockName from lock where id = #value#
]]>
</ select >
< select id ="selectAllLocks" resultMap ="LockResult" >
<![CDATA[
select id,lockName from lock
]]>
</ select >
< select id ="getKeyByLockId" parameterClass ="int" resultClass ="key" >
<![CDATA[
select id,lockId,keyName from lock where lockId = #value#
]]>
</ select >
</ sqlMap >
现在可以代码中使用了
...
..
try {
Reader reader = Resources.getResourceAsReader( " mxd/dao/data/SqlMapConfig.xml " );
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close(); }
catch (IOException e){
throw new RuntimeException( " Something bad happened while building the SqlMapClient instance. " + e, e);}
List locks = sqlMapper.queryForList( " selectAllLocks " );
// 取一个Lock对象。
Lock lock = (Lock)locks.get( 0 );
// 从Lock对象取List<Key>。
List keys = lock.getKeys();
// 示例结束,取到结果了,剩余的一个关联是取对象Lock,其余操作差不多,就不写出了。
...
.
..
try {
Reader reader = Resources.getResourceAsReader( " mxd/dao/data/SqlMapConfig.xml " );
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close(); }
catch (IOException e){
throw new RuntimeException( " Something bad happened while building the SqlMapClient instance. " + e, e);}
List locks = sqlMapper.queryForList( " selectAllLocks " );
// 取一个Lock对象。
Lock lock = (Lock)locks.get( 0 );
// 从Lock对象取List<Key>。
List keys = lock.getKeys();
// 示例结束,取到结果了,剩余的一个关联是取对象Lock,其余操作差不多,就不写出了。
...
.