zoukankan      html  css  js  c++  java
  • 关联查询

    关联映射一对多

      以国家和省份对应的一对多关系举例。

          数据库:

            Country表:

              

            Provincial表:

              

          实体类:

            Country表

              public class Country {
                     private int cid;
                     private String cname;
                     private List<Provincial> ps;//省份的泛型集合
                     //省略get/set方法
              }

             Provincial表

              public class Provincial {
                     private int pid;
                     private String pname;

                     //省略get/set方法

              }    

          Dao层接口:

            

             编写小配置xml文件:      

            <mapper namespace="com.pr.dao.CountryDao">
                <!--由于是关联查询 返回的是多张表中的结果集,必须定义resultMap映射-->
                <resultMap id="countryMap" type="com.pr.entity.Country">
                    <id property="cid" column="cid"></id>
                    <result property="cname" column="cname"></result>
                    <!--此处使用的是collection节点,由于在Country类中插入的是List集合    ofType:为集合中的泛型 -->
                    <collection property="ps" ofType="com.pr.entity.·">
                        <!--在collection中声明Provincial中的属性与表中列的映射-->
                        <id column="pid" property="pid"></id>
                        <result column="pname" property="pname"></result>
                    </collection>
                </resultMap>            

                <select id="getOne" resultType="com.pr.entity.Country" parameterType="int" resultMap="countryMap">
                    SELECT c.cid,c.cname,pid,pname FROM country c INNER JOIN provincial p ON c.cid=p.cid WHERE c.cid=#{cid}
                </select>

            </mapper>

          测试:

            

    自连接一对多

        数据库:

          

        实体类: 

          public class Category {
                  private int id;
                 private String name;
                  private int type;
                   private List<Category> lists=new ArrayList<>();

                 //省略get/set方法

          }

        Dao层接口:

          

        编写xml文件:

        <mapper namespace="com.pr.dao.Easybuy_product_category">
              <resultMap id="cateMap" type="com.pr.entity.Easybuy_product_categorys">
                  <id column="id" property="id"></id>
                  <result column="name" property="name"></result>
                <result column="type" property="type"></result>
                <!--
                    参照实体类,lists变量的类型是集合(典型的1:N的关系)
                    select:指定查询关联对象的select语句(getOneByParent)
                    column:查询关联对象select语句的条件值
                    本例:根据id获取对应的分类后,以此id为条件获取子级分类(parentId=父级分类ID)
                -->
                <collection property="lists" ofType="com.pr.entity.Easybuy_product_categorys" select="getTwo" column="id"/>
            </resultMap>

            <!--多对一-->
            <select id="getTwo" parameterType="int" resultMap="cateMap">
                select * from easybuy_product_category where parentId=#{id}
            </select>
        </mapper>

        测试:

          

    关联映射多对一   

        数据库: 

           Country表:

              

            Provincial表:

              

        实体类:

           Country表

              public class Country {
                     private int cid;
                     private String cname;
                     private List<Provincial> ps;//省份的泛型集合
                     //省略get/set方法
              }

             Provincial表

              public class Provincial {
                     private int pid;
                     private String pname;

                     //省略get/set方法

              }

        Dao层接口:

          

        编写xml文件:     

          <mapper namespace="com.pr.dao.CountryDao">
              <resultMap id="ProvincialMap" type="com.pr.entity.Provincial">
                  <id column="pid" property="pid"></id>
                  <result column="pname" property="pname"></result>
                  <!--当关联对象为普通对象时,使用association标签   javaType:为关联对象的类型-->
                  <association property="country" javaType="com.pr.entity.Country">
                      <id column="cid" property="cid"></id>
                      <result column="cname" property="cname"></result>
                  </association>
              </resultMap>

              <select id="getTwo" resultMap="ProvincialMap">
                   select * from provincial p inner join country c on p.cid=c.cid;
              </select>
          </mapper>

        测试:

          

    关联映射多对多

        数据库:

          student表:

            

          teacher表:

            

           stutea表:

            

        实体类:

          student类:    

             public class Student {
                  private int stuid;
                  private String stuname;
                  private List<Teacher> teachers=new ArrayList<>();

                  public List<Teacher> getTeachers() {
                        return teachers;
                  }

                  public void setTeachers(List<Teacher> teachers) {
                        this.teachers = teachers;
                  }

                  public int getStuid() {
                        return stuid;
                  }

                  public void setStuid(int stuid) {
                        this.stuid = stuid;
                  }

                  public String getStuname() {
                        return stuname;
                  }

                  public void setStuname(String stuname) {
                        this.stuname = stuname;
                  }
            }

          teacher表:      

            public class Teacher {
                  private  int teaid;
                  private  String teaname;
                  private List<Student> students=new ArrayList<>();

                  public List<Student> getStudents() {
                        return students;
                  }

                  public void setStudents(List<Student> students) {
                        this.students = students;
                  }

                  public int getTeaid() {
                        return teaid;
                  }

                  public void setTeaid(int teaid) {
                        this.teaid = teaid;
                  }

                  public String getTeaname() {
                        return teaname;
                  }

                  public void setTeaname(String teaname) {
                        this.teaname = teaname;
                  }
            }

        Dao层接口:

            

        编写xml文件:   

          <mapper namespace="com.pr.dao.TeacherDao">
              <resultMap id="teacherMap" type="com.pr.entity.Teacher">
                  <id column="teaid" property="teaid"></id>
                  <result column="teaname" property="teaname"></result>
                  <!--为students集合设置关联属性-->
                  <collection property="students" ofType="com.pr.entity.Student">
                      <id column="stuid" property="stuid"></id>
                      <result column="stuname" property="stuname"></result>
                  </collection>
              </resultMap>

              <select id="getOne" resultMap="teacherMap">
                  SELECT s.*,t.* FROM teacher t,student s,stutea ts  WHERE t.`teaid`=ts.`teaid` AND s.`stuid`=ts.`stuid`
              </select>
          </mapper>

        测试:

          

  • 相关阅读:
    vs2015+opencv3.3.1 +Eigen 3.3.4 c++实现 薄膜插值 泊松图像编辑(v=0||Δf=0)
    vs2015+opencv3.3.1 实现 c++ 双边滤波器(Bilateral Filter)
    vs2015+opencv3.3.1 实现 c++ 彩色高斯滤波器(Gaussian Smoothing, Gaussian Blur, Gaussian Filter)
    vs2015+opencv3.3.1 实现 c++ 灰度高斯滤波器
    vs2015+opencv3.3.1 实现 c++ 直方图均衡化
    函数形参为基类数组,实参为继承类数组,下存在的问题------c++程序设计原理与实践(进阶篇)
    函数返回值string与返回值bool区别------c++程序设计原理与实践(进阶篇)
    (c++11)随机数------c++程序设计原理与实践(进阶篇)
    实现求解线性方程(矩阵、高斯消去法)------c++程序设计原理与实践(进阶篇)
    Centos ATI 显卡安装,“LCD 信号超出范围” 解决方法
  • 原文地址:https://www.cnblogs.com/wnwn/p/11661503.html
Copyright © 2011-2022 走看看