浅入浅出MyBatis(09):获取一篇文章及其作者的信息

January 14th 2015  | Tags: MyBatis, orm

2015-01-14

该文章是《专题:浅入浅出MyBatis》系列文章的一篇。点击进入该专题目录

工具与环境:Intellij IDEA 14,JDK 1.7,MyBatis 3.2,MySQL 5.5。

这次是在浅入浅出MyBatis(08):获取一个用户的所有blog的基础上讲一下如何获取一篇文章机器作者的信息。

修改letian.mybatis.bean下的Blog.java

将其内容修改为:

package letian.mybatis.bean;

public class Blog {
    private int id;
    private int ownerId;
    private String title;
    private String content;

    private User user;  // 新添加

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public int getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(int ownerId) {
        this.ownerId = ownerId;
    }

    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 getContent() {
        return content;
    }

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

    @Override
    public String toString() {
        return "Blog{" +
                "id=" + id +
                ", ownerId=" + ownerId +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", user=" + user +
                '}' + '\n';
    }
}

可以看到,新增加了User类型的user属性,并为user属性添加了setter和getter方法。toString()方法也略做修改。

在letian.mybatis.dao下新建BlogMapper.java

其内容如下:

package letian.mybatis.dao;


import letian.mybatis.bean.Blog;

public interface BlogMapper {

    Blog findById(int id);
}

在letian.mybatis.mapper下新建BlogMapper.xml

其内容如下:

<?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="letian.mybatis.dao.BlogMapper">

    <resultMap id="blogResult" type="letian.mybatis.bean.Blog">
        <result property="id" column="blog_id"/>
        <result property="ownerId" column="user_id"/>
        <result property="title" column="blog_title"/>
        <result property="content" column="blog_content"/>

        <association property="user" javaType="letian.mybatis.bean.User">
            <id property="id" column="user_id"/> <!-- 用result也可以 -->
            <result property="name" column="user_name"/>
            <result property="email" column="user_email"/>
            <result property="password" column="user_password"/>
        </association>

    </resultMap>

    <select id="findById" parameterType="Integer" resultMap="blogResult" resultType="letian.mybatis.bean.Blog">
        SELECT
        blog.id AS blog_id,
        blog.title AS blog_title,
        blog.content AS blog_content,
        user.id AS user_id,
        user.name AS user_name,
        user.email AS user_email,
        user.password AS user_password
        FROM blog, user WHERE blog.id = #{id} and user.id=blog.owner_id;
    </select>

</mapper>

这个的配置原理和浅入浅出MyBatis(08):获取一个用户的所有blog是类似的。由于blog对user是一对一的关系,所以没有使用<collection></collection>,而是使用了<association></association>

修改Main.java

将Main.java内容修改为:

import java.io.IOException;

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 letian.mybatis.bean.User;
import letian.mybatis.bean.Blog;
import letian.mybatis.dao.UserMapper;
import letian.mybatis.dao.BlogMapper;


public class Main {

    public static void main(String[] args) throws IOException {

        SqlSessionFactory sessionFactory;
        sessionFactory = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsReader("mybatis-config.xml"));

        SqlSession sqlSession = sessionFactory.openSession();

//        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//        User user = userMapper.findById(1);
//        System.out.println(user);

        BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
        Blog blog = blogMapper.findById(1);
        System.out.println(blog);

        sqlSession.close();
    }
}

执行

执行Main.java,结果如下:

Blog{id=1, ownerId=1, title='标题1', content='文本1', user=User{id=1, name='letian', email='letian@111.com', password='123'
, blogs=null}}

(完)