One of the mybatis relationship mappings is one-to-many and one-to-one.

Source: Internet
Author: User

I. Introduction:

This example describes how to use a customer and an order: a customer can have multiple orders, and an order only corresponds to one customer.


Ii. Example:

1. code structure:



2. Table creation statement:

CREATE DATABASE test;  USE test;CREATE TABLE person(  personId VARCHAR(36) PRIMARY KEY,  personName VARCHAR(64),  personAddress VARCHAR(128),  personTel VARCHAR(11));CREATE TABLE orders(  orderId VARCHAR(36) PRIMARY KEY,  orderNumber VARCHAR(20),  orderPrice INT,  pid VARCHAR(36));INSERT INTO person VALUES('001', 'Jack', 'Wuhan', '1234567');INSERT INTO orders VALUES('O_00001', '00001', 100, '001');INSERT INTO orders VALUES('O_00002', '00002', 200, '001');SELECT p.*, o.*FROM person p   JOIN orders o ON (p.personId=o.pid)WHERE p.personId = '001'

3. Customer entity:

/*** Customer entity */public class person {private string ID; private string name; private string address; private string Tel; private list <order> orders; @ overridepublic string tostring () {return "{ID:" + ID + ", name:" + name + ", address:" + address + ", tel: "+ Tel + "}";}}

4. Order entity:

/*** Order entity */public class order {private string ID; private string number; private int price; private person; @ overridepublic string tostring () {return "{ID: "+ ID +", number: "+ number +", price: "+ Price + "}";}}


5. One-to-Multiple object configuration: person. xml

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype mapperpublic "-// mybatis.org//dtd mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namespace =" com. zdp. domain. person "> <resultmap type =" person "id =" personbean "> <ID column =" personid "property =" ID "/> <result column =" personname "property =" name "/> <result column =" personaddress "property =" Address "/> <result column =" persontel "property =" tel "/> <! -- One-to-many relationship --> <! -- Property: the value of the Set property. oftype: indicates the type of elements in the collection --> <collection property = "orders" oftype = "order"> <ID column = "orderid" property = "ID"/> <result Column = "ordernumber" property = "Number"/> <result column = "orderprice" property = "price"/> </collection> </resultmap> <! -- Query person by ID and query orders by Association --> <select id = "selectpersonbyid" parametertype = "string" resultmap = "personbean"> select P. *, O. * From person P, orders O where p. personid = O. PID and P. personid =#{ ID} </SELECT> </mapper>

6. Multiple-to-one instance Configuration:

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype mapperpublic "-// mybatis.org//dtd mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namespace =" com. zdp. domain. order "> <resultmap type =" order "id =" orderbean "> <ID column =" orderid "property =" ID "/> <result column =" ordernumber "property =" number "/> <result column =" orderprice "property =" price "/> <! -- Multi-to-one relationship --> <! -- Property: indicates the property value. javatype: indicates the property type --> <association property = "person" javatype = "person"> <ID column = "personid" property = "ID"/> <result column =" personname "property =" name "/> <result column =" personaddress "property =" Address "/> <result column =" persontel "property =" tel "/> </Association> </resultmap> <! -- Query order by ID and associate the query by person --> <select id = "selectorderbyid" parametertype = "string" resultmap = "orderbean"> select P. *, O. * From person P, orders O where p. personid = O. PID and O. orderid =#{ ID} </SELECT> </mapper>

7. Total configuration: sqlmapconfig. xml

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype configurationpublic "-// mybatis.org//dtd config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <configuration> <typealiases> <typealias type =" com. zdp. domain. person "alias =" person "/> <typealias type =" com. zdp. domain. order "alias =" order "/> </typealiases> <environments default =" development "> <environment id =" development "> <transactionmanager type =" JDBC "/> <datasource type = "pooled"> <property n Ame = "driver" value = "com. mySQL. JDBC. driver "/> <property name =" url "value =" JDBC: mysql: // localhost/test "/> <property name =" username "value =" root "/> <property name =" password "value =" root "/> </datasource> </environment> </environments> <mappers> <! -- Ing file location --> <mapper resource = "com/zdp/domain/person. XML "/> <mapper resource =" com/zdp/domain/order. XML "/> </mappers> </configuration>

8. Test file:

/*** Test one-to-many and multiple-to-one */public class mybatistest {private sqlsessionfactory SSF; @ beforepublic void initsf () throws exception {string resource = "sqlmapconfig. XML "; inputstream = resources. getresourceasstream (Resource); SSF = new sqlsessionfactorybuilder (). build (inputstream) ;}@ test // One-to-multiple association query public void selectpersonbyid () throws exception {sqlsession session = SSF. opensession (); person = session. selectone ("com. zdp. domain. person. selectpersonbyid "," 001 "); system. out. println (person. getorders () ;}@ test // public void selectorderbyid () throws exception {sqlsession session = SSF. opensession (); Order order = session. selectone ("com. zdp. domain. order. selectorderbyid "," o_00001 "); system. out. println (Order. getperson (). getname ());}}


One of the mybatis relationship mappings is one-to-many and one-to-one.

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.