From getting started to giving up MyBatis 4: One-to-multiple association query, mybatis getting started

Source: Internet
Author: User

From getting started to giving up MyBatis 4: One-to-multiple association query, mybatis getting started
Preface

In the previous article, we learned one-to-one association query. In this article, we learned one-to-many Association queries. In one-to-multiple join queries, the key point is still configuring resultMap. Configure the collection attribute in resultMap. Do not ignore the ofType attribute.

 

Build a Development Environment

Create tables author and table blog to create one-to-many query scenarios.

Create author and blog model. In the author class, the attribute List <Blog> blogs is added.

public class Author {    private int id;    private String name;    private List<Blog> blogs;    public List<Blog> getBlogs() {        return blogs;    }    public void setBlogs(List<Blog> blogs) {        this.blogs = blogs;    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

 

public class Blog {    private int id;    private String title;    private String category;    private int author_id;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getCategory() {        return category;    }    public void setCategory(String category) {        this.category = category;    }    public int getAuthor_id() {        return author_id;    }    public void setAuthor_id(int author_id) {        this.author_id = author_id;    }}

Create alias in mybatis. xml and reference resource mapper. xml

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! DOCTYPE configuration 3 PUBLIC "-// mybatis.org//DTD Config 3.0 // EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <! -- MyBatis configuration for SqlServer --> 8 <typeAliases> 9 <typeAlias alias = "User" type = "com. autohome. model. user "/> 10 <typeAlias alias =" Teacher "type =" com. autohome. model. teacher "/> 11 <typeAlias alias =" Student "type =" com. autohome. model. student "/> 12 <typeAlias alias =" Author "type =" com. autohome. model. author "/> 13 <typeAlias alias =" Blog "type =" com. autohome. model. blog "/> 14 </typeAliases> 15 <environments default =" development "> 16 <environment id =" development "> 17 <transactionManager type =" JDBC "/> 18 <dataSource type = "POOLED"> 19 <property name = "driver" value = "com. microsoft. sqlserver. jdbc. SQLServerDriver "/> 20 <property name =" url "value =" jdbc: sqlserver :/// localhost: 1433; databaseName = test "/> 21 <property name =" username "value =" sa "/> 22 <property name =" password "value =" 0 "/> 23 </dataSource> 24 </environment> 25 </environments> 26 27 28 <mappers> 29 <mapper resource = "mapper/Author. xml "/> 30 <mapper resource =" mapper/User. xml "/> 31 <mapper resource =" mapper/Student. xml "/> 32 </mappers> 33 </configuration>View Code

 

Create Mapper. xml (Author. xml)
 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="com.autohome.mapper.Author"> 6     <resultMap id="authorResultMap" type="Author"> 7         <id property="id" column="id"/> 8         <result property="name" column="name"/> 9         <collection property="blogs" ofType="Blog">10             <id column="bid" property="id"/>11             <result column="title" property="title"/>12             <result column="category" property="category"/>13         </collection>14     </resultMap>15 16     <select id="getAuthorBlogsById" parameterType="int" resultMap="authorResultMap">17         SELECT a.id,name,b.id bid,title,category FROM t_author a18         LEFT JOIN t_blog b on a.id=b.author_id19         WHERE a.id=#{id}20 21     </select>22 </mapper>

 

Unit Test
@ Test public void getAuthorBlog () {SqlSession sqlSession = null; try {sqlSession = sqlSessionFactory. openSession (); Author author = sqlSession. selectOne ("com. autohome. mapper. author. getAuthorBlogsById ", 1); System. out. println ("author information id:" + author. getId () + ", name:" + author. getName (); System. out. println ("author's Blog:"); for (blog Blog: author. getBlogs () {System. out. println ("id:" + blog. getId () + ", title:" + blog. getTitle () + ", category:" + blog. getCategory () ;}} catch (Exception e) {e. printStackTrace ();} finally {sqlSession. close ();}}

Attached unit test

 

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.