码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis性能优化二之多对多查询:用一次请求解决n次请求查询

时间:2015-08-05 01:06:54      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:性能优化   oracle sql   mybatis   

<resultMap type="com.cn.vo.Teacher" id="teacher">
		<id property="id" column="id" javaType="int" jdbcType="INTEGER" />
		<result property="name" column="name" javaType="string"
			jdbcType="VARCHAR" />

	<!-- 	<collection property="students" column="t_s_id" ofType="com.cn.vo.Student">
			<id property="sid" column="sid" javaType="int" jdbcType="INTEGER" />
			<result property="sname" column="sname" javaType="string"
				jdbcType="VARCHAR" />
		</collection> -->
		
		<collection property="students" resultMap="studentResultMap" />
		<collection property="goods" resultMap="goodsResultMap" />
	</resultMap>
	
	<resultMap type="com.cn.vo.Student" id="studentResultMap">
	        <id property="sid" column="sid" javaType="int" jdbcType="INTEGER" />
			<result property="sname" column="sname" javaType="string" jdbcType="VARCHAR" />
	</resultMap>
 
	<resultMap type="com.cn.vo.GoodItem" id="goodsResultMap">
	        <id property="gid" column="gid" javaType="int" jdbcType="INTEGER" />
			<result property="goodName" column="goodName" javaType="string" jdbcType="VARCHAR" />
			<result property="price" column="price" javaType="float" jdbcType="FLOAT" />
			<result property="good_sid" column="good_sid" javaType="int" jdbcType="INTEGER" />
	</resultMap>
	

	<select id="one2many" parameterType="int" resultMap="teacher">
		<!-- select
		t.id,t.name,s.t_s_id,s.sid,s.sname
		from teacher t 
		left join student s on t.id = s.t_s_id 
		left join goodItem g on g.good_sid=s.t_s_id
		where t.id = #{id}  --> 
		select
		t.id,t.name,s.t_s_id,s.sid,s.sname,g.gid,g.goodname,g.price,g.good_sid
		from teacher t 
		left join student s on   t.id = s.t_s_id 
		left join goodItem g on  g.good_sid = s.sid
		where t.id = #{id}
    </select>  


以上是优化的结论:用一次请求解决n次请求查询

题目:在teacher 表中找到该 teacher下面的n个student并找出n个学生每个学生有多少个goods。

sql:

CREATE TABLE  teacher  (
   id number  NOT NULL  ,
  name varchar(100) DEFAULT NULL,
  PRIMARY KEY (id)
) ;


CREATE TABLE  student  (
  sid number  NOT NULL  ,
  sname varchar(100) DEFAULT NULL,
  t_s_id number  NOT NULL  ,
  PRIMARY KEY (sid) 
) ;

 insert into teacher(id,name) values(111,'zhangsan');
 insert into teacher(id,name) values(222,'lisi');
 insert into student(sid,sname,t_s_id) values(1,'xs1',111);
 insert into student(sid,sname,t_s_id) values(2,'xs2',111);
 insert into student(sid,sname,t_s_id) values(3,'xs3',222);
 insert into student(sid,sname,t_s_id) values(4,'xs4',111);
 
select * from student;
select * from teacher;


select t.id,t.name,s.t_s_id,s.sid,s.sname
		from teacher t 
    left join student s 
    on t.id = s.t_s_id where t.id = 111 
    
    
    create table goodItem(
     gid number not null,
     goodName varchar(10),
     price float,
     good_sid number
    )
    
    insert into goodItem(gid,Goodname,Price,Good_Sid)
    values(1,'iphone6','6000',2);
    insert into goodItem(gid,Goodname,Price,Good_Sid)
    values(2,'iphone5','5000',2);
    insert into goodItem(gid,Goodname,Price,Good_Sid)
    values(3,'iphone4','4000',2);
     insert into goodItem(gid,Goodname,Price,Good_Sid)
    values(4,'iphone3','3000',1);
    
    


vo:

package com.cn.vo;

public class Student {
	private int sid;
	private String sname;
	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}

	 

}


package com.cn.vo;

import java.util.List;



public class Teacher {
	private int id;
	private String name;
	private List<Student> students;
	private List<GoodItem> goods;
	
	

	public List<GoodItem> getGoods() {
		return goods;
	}

	public void setGoods(List<GoodItem> goods) {
		this.goods = goods;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

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

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


package com.cn.vo;

public class Item {
	private  Integer  gid;
	private String goodName;
	private float price;
	private Integer good_sid;
	public Integer getGid() {
		return gid;
	}
	public void setGid(Integer gid) {
		this.gid = gid;
	}
	public String getGoodName() {
		return goodName;
	}
	public void setGoodName(String goodName) {
		this.goodName = goodName;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	public Integer getGood_sid() {
		return good_sid;
	}
	public void setGood_sid(Integer good_sid) {
		this.good_sid = good_sid;
	}
	
	
}


package com.cn.vo;

import java.util.List;

public class GoodItem extends Item{

/*	private List<Student> students;
	private List<Teacher> teachers;
	
	public List<Student> getStudents() {
		return students;
	}
	public void setStudents(List<Student> students) {
		this.students = students;
	}
	public List<Teacher> getTeachers() {
		return teachers;
	}
	public void setTeachers(List<Teacher> teachers) {
		this.teachers = teachers;
	}
	 */
	
	
}






版权声明:本文为博主原创文章,未经博主允许不得转载。

mybatis性能优化二之多对多查询:用一次请求解决n次请求查询

标签:性能优化   oracle sql   mybatis   

原文地址:http://blog.csdn.net/luozhonghua2014/article/details/47285233

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!