Ibatis Association of multiple tables.
Ibatis table associations, which are independent of database statements, are associated with the results of several statements in a Java program.
This form of correlation, while large amounts of data is very luxurious behavior, but it looks very clean and easy to use.
Here, for example, lock and key, a lock can be used with multiple keys. CREATE TABLE Lock (
ID int,
Lockname varchar
);
CREATE TABLE Key (
ID int,
lockid int,
KeyName varchar
);
Define mapping class package zzcv.dao.domain;
public class Lock
{
private int id;
Private String Lockname;
Private Object keys; Here you can store a list of query results.
public void GetId () {
return.
...
Omitted
}
Package zzcv.dao.domain;
public class Key
{
private int id;
private int lockid;
Private String KeyName;
Private Object lock; Here you can store key-matching lock.
...
Omitted
}
Table associations are implemented through the Resultmap of the configuration file, and queries that do not need to be associated are still available 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 >
<!--a one-to-one association with the ID of the lock saved in the key table, Ibatis populates the lock property with the results of Getlockbyid (lockid)-->
< Resultmap ID = "Lockresult" class = "Lock" >
< Result property = "id" column = "id"/>
< Result property = "lockname" column = "Lockname"/>
< Result property = "keys" column = "id" select = "Getkeysbylockid"/>
</Resultmap >
<!--a One-to-many association implemented through the lock ID, Ibatis uses the Getkeysbylockid (ID) to populate the Keys property-->
<!--Many-to-many can be implemented through nesting, and there is no listing of-->
< 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 >
Now you can use ... in your code.
..
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");
Takes a lock object.
Lock lock = (lock) locks.get (0);
Take list<key> from the lock object.
List keys = Lock.getkeys ();
The example ends with the result, and the rest of the association is the object lock, and the rest is almost done.
...
.