yangshuaihahaha
yangshuaihahaha
2017-03-08 03:26

mybatis嵌套查询,一个resultMap中有多个association标签,怎么写sql语句?

  • 多对多
  • resultmap
  • mybatis

1. 一下是我的实体类

  1. private int id;
  2. private Users users;//房屋持有者
  3. private Types types;//房屋类型
  4. private String title;
  5. private String description;
  6. private double price;
  7. private Date pubdate;//发布日期
  8. private int floorage;//建筑面积
  9. private int contact;//联系方式
  10. private Street street;//房屋所属街区
  11. public int getId() {
  12. return id;
  13. }
  14. public void setId(int id) {
  15. this.id = id;
  16. }
  17. public String getTitle() {
  18. return title;
  19. }
  20. public void setTitle(String title) {
  21. this.title = title;
  22. }
  23. public String getDescription() {
  24. return description;
  25. }
  26. public void setDescription(String description) {
  27. this.description = description;
  28. }
  29. public double getPrice() {
  30. return price;
  31. }
  32. public void setPrice(double price) {
  33. this.price = price;
  34. }
  35. public Date getPubdate() {
  36. return pubdate;
  37. }
  38. public void setPubdate(Date pubdate) {
  39. this.pubdate = pubdate;
  40. }
  41. public int getFloorage() {
  42. return floorage;
  43. }
  44. public void setFloorage(int floorage) {
  45. this.floorage = floorage;
  46. }
  47. public int getContact() {
  48. return contact;
  49. }
  50. public void setContact(int contact) {
  51. this.contact = contact;
  52. }
  53. public Users getUsers() {
  54. return users;
  55. }
  56. public void setUsers(Users users) {
  57. this.users = users;
  58. }
  59. public Types getTypes() {
  60. return types;
  61. }
  62. public void setTypes(Types types) {
  63. this.types = types;
  64. }
  65. public Street getStreet() {
  66. return street;
  67. }
  68. public void setStreet(Street street) {
  69. this.street = street;
  70. }

##下面是对应的xml文件,以及sql语句

 <mapper namespace="com.rhit.dao">

    <resultMap id="HouseMap" type="com.rhit.entity.House">
        <id property="id" column="ID" />

        <result property="title" column="TITLE" />
        <result property="description" column="DESCRIPTION" />
        <result property="price" column="PRICE" />
        <result property="pubdate" column="PUBDATE" />
        <result property="floorage" column="FLOORAGE" />
        <result property="contact" column="CONTACT" />

        <association property="types" javaType="Types">
        <id property="id" column="ID" />
        <result property="name" column="NAME" />
        </association>

        <association property="street" javaType="Street">
        <id property="id" column="ID" />
        <result property="name" column="NAME" />
        </association>

        <association property="users" javaType="Users">
        <id property="id" column="ID" />
        <result property="name" column="NAME" />
        <result property="password" column="PASSWORD" />
        <result property="telphone" column="TELEPHONE" />
        <result property="username" column="USERNAME" />
        <result property="isadmin" column="ISADMIN" />
        </association>


    </resultMap>


    <select id="getHouse" resultMap="HouseMap">

    select * from house h,users u,types t,street s where  u.id=h.user_id and t.id=h.type_id and s.id=h.street_id
    </select>

</mapper>

##下面是调用测试
public class Test {

public static void main(String[] args) {
    SqlSession session=MyBatisUtil.getSession();
    List<House> list=session.selectList("getHouse");

    System.out.println(list.get(0).getStreet().getName());
    System.out.println(list.get(0).getTypes().getName());
  System.out.println(list.get(0).getUsers().getName());
}

}

##问题
1.测试结果只获取到了user的名字,而types和street的名字跟user的名字一样?
2.一个resultMap中可以有多个association么?这种多对多的关系该怎么处理?
3.一个resultMap可以有多个collection么?怎么写sql语句呢?

求大神么解答,在线等,先谢过!

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答