Mybaits one-to-one query method

Source: Internet
Author: User
Tags one table

Mybaits single-One Query method: Table data and table structure
CREATE TABLETeacher (t_idINT PRIMARY KEYAuto_increment,t_nameVARCHAR( -));CREATE TABLEClass (c_idINT PRIMARY KEYAuto_increment,c_nameVARCHAR( -), teacher_idINT);ALTER TABLEClassADD CONSTRAINTfk_teacher_idFOREIGN KEY(teacher_id)REFERENCESteacher (t_id);INSERT  intoTeacher (T_name)VALUES('LS1');INSERT  intoTeacher (T_name)VALUES('LS2');INSERT  intoClass (C_name, teacher_id)VALUES('bj_a',1);INSERT  intoClass (C_name, teacher_id)VALUES('Bj_b',2);

II: Basic Preparation

As on the SQL can be seen, here is a one-to-one table, a teacher, corresponding to a class, then, if you want to use SQL to identify the class information (including teacher information), then we can check:

Method One:

SELECT *  from WHERE c.teacher_id= and c.c_id=1

Method Two:

Select *  from where c_id=1Select*fromwhere t_id= 1

Class Entity classes:

 Public class Classes {    privateint  ID;     Private String name;     Private Teacher Teacher;}.. The Get Set method is omitted, and the default constructor method

Teacher Entity classes:

 Public class Teacher {    privateint  ID;     Private String name;} The Get Set method is omitted, and the empty construction method

Three: Mybaits one-on-one query 3.1. Use the first method of SQL for one-to-one queries

In the entity class can also be seen, classes class has a teacher attribute, so this is a one-to-one writing, so in the configuration file, also to the expression of this relationship, the following gives the configuration file:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd "><Mappernamespace= "Zxj.domain.ClassMapper">    <!--query class information by ID (with teacher's information) -    <!--Query Method Sql:select * from class C,teacher T WHERE c.teacher_id=t.t_id and C.c_id=1 -    <SelectID= "GetClass"ParameterType= "int"Resultmap= "Getclassmap">SELECT * from class C,teacher T WHERE c.teacher_id=t.t_id and C.c_id=#{id}</Select>    <!--in the returned result set, specify each column, which object corresponds to, use the association tag to declare the other object -    <Resultmaptype= "Zxj.domain.Classes"ID= "Getclassmap">        <ID Property= "id"column= "c_id"/>        <resultcolumn= "C_name" Property= "Name"/>        <Association Property= "Teacher"Javatype= "Zxj.domain.Teacher">            <ID Property= "id"column= "t_id"/>            <result Property= "Name"column= "T_name"/>        </Association>    </Resultmap></Mapper>

In the above configuration file, using the <resultMap> tag, you can map each field in the database to the fields in the Java entity class, and then Mybaits will know their relationship and then encapsulate them as objects.

Let's do a test here:

/*** Test one-to-one query <br> * Query Sql:select * FROM class C,teacher T WHERE c.teacher_id=t.t_id and C.c_id=1 */@Test Public voidTestonetoone () {InputStream in= Testmybaits.class. getClassLoader (). getResourceAsStream ("Conf.xml"); Sqlsessionfactory Factory=NewSqlsessionfactorybuilder (). build (in); Sqlsession Session=factory.opensession (); String Statement= "Zxj.domain.ClassMapper.getClass"; Classes C= Session.selectone (statement, 1);        System.out.println (c);    Session.close (); }

The method used is the same as the usual use.

3.2. Use the second type of SQL for one-to-one queries

How the configuration file is written:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd "><Mappernamespace= "Zxj.domain.ClassMapper">        <!--another way to query one -    <!--method Two: SELECT * from class where c_id=1 a select * from teacher where t_id=1 (here t_id is the ID of the previous SQL)  -    <SelectID= "GetClass2"ParameterType= "int"Resultmap= "GETCLASSMAP2">SELECT * from class where C_id=#{id}</Select>        <SelectID= "Getteacher"ParameterType= "int"Resulttype= "Zxj.domain.Teacher">        <!--The alias is needed here because the fields in the database are different from the fields in the entity class, and we need to make it one to encapsulate the object -Select t_id id,t_name name from teacher where T_id=#{id}</Select>    <Resultmaptype= "Zxj.domain.Classes"ID= "GETCLASSMAP2">        <IDcolumn= "c_id" Property= "id"/>        <resultcolumn= "C_name" Property= "Name"/>        <!--The implementation principle is to query two SQL, the condition of the second query is based on the results of the first query, the following column property is to pass the parameters of the past query -        <Association Property= "Teacher"column= "teacher_id"Select= "Getteacher" >        </Association>    </Resultmap></Mapper>

It is to emit two SQL, the first SQL check class, the second SQL teacher, but the condition of the second SQL is based on the results of the first SQL detected as a condition.

Test:

    /*** Way Two <br> * Test one-to-one query <br> * Query Sql:select * FROM class C,teacher T WHERE c.teacher_id=t.t_id an D c.c_id=1*/@Test Public voidTestOneToOne2 () {InputStream in= Testmybaits.class. getClassLoader (). getResourceAsStream ("Conf.xml"); Sqlsessionfactory Factory=NewSqlsessionfactorybuilder (). build (in); Sqlsession Session=factory.opensession (); String Statement= "Zxj.domain.ClassMapper.getClass2"; Classes C= Session.selectone (statement, 1);        System.out.println (c);    Session.close (); }

This completes a one-to-one query.

Mybaits one-to-one query method

Related Article

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.