Ibatis Configure multiple table associations (pair of one or one pairs, many-to-many)

Source: Internet
Author: User

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.
...
.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.