我踩过的坑是你们前进的高速公路

Mybatis的一对多关系的配置

今天学习的是Mybatis下的一对多的关系配置。


 

首先放出的是表的ER图,方便大家理解。

简单的说明一下,这两张表有一对多的关系,也就是说,一条指令对应了多条的指令内容,那么在这样的关系下,mybatis如何去配置。

 

 根据ER图,我们开始在MySQL编写表的结构

command表
CREATE TABLE `command` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(255) DEFAULT NULL,
  `DESCRIPTION` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
command_content表
CREATE TABLE `command_content` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `CONTENT` varchar(255) NOT NULL,
  `COMMAND_ID` int(11) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

 

java代码部分,我们先编写这两个表的实体类

/**
 * 与指令表对应的实体类
 */
public class Command {
    /**
     * 主键
     */
    private String id;
    /**
     * 指令名称
     */
    private String name;
    /**
     * 描述
     */
    private String description;
    /**
     * 一条指令对应的自动回复内容列表
     */
    private List<CommandContent> contentList;
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public List<CommandContent> getContentList() {
        return contentList;
    }
    public void setContentList(List<CommandContent> contentList) {
        this.contentList = contentList;
    }
}

 

/**
 * 与指令内容表对应的实体类
 */
public class CommandContent {
    /**
     * 主键
     */
    private String id;
    
    /**
     * 自动回复的内容
     */
    private String content;
    
    /**
     * 关联的指令表主键
     */
    private String commandId;

    public String getId() {
        return id;
    }

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

    public String getContent() {
        return content;
    }

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

    public String getCommandId() {
        return commandId;
    }

    public void setCommandId(String commandId) {
        this.commandId = commandId;
    }
}

 

到mapper,我们需要编写两个映射文件

<?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="Command">

  <resultMap type="com.imooc.bean.Command" id="CommandResult">
    <id column="C_ID" jdbcType="INTEGER" property="id"/>
    <result column="NAME" jdbcType="VARCHAR" property="name"/>
    <result column="DESCRIPTION" jdbcType="VARCHAR" property="description"/>
    <collection property="contentList" resultMap="CommandContent.Content"></collection>
  </resultMap>

  <select id="queryCommandList" parameterType="com.imooc.bean.Message" resultMap="CommandResult">
    select a.ID C_ID,a.NAME,a.DESCRIPTION,b.ID,b.CONTENT,b.COMMAND_ID from COMMAND a 
    left join COMMAND_CONTENT b on a.ID = b.COMMAND_ID
    <where>
        <if test="name!=null and !&quot;&quot;.equals(name.trim())">
            and NAME = #{name}
        </if>
        <if test="description!=null and !&quot;&quot;.equals(description.trim())">
            and DESCRIPTION like '%' #{description} '%'
        </if>
    </where>
  </select>

  
</mapper>
<?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="CommandContent">

  <resultMap type="com.imooc.bean.CommandContent" id="Content">
    <id column="ID" jdbcType="INTEGER" property="id"/>
    <result column="CONTENT" jdbcType="VARCHAR" property="content"/>
    <result column="COMMAND_ID" jdbcType="INTEGER" property="commandId"/>
  </resultMap>


  
</mapper>

通过以上的配置,我们就可以实现两张表的一对多关系的配置了。

 

posted @ 2018-09-10 21:06  大名鼎鼎的狮子  阅读(852)  评论(0编辑  收藏  举报
百度