MyBatis:一對多關聯查詢

MyBatis從入門到放棄四:一對多關聯查詢

前言

      上篇學習了一對一關聯查詢,這篇咱們學習一對多關聯查詢。一對多關聯查詢關鍵點則依然是配置resultMap,在resultMap中配置collection屬性,別忽略了ofType屬性。html

 

搭建開發環境

     建立表author、表blog,構建一對多的查詢場景。java

     建立author、blog model。author類中主要是添加屬性List<Blog> blogs屬性。sql

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;
    }
}

  在mybatis.xml建立alias、引用resource mapper.xmlmybatis

複製代碼
 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針對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>
複製代碼

 

建立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 a 18  LEFT JOIN t_blog b on a.id=b.author_id 19  WHERE a.id=#{id} 20 21 </select> 22 </mapper>
複製代碼

 

單元測試

 @Test
    public void getAuthorBlog(){
        SqlSession sqlSession=null;
        try{
            sqlSession=sqlSessionFactory.openSession();

            Author author = sqlSession.selectOne("com.autohome.mapper.Author.getAuthorBlogsById",1);
            System.out.println("做者信息 id:"+author.getId()+",name:"+author.getName());
            System.out.println("做者博客:");
            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();
        }
    }
相關文章
相關標籤/搜索