码迷,mamicode.com
首页 > 其他好文 > 详细

ibatis主子表查询

时间:2015-06-12 19:17:11      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

1、sql

主表

CREATE TABLE `account_group` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL COMMENT 公众号分组,
  `count` int(11) DEFAULT NULL COMMENT 分组下有多少公众号,
  `user_id` int(11) DEFAULT NULL COMMENT 用户id,
  `created_at` timestamp NULL DEFAULT NULL COMMENT 创建时间,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

字表

CREATE TABLE `wx_account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `accountname` varchar(255) NOT NULL COMMENT accountname,
  `account_group_id` int(11) DEFAULT NULL COMMENT 公众号分组id,
  `createTime` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

字表通过account_group_id与主表id关联
2、实体类

主表

/**                                                
 * Domain Object. Generated by RAD automatically.
 *                                               
 */                                              
public class AccountGroup extends BaseModel implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -2651997950883256833L;
    private Integer user_id;
    private String name;
    private Integer count;
    private Integer id;
    private List<WxAccount> accounts;


    public List<WxAccount> getAccounts() {
        return accounts;
    }

    public void setAccounts(List<WxAccount> accounts) {
        this.accounts = accounts;
    }

    public Integer getUser_id() {
        return user_id;
    }


    public Integer getId() {
        return id;
    }

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

}

子表:略

3、sql-xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="AccountGroup">
    <resultMap id="NewResultMap" class="com.xxxxx.accountGroup.model.AccountGroup">
        <result column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="count" property="count" jdbcType="INTEGER" />
        <result column="user_id" property="user_id" jdbcType="INTEGER" />
        <result column="created_at" property="created_at" jdbcType="VARCHAR" />
        <result column="id" property="accounts" select="getAccountByGroupId"/>
    </resultMap>
    <sql id="getAccountGroupList_body">
        <dynamic prepend="WHERE">
            <isNotNull prepend="AND" property="id"> id=#id# </isNotNull>
            <isNotNull prepend="AND" property="name"> name=#name# </isNotNull>
            <isNotNull prepend="AND" property="count"> count=#count# </isNotNull>
            <isNotNull prepend="AND" property="user_id"> user_id=#user_id# </isNotNull>
            <isNotNull prepend="AND" property="created_at"> created_at=#created_at# </isNotNull>
        </dynamic>
    </sql>
  
    <select id="getAccountGroupListWithAccount" parameterClass="com.xxxxx.accountGroup.model.AccountGroup" resultMap="NewResultMap">
        SELECT * FROM account_group
        <include refid="getAccountGroupList_body" />
    </select>
  
    <select id="getAccountByGroupId" parameterClass="int"
        resultClass="com.xxxxx.wxAccount.model.WxAccount">
        select *
        FROM wx_account
        where account_group_id = #id#
    </select>
</sqlMap>

 查询结果中group实体的accounts属性会自动填充为List<account>

ibatis主子表查询

标签:

原文地址:http://www.cnblogs.com/wudicaidou/p/4572242.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!