MyBatis从入门到废弃四:一对多关系查询

前言

     
上篇学习了一对一关乎查询,这篇大家学习一对多涉及查询。一对多关系查询关键点则依旧是布置resultMap,在resultMap中配置collection属性,别忽略了ofType属性。

 

搭建开发条件

     创立表author、表blog,构建一对多的询问现象。

图片 1

     成立author、blog model。author类中首如果添加属性List<Blog>
blogs属性。

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.xml

图片 2图片 3

 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>

View Code

 

创建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();
        }
    }

附单元测试截图

图片 4

 

网站地图xml地图