Mybatis表关联一对多

应用场景:首先根据用户 ID 读取一个用户信息,然后再读取这个用户所发布贴子(post)。

创建sql表

 

 

创建表对应的 JavaBean 对象 

package com.tanlei.newer.pojo;

import java.io.Serializable;
import java.util.List;

/**
 * @author:Mr.Tan
 * @Create:2018-11-01-11-19
 **/
public class Person implements Serializable {
    private int id;
    private String username;
    private String mobile;
    private List<Post> posts;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public List<Post> getPosts() {
        return posts;
    }

    public void setPosts(List<Post> posts) {
        this.posts = posts;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", mobile='" + mobile + '\'' +
                ", posts=" + posts +
                '}';
    }
}

package com.tanlei.newer.pojo;

/**
 * @author:Mr.Tan
 * @Create:2018-11-01-11-20
 **/
public class Post {
    private int id;
    private Person person;
    private String title;
    private String content;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Post{" +
                "id=" + id +
                ", person=" + person +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}

 

配置文件

主配置文件:src/config/Configure.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC   "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <typeAlias alias="User" type="com.tanlei.newer.pojo.User"></typeAlias>
        <typeAlias alias="Person" type="com.tanlei.newer.pojo.Person"></typeAlias>
        <typeAlias alias="Post" type="com.tanlei.newer.pojo.Post"></typeAlias>
    </typeAliases>
    
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8"></property>
                <property name="username" value="root"></property>
                <property name="password" value="password"></property>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="com/tanlei/newer/pojo/Person.xml"></mapper>
    </mappers>
</configuration>

  

 

bean映射文件

 

<?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">
<mapper namespace="com.tanlei.newer.pojo.IPerson">
    <resultMap type="Person" id="resultPersonMap" >
        <result property="id" column="id"></result>
        <result property="username" column="username"></result>
        <result property="mobile" column="mobile"></result>
        <collection property="posts" ofType="com.tanlei.newer.pojo.Post" column="persionid">
            <id property="id" column="post_id"  javaType="int" jdbcType="INTEGER" ></id>
            <result property="title" column="title"  javaType="String" jdbcType="VARCHAR"></result>
            <result property="content" column="content" javaType="String" jdbcType="VARCHAR"></result>
        </collection>

    </resultMap>

    <select id="getPerson" resultMap="resultPersonMap" parameterType="int">
        SELECT pp.*,p.*
		FROM person pp, post p
		WHERE pp.id=p.personid AND id=#{id}
    </select>
</mapper>

 

接口类

package com.tanlei.newer.pojo;

public interface IPerson {
    //查找一个用户信息
    public Person getPerson(int id);
}

 

main类

 

package com.tanlei.newer.test;

import com.tanlei.newer.pojo.Person;
import com.tanlei.newer.pojo.Post;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

/**
 * @author:Mr.Tan
 * @Create:2018-11-05-13-52
 **/
public class PersonMain {
    public static Reader reader;
    public static  SqlSessionFactory sqlSessionFactory;

    static {
        try {
            reader= Resources.getResourceAsReader("config/Configure.xml");
            sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public  static  SqlSessionFactory getSession(){
        return  sqlSessionFactory;
    }

    public static void main(String[] args) {
        SqlSession sqlSession=sqlSessionFactory.openSession();
        int personid=1;
        Person person=sqlSession.selectOne("com.tanlei.newer.pojo.IPerson.getPerson",1);
        System.out.println("Username:"+person.getUsername());
        List<Post> posts=person.getPosts();
        for(Post post:posts){
            System.out.println("Title:" + post.getTitle());
            System.out.println("Content:" + post.getContent());
        }
        sqlSession.close();
    }
}

  

运行结果

 

 

 

 

 

 

 

  

posted @ 2018-11-05 14:21  言西早石头侠  阅读(196)  评论(0编辑  收藏  举报