zoukankan      html  css  js  c++  java
  • 学习Mybatis中的一对多表关联

    一、创建Person_class表。

    添加属性classid和classname;

    设置外键。

    二、创建实体类:

    package org.ruangong.entity;
    
    import java.util.List;
    
    public class PersonClass {
    private int classid;
    private String classname;
    List<Person> person;
    public int getClassid() {
    	return classid;
    }
    public void setClassid(int classid) {
    	this.classid = classid;
    }
    public String getClassname() {
    	return classname;
    }
    @Override
    public String toString() {
    	return "PersonClass [classid=" + classid + ", classname=" + classname + ", person=" + person + "]";
    }
    public void setClassname(String classname) {
    	this.classname = classname;
    }
    }
    

      在personMapper.xml中添加:

     <!-- 一对多关联查询 -->
    	   <select id="queryPerson_cardId3" resultMap="class_persons_map" parameterType="int">
    	 	select C.*,P.* from person2 P inner join person_class C on C.classid = P.classid
    	 	 where C.classid = #{classid}
    	 </select>
    	 <resultMap type="PersonClass" id="class_persons_map">
    	 	<id property="classid" column="classid"/>
    	 	<result property="classname" column="classname"/>
    	 	<!-- 一对多用collection -->
    	  	<collection property="person" ofType="Person">
    	  		<id property="id" column="id"/>
    		  <result property="name" column="name"/>
    		  <result property="age" column="age"/>
    		  <result property="persex" column="sex"/>
    	  	</collection>
    	 </resultMap>
    

      在resultMap标签中,若为一对多表关联,那么在这里必须使用collection标签,若为一对一就是用association标签。

    然后要注意若返回值为集合元素,那么必须使用ofType,如果是对象就是用javaType。

  • 相关阅读:
    get请求乱码情况
    write()和prinln()的区别?
    校验码实现
    下载图片代码并且解析乱码
    servlet下根据相对路径找资源
    url-pattern配置
    获取网站资源 getResourceAsStream
    Servlet线程安全性
    http1.1 协议响应方面参数
    HTTP1.1协议请求方面参数
  • 原文地址:https://www.cnblogs.com/jccjcc/p/13964412.html
Copyright © 2011-2022 走看看