Java Framework mybaits pair of one or one-to-many

Source: Internet
Author: User

One: Elaboration

We in the daily operation, many times will encounter multi-table joint query, due to the lack of reference, there will be a pair of one or one-to-many cases. For example: Account information and order form, from the order table angle and account information is one-to-one situation (an order can only be a user's case), from the user's point of view, there will be a one-to-many situation (a user will have multiple orders).

Two, one to one:

Need to be clear:

Now we create 2 tables:

1 CREATE TABLE username (2 ID INT PRIMARY KEY auto_increment,3NAME VARCHAR ( -),4Sex VARCHAR ( -)5         );6 7 CREATE TABLE Orders (8 ID INT PRIMARY KEY auto_increment,9Num VARCHAR ( -),Ten user_id INT One);

Create an order table and a user table. Insert some values.

Reference is: Order

Federated Query SQL:

1 SELECT o. ' id ' as OID, O. ' num ' as num, O. ' user_id ' as uid,u.* from orders as O, username as u WHERE o.user_id=u.i D

Results:

The first method:

First, the mapping class:

Orders class:

1 Package Jd.com.ou;2 3  Public classOrders {4     PrivateString num;5     PrivateInteger user_id;6 7      Public voidsetnum (String num) {8          This. num =num;9     }Ten  One      Public voidsetuser_id (Integer user_id) { A          This. user_id =user_id; -     } -  the      PublicInteger getuser_id () { -         returnuser_id; -     } -  +      PublicString Getnum () { -         returnnum; +     } A}

User class:

1 Package Jd.com.ou;2 3  Public classUser {4     PrivateString name;5     PrivateString sex;6 7      Public voidsetName (String name) {8          This. Name =name;9     }Ten  One      Public voidsetsex (String sex) { A          This. Sex =sex; -     } -  the      PublicString GetName () { -         returnname; -     } -  +      PublicString Getsex () { -         returnsex; +     } A}

Note: Instead of creating these 2 tables for a federated query, these 2 classes are the Projo classes for the user table and the Ordes table.

Then we define the Projo class that returns the data:

1 Package Jd.com.ou;2 3  Public classCustomuo extends Orders {4     PrivateString name;5     PrivateString sex;6 7      Public voidsetsex (String sex) {8          This. Sex =sex;9     }Ten  One      Public voidsetName (String name) { A          This. Name =name; -     } -  the      PublicString Getsex () { -         returnsex; -     } -  +      PublicString GetName () { -         returnname; +     } A  at @Override -      PublicString toString () { -         return  This. name+ This. sex+ This. Getnum (); -     } -}

Mapper configuration file:

1<?xml version="1.0"encoding="UTF-8"?>2<!DOCTYPE Mapper3Public"-//mybatis.org//dtd Mapper 3.0//en"4         "HTTP://MYBATIS.ORG/DTD/MYBATIS-3-MAPPER.DTD">5<mappernamespace="Jd.com.ou.oumapper">6<SelectId="Findorderanduser"Resulttype="Jd.com.ou.customuo"Parametertype="Jd.com.ou.customuo">7SELECT o. ' id ' as OID, O. ' num ' as num, O. ' user_id ' as uid,u.* from orders as O, username as u WHERE o. ' user_id ' =u.id;8</Select>9</mapper>

Note in the SQL statement, the WHERE keyword is preceded by the alias that defines the column, where the expression is not used in the alias of the column.

Mapper Interface:

1 Package Jd.com.ou; 2 3 import java.util.List; 4 5  Public Interface Oumapper {6     List<customuo> Findorderanduser (); 7 }

Test class:

1 Package Jd.com.ou;2 3 import org.apache.ibatis.io.Resources;4 import org.apache.ibatis.session.SqlSession;5 import org.apache.ibatis.session.SqlSessionFactory;6 import Org.apache.ibatis.session.SqlSessionFactoryBuilder;7 import org.junit.jupiter.api.Test;8 9 import java.io.IOException;Ten import Java.io.InputStream; One  A import java.util.List; -  -  Public classTestdemo { the  - @Test -      Public  voidTestdemo () throws IOException { -String reource="Sqlmapconfig.xml"; +InputStream inp=Resources.getresourceasstream (reource); -Sqlsessionfactory sqlsessionfactory=NewSqlsessionfactorybuilder (). Build (INP); +Sqlsession sqlsession=sqlsessionfactory.opensession (); AOumapper Oum=sqlsession.getmapper (Oumapper.class); atList<customuo> list=Oum.findorderanduser (); -System. out. println (list); -     } -}

Attention:

Here our definition receives the return value of the Customuo class, and we do not set the user's field to the Customuo property. Why??

Because we did not pass in the parameter, that is, we cannot set the user class to the Customuo property, so if calling Customuo's user property would be the default value of the object: null.

So what do we do if we want to use the user field?

The second method:

Java Framework mybaits pair of one or one-to-many

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.