ITPub博客

首页 > IT职业 > > mybatis 多对多 在collection中使用select查询报错。。求解

mybatis 多对多 在collection中使用select查询报错。。求解

作者:jasonhsp 时间:2019-04-17 15:14:49 0 删除 编辑

website类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Website {
    private Integer id;
    private String name;
    private String websiteAddress;
    private List<Category> categoryList;
    private List<Score> scoreList;
 
    public Website() {
        super();
    }
 
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }
    public String getWebsiteAddress() {
        return websiteAddress;
    }
    public void setWebsiteAddress(String websiteAddress) {
        this.websiteAddress = websiteAddress == null ? null : websiteAddress.trim();
    }
    public List<Category> getCategoryList() {
        return categoryList;
    }
    public void setCategoryList(List<Category> categoryList) {
        this.categoryList = categoryList;
    }
    public List<Score> getScoreList() {
        return scoreList;
    }
    public void setScoreList(List<Score> scoreList) {
        this.scoreList = scoreList;
    }
}

category类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class Category {
    private Integer id;
    private String name;
    private Integer parentCategoryId;
    private List<User> userList;
    private List<Website> websiteList;
     
    public Integer getId() {
        return id;
    }
    public Category() {
        super();
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }
    public Integer getParentCategoryId() {
        return parentCategoryId;
    }
    public void setParentCategoryId(Integer parentCategoryId) {
        this.parentCategoryId = parentCategoryId;
    }
    public List<User> getUserList() {
        return userList;
    }
    public void setUserList(List<User> userList) {
        this.userList = userList;
    }
    public List<Website> getWebsiteList() {
        return websiteList;
    }
    public void setWebsiteList(List<Website> websiteList) {
        this.websiteList = websiteList;
    }
}

中间类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class WebsiteToCategory {
    private Integer websiteId;
    private Integer categoryId;
 
    public WebsiteToCategory() {
        super();
    }
     
    public Integer getWebsiteId() {
        return websiteId;
    }
    public void setWebsiteId(Integer websiteId) {
        this.websiteId = websiteId;
    }
    public Integer getCategoryId() {
        return categoryId;
    }
    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }
}

websitemapper:

1
2
3
4
5
6
7
8
9
10
11
12
 <resultMap id="BaseResultMap" type="com.wechat.bean.score.Website">
      <id column="id" jdbcType="INTEGER" property="id"/>
      <result column="name" jdbcType="VARCHAR" property="name"/>
      <result column="website_address" jdbcType="VARCHAR" property="websiteAddress"/>
      <collection property="categoryList" column="{website_id=id}"
                  select="com.wechat.dao.WebsiteToCategoryMapper.selectCategoryByWebsiteId"/>
  </resultMap>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  select *
  from website
  where id = #{id,jdbcType=INTEGER}
</select>

categorymapper:

1
2
3
4
5
6
7
8
9
10
11
12
13
<resultMap id="BaseResultMap" type="com.wechat.bean.score.Category">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="parent_category_id" jdbcType="INTEGER" property="parentCategoryId" />
    <collection property="websiteList" ofType="com.wechat.bean.score.Website" column="category_id" select="com.wechat.dao.WebsiteToCategoryMapper.selectWebsiteByCategoryId" />
  </resultMap>
 
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select *
    from category
    where id = #{id,jdbcType=INTEGER}
  </select>
</mapper>

中间类mapper:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!--根据网站id查分类-->
<resultMap id="BaseResultMap1" type="com.wechat.bean.score.Category">
  <id column="id" jdbcType="INTEGER" property="id" />
  <result column="name" jdbcType="VARCHAR" property="name" />
  <result column="parent_category_id" jdbcType="INTEGER" property="parentCategoryId" />
</resultMap>
<select id="selectCategoryByWebsiteId" parameterType="Integer" resultMap="BaseResultMap1">
  SELECT c.* FROM category c,website_to_category wc WHERE c.id=wc.category_id and wc.website_id=#{website_id}
</select>
 
<!--根据分类id查网站-->
<resultMap id="BaseResultMap2" type="com.wechat.bean.score.Website">
  <id column="id" jdbcType="INTEGER" property="id"/>
  <result column="name" jdbcType="VARCHAR" property="name" />
  <result column="website_address" jdbcType="VARCHAR" property="websiteAddress" />
</resultMap>
<select id="selectWebsiteByCategoryId" parameterType="Integer" resultMap="BaseResultMap2">
  SELECT w.* FROM website w,website_to_category wc WHERE w.id=wc.website_id and wc.category_id=#{category_id}
</select>


直接执行中间类的两个方法是都可以查询出来的,但放在collection中子查询就不行


开始我在websitemapper中的resultmap中的collenction的column写的市sebsite_id,查询出来的对象list是空的

又改成column="{website_id=id}"想让他强制执行,结果就一直报错

Error instantiating class java.lang.Integer with invalid types () or values (). Cause: java.lang.NoSuchMethodException: java.lang.Integer.<init>()


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/615/viewspace-3164643/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-12

  • 博文量
    342
  • 访问量
    2721