Mybatis学习笔记(四)-----一对多关系配置

来源:互联网 发布:热敏打印机软件 编辑:程序博客网 时间:2024/06/03 19:32

增加表结构的XML

此处为一个command的name,关联多个command的content信息。
即1个(name)对多个(content)。
command为主表,command_content为子表。
Command.xml的配置相对复杂,而CommandContent.xml相对简单。

(1)首先为两个表增加BEAN类,负责接收数据。

/**对应于command表*/public class Command {  private int id;  private String name;  private String description;  private List<CommandContent> contentList;  public List<CommandContent> getContentList() {    return contentList;  }  public void setContentList(List<CommandContent> contentList) {    this.contentList = contentList;  }  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 String getDescription() {    return description;  }  public void setDescription(String description) {    this.description = description;  }}

需要特别说明的是,因为是一对多的关系,所以在一的那个类中加入多的集合,此处采用List<CommandContent>。因此,需要在xml配置文件中特殊处理。

/**对应command_content表*/public class CommandContent {  private int id;  private String content;  private String content_id;  public int getId() {    return id;  }  public void setId(int id) {    this.id = id;  }  public String getContent() {    return content;  }  public void setContent(String content) {    this.content = content;  }  public String getContent_id() {    return content_id;  }  public void setContent_id(String content_id) {    this.content_id = content_id;  }}

(2)配置两个表结构的XML文件。

<!-- Command.xml --><mapper namespace="Command">    <resultMap type="com.shen.bean.Command" id="Command">        <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.CommandContent" />    </resultMap>    <select id="queryCommandList" parameterType="com.shen.bean.Command"        resultMap="Command">        select a.ID C_ID,a.Name,a.DESCRIPTION,b.ID,b.COMMAND_ID,b.CONTENT from        command a left join        command_content b on a.ID=b.COMMAND_ID        <where>            <if test="command!=null and !&quot;&quot;.equals(name.trim())">and a.NAME = #{name}</if>            <if test="description!=null and !&quot;&quot;.equals(description.trim())">and a.DESCRIPTION like '%' #{description} '%'</if>        </where>    </select></mapper>

需要理解的两个点:
1)在JDBC中,获取数据时的rs.getString("XXX")中的XXX是查询列名,该“列名”不是数据库中的字段名,而是SELECT XXX from Command中的XXX(即查询语句的列名)。

若查询列名有SELECT XXX YYY from Command这样的形式,即查询列名有别名,则JDBC获取数据时应该为rs.getString(“YYY”)。

Mybatis中也是这样设计的,在<resultMap>中的column就是getString()中的查询列名(有别名则为别名)。
2)若为多表查询时,数据表有别名,查询列名为a.XXX,则在上述规则中,不带”a.”,仍为”XXX”。
3)若为多表查询时,多个表中有相同的字段名,会导致Mybatis无法正确与BEAN对应起来,需要在查询语句中的SELECT语句中设置成不同的别名,而<resultMap>中设置成对应的别名。

如:
上述的a.IDb.ID都是ID,此处设置成a.ID C_IDresultMap中的column设置成C_ID,而b.ID未设置仍为ID,故在下面的”CommandContent.xml”中的column仍为ID

<!-- CommandContent.xml --><mapper namespace="CommandContent">    <resultMap type="com.shen.bean.CommandContent" id="CommandContent">        <id column="ID" jdbcType="INTEGER" property="id" />        <result column="CONTENT" jdbcType="VARCHAR" property="content" />        <result column="CONTENT_ID" jdbcType="VARCHAR" property="content_id" />    </resultMap></mapper>

(3)修改Service

修改Service调用queryCommandDao;

public class QueryService {  public List<Message> queryMessageList(String command, String description) {    MessageDao messageDao = new MessageDao();    return messageDao.queryMessageList(command, description);  }  public String queryByCommand(String command) {    CommandDao commandDao = new CommandDao();    List<Command> commandList;    if ("帮助".equals(command)) {      commandList = commandDao.queryCommandList(null, null);      StringBuilder result = new StringBuilder();      for (int i = 0; i < commandList.size(); i++) {        if (i != 0) {          result.append("<br/>");        }        result.append("回复[" + commandList.get(i).getName() + "]可以查看\"" + commandList.get(i).getContentList().get(i).getContent()+"\"");      }      return result.toString();    }    commandList = commandDao.queryCommandList(command, null);    if (commandList.size() > 0) {      List<CommandContent> contentList=commandList.get(0).getContentList();      int i = new Random().nextInt(contentList.size());      return contentList.get(i).getContent();    }    return Iconst.NO_MATCHING_CONTENT;  }}
阅读全文
0 0
原创粉丝点击