Mybitis (Ibitis) series of essays Five: multiple tables (one-to-many association queries)

Source: Internet
Author: User

Mybitis (Ibitis) series of essays: mybitis instance of getting StartedMybitis (Ibitis) series two essays: type Alias (typealiases) and table-Object Mapping (ORM)Mybitis (Ibitis) series of essay Three: simple implementation of CRUDMybitis (Ibitis) series of essays four: Multi-table (many-to-one query operation)Mybitis (Ibitis) series of essays Five: multiple tables (one-to-many association queries)Mybitis (Ibitis) series of essays Six: Mybitis and spring Integration

This blog post describes a one-to-many-table association query in multiple tables
or in accordance with the previous step, do some preparatory work; Create two tables: One is the user, one is the corresponding mobile phone, one user can have a mobile phone.
This is the User T_user table


This is the mobile phone t_mobile watch.

In the Java entity object pair, a one-to-many can be implemented according to list and set, both in the Mybitis with the collection tag to use, and later will do a detailed configuration introduction

Create a JavaBean object for a table

Mobile Bean

[Java]View PlainCopy
  1. Public class Mobile {
  2. private int id;
  3. private String Telnumber;
  4. public int getId () {
  5. return ID;
  6. }
  7. public void setId (int id) {
  8. this.id = ID;
  9. }
  10. Public String Gettelnumber () {
  11. return telnumber;
  12. }
  13. public void Settelnumber (String telnumber) {
  14. this.telnumber = Telnumber;
  15. }
  16. }


User Bean

[Java]View PlainCopy
  1. Package Com.jefry;
  2. Import java.util.List;
  3. Public class User {
  4. private int id;
  5. private String UserName;
  6. private String password;
  7. private list<mobile> mobiles; //This can also be a set set
  8. Public list<mobile> Getmobiles () {
  9. return mobiles;
  10. }
  11. public void Setmobiles (list<mobile> mobiles) {
  12. this.mobiles = mobiles;
  13. }
  14. Public String GetUserName () {
  15. return userName;
  16. }
  17. public void Setusername (String userName) {
  18. this.username = userName;
  19. }
  20. Public String GetPassword () {
  21. return password;
  22. }
  23. public void SetPassword (String password) {
  24. This.password = password;
  25. }
  26. public int getId () {
  27. return ID;
  28. }
  29. public void setId (int id) {
  30. this.id = ID;
  31. }
  32. }

Overwrite the mapping file on the basis of the previous article:

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8" ?>
  2. <! DOCTYPE Mapper
  3. Public "-//mybatis.org//dtd Mapper 3.0//en"
  4. "Http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="user">
  6. <resultmap id="Userresultmap" type="User">
  7. <ID property="id" column="id" javatype="int" jdbctype="INTEGER" />
  8.         <result < span class= "attribute" >property= "userName"  column=< Span class= "Attribute-value" > "name"  javatype= "string " jdbctype=" VARCHAR "/>   
  9.         < Result property= "password"   column= "pass"  javatype=< Span class= "Attribute-value" > "string"  jdbctype= " VARCHAR "/>  
  10.         <collection  property= "mobiles"   Column= "userid"  oftype= "Mobile" >    
  11.             < id property= "id"  column= "id"  javatype=" int " jdbctype=" INTEGER " />    
  12. <result property="Telnumber" column="Telnumber" javatype="string" jdbctype= "VARCHAR"/>
  13. </Collection>
  14. </resultmap>
  15. <!--Multi-table query operation--
  16. <Select id= "selectuser" parametertype="int" resultmap="Userresultmap" >
  17. <!--assign aliases to the primary key ID of the mobile and the user's primary key ID, and avoid injecting into the corresponding object name conflict because two table field names are the same--
  18. Select M.id m_id,m.telnumber,u.id u_id,u.name,u.pass from T_mobile m,t_user u where m.userid = u.id and u.id = #{id}
  19. </Select>
  20. </mapper>

Finally, pass the test OK

[Java]View PlainCopy
  1. Public class Test {
  2. static String resource = "Mybatis-config.xml";
  3. public static void Main (string[] args) throws IOException {
  4. InputStream InputStream = resources.getresourceasstream (Resource);
  5. Sqlsessionfactory sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (InputStream);
  6. sqlsession session = Sqlsessionfactory.opensession ();
  7. try {
  8. User user = Session.selectone ("User.selectuser", 1);
  9. list<mobile> mobiles = User.getmobiles ();
  10. For (Mobile mobile:mobiles) {
  11. System.out.println ("User:" + user.getusername () + ", tel:" + mobile.gettelnumber ());
  12. }
  13. } finally {
  14. Session.close ();
  15. }
  16. }
  17. }


SOURCE Download: http://download.csdn.net/detail/jefry_xdz/5229451

http://blog.csdn.net/jefry_xdz/article/details/8767358

Http://www.cnblogs.com/yinkh/p/5564033.html

Mybitis (Ibitis) series of essays Five: multiple tables (one-to-many association queries)

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.