需要一对一查询Course和Teacher两个表
新建一个类,继承Course类,然后将Teacher类中的属性加到这个新类中,还需要有get和set方法
之后用debug查看
package com.zym.pojo; public class ct extends Course{ // private String t_id; private String t_name; // @Override //// public String getT_id() { //// return t_id; //// } // // @Override // public void setT_id(String t_id) { // this.t_id = t_id; // } public String getT_name() { return t_name; } public void setT_name(String t_name) { this.t_name = t_name; } }
使用resultMap,定义专门的resultMap用于映射一对一查询结果。
在一个类中,添加另一个类的对象属性,并设置get set方法
public class Teacher { private String t_id; private String t_name; private Course Course; public Course getCourse() { return Course; } public void setCourse_obj(Course course_obj) { Course = course_obj; }
xml
<resultMap id="Teacher_map" type="Teacher"> <id property="t_id" column="t_id"/> <result property="t_name" column="t_name"/> <association property="Course" javaType="Course"> <id property="t_id" column="t_id"/> <result property="c_name" column="c_name"/> <result property="c_id" column="c_id"/> </association> </resultMap> <select id="select_onebyone_map" resultMap="Teacher_map"> SELECT c_name,t_name,c.c_id,t.t_id,c.t_id FROM Course c LEFT JOIN Teacher t on c.t_id=t.t_id; </select>
student和score的一对多
public class Student { private Integer s_id; private String s_name; private Date s_birth; private String s_sex; private List<Score> Scores; public List<com.zym.pojo.Score> getScore() { return Scores; } public void setScore(List<com.zym.pojo.Score> score) { Scores = score; }
xml
<resultMap id="stu_score_map" type="Student"> <id property="s_id" column="s_id"/> <result property="s_name" column="s_name"/> <result property="s_birth" column="s_birth"/> <result property="s_sex" column="s_sex"/> <collection property="Scores" javaType="List" ofType="Score"> <id property="s_id" column="s_id_2"/> <result property="s_score" column="s_score"/> <result property="c_id" column="c_id"/> </collection> </resultMap> <select id="select_ones_score" resultMap="stu_score_map"> SELECT s_name,stu.s_id,sco.s_id as sco_s_id,s_sex,s_score FROM Student stu LEFT JOIN Score sco on stu.s_id=sco.s_id </select>
public class Goods { private Integer id; private String gname; private Double price; private String gtype; //多对多 private List<Order> order;
public class Order implements Serializable{ private Integer id; private Integer userId; private Integer number; private Date createtime; private String note; // 一对一 private User user; // 多对多 private List<Goods> listgoods;
xml
<mapper namespace="com.jy.mapper.UserMapper"> <!-- mang2mang 订单和商品信息 --> <resultMap type="Order" id="orderandgoodsmap"> <!-- 主键映射关系 --> <id property="id" column="oid"/>/映射中间表 <result property="createtime" column="createtime"/> <result property="number" column="number"/> <collection property="listgoods" javaType="list" ofType="goods"> <!-- 主键映射关系 --> <id property="id" column="gid"/> /映射中间表 <result property="gname" column="gname"/> <result property="gtype" column="gtype"/> </collection> </resultMap> <select id="selectOrderAndGoods" resultMap="orderandgoodsmap"> select o.id oid,o.createtime,o.number,g.id gid,g.gname,g.gtype from t_goods g,t_order o,order_goods og where g.id=og.gid and o.id=og.oid </select>
原文:https://www.cnblogs.com/taozizainali/p/11178183.html