举例:
一个文章有一个用户,当查询文章时,显示文章信息和文章作者信息,这里不考虑一个文章多个作者的问题。
表结构:
文章表:
DROP TABLE IF EXISTS `article`; CREATE TABLE `article` ( `id` int(11) NOT NULL AUTO_INCREMENT, `article_name` varchar(50) NOT NULL COMMENT ‘文章名‘, `article_content` varchar(1000) NOT NULL COMMENT ‘文章内容‘, `author_id` int(11) NOT NULL COMMENT ‘作者id‘, `create_time` datetime NOT NULL COMMENT ‘创建时间‘, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
作者表:
DROP TABLE IF EXISTS `author`; CREATE TABLE `author` ( `id` int(11) NOT NULL AUTO_INCREMENT, `author_name` varchar(50) NOT NULL COMMENT ‘作者名‘, `author_age` int(11) NOT NULL COMMENT ‘作者年龄‘, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
文章实体:
package com.heli.core.user.model; import java.io.Serializable; import java.util.Date; public class Article implements Serializable { private Integer id; private String articleName; private String articleContent; private Integer authorId; private Date createTime; private Author author; public Author getAuthor() { return author; } public void setAuthor(Author author) { this.author = author; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getArticleName() { return articleName; } public void setArticleName(String articleName) { this.articleName = articleName; } public String getArticleContent() { return articleContent; } public void setArticleContent(String articleContent) { this.articleContent = articleContent; } public Integer getAuthorId() { return authorId; } public void setAuthorId(Integer authorId) { this.authorId = authorId; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } }
作者实体:
package com.heli.core.user.model; import java.io.Serializable; public class Author implements Serializable { private Integer id; private String authorName; private Integer authorAge; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAuthorName() { return authorName; } public void setAuthorName(String authorName) { this.authorName = authorName; } public Integer getAuthorAge() { return authorAge; } public void setAuthorAge(Integer authorAge) { this.authorAge = authorAge; } }
文章的映射文件ArticleMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" > <mapper namespace="com.heli.core.user.mapper.ArticleMapper" > <resultMap id="BaseResultMap" type="com.heli.core.user.model.Article" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="article_name" property="articleName" jdbcType="VARCHAR" /> <result column="article_content" property="articleContent" jdbcType="VARCHAR" /> <result column="author_id" property="authorId" jdbcType="INTEGER" /> <result column="create_time" property="createTime" jdbcType="TIMESTAMP" /> <association property="author" column="author_id" javaType="com.heli.core.user.model.Author" select="getAuthor" > </resultMap> <resultMap id="authorResultMap" type="com.heli.core.user.model.Author" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="author_name" property="authorName" jdbcType="VARCHAR" /> <result column="author_age" property="authorAge" jdbcType="INTEGER" /> </resultMap> <sql id="Base_Column_List" > id, article_name, article_content, author_id, create_time </sql> <select id="getAuthor" resultMap="authorResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from author where id = #{authorId,jdbcType=INTEGER} </select> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from article where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from article where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.heli.core.user.model.Article" useGeneratedKeys="true" keyProperty="id" > insert into article (article_name, article_content, author_id, create_time) values (#{articleName,jdbcType=VARCHAR}, #{articleContent,jdbcType=VARCHAR}, #{authorId,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}) </insert> <insert id="insertSelective" parameterType="com.heli.core.user.model.Article" useGeneratedKeys="true" keyProperty="id" > insert into article <trim prefix="(" suffix=")" suffixOverrides="," > article_name, article_content, author_id, create_time, </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > #{articleName,jdbcType=VARCHAR}, #{articleContent,jdbcType=VARCHAR}, #{authorId,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.heli.core.user.model.Article" > update article <set > <if test="articleName != null" > article_name = #{articleName,jdbcType=VARCHAR}, </if> <if test="articleContent != null" > article_content = #{articleContent,jdbcType=VARCHAR}, </if> <if test="authorId != null" > author_id = #{authorId,jdbcType=INTEGER}, </if> <if test="createTime != null" > create_time = #{createTime,jdbcType=TIMESTAMP}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.heli.core.user.model.Article" > update article set article_name = #{articleName,jdbcType=VARCHAR}, article_content = #{articleContent,jdbcType=VARCHAR}, author_id = #{authorId,jdbcType=INTEGER}, create_time = #{createTime,jdbcType=TIMESTAMP} where id = #{id,jdbcType=INTEGER} </update> </mapper>
这样查询文章时,就可以获取文章的作者信息了。
原文:http://my.oschina.net/ydsakyclguozi/blog/510533