多表关联查询_resultMap_集合对象

多表关联查询_resultMap_集合对象_N+1方式实现

 package com.bjsxt.mapper;

 import java.util.List;

 import com.bjsxt.pojo.Clazz;

 public interface ClazzMapper {

     List<Clazz> selAll();
}
 package com.bjsxt.mapper;

 import java.util.List;

 import com.bjsxt.pojo.Student;

 public interface StudentMapper {

     List<Student> selByCid(int cid);
}
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjsxt.mapper.ClazzMapper">
<resultMap type="clazz" id="cmap">
<id property="id" column="id" />
<collection property="stus" select="com.bjsxt.mapper.StudentMapper.selByCid" column="id"></collection>
</resultMap>
<select id="selAll" resultMap="cmap">
select * from t_class
</select>
</mapper>
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjsxt.mapper.StudentMapper">
<select id="selByCid" resultType="student" parameterType="int">
select * from t_student where cid=#{0}
</select>
</mapper>
 package com.bjsxt.pojo;

 import java.io.Serializable;
import java.util.List; public class Clazz implements Serializable { private int id;
private String name;
private String room;
private List<Student> stus; public Clazz() {
super();
} 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 String getRoom() {
return room;
} public void setRoom(String room) {
this.room = room;
} public List<Student> getStus() {
return stus;
} public void setStus(List<Student> stus) {
this.stus = stus;
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((room == null) ? 0 : room.hashCode());
result = prime * result + ((stus == null) ? 0 : stus.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Clazz other = (Clazz) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (room == null) {
if (other.room != null)
return false;
} else if (!room.equals(other.room))
return false;
if (stus == null) {
if (other.stus != null)
return false;
} else if (!stus.equals(other.stus))
return false;
return true;
} @Override
public String toString() {
return "Clazz [id=" + id + ", name=" + name + ", room=" + room + ", stus=" + stus + "]";
} }
 package com.bjsxt.pojo;

 import java.io.Serializable;

 public class Student implements Serializable {

     private int id;
private String name;
private int age;
private String gender;
private int cid; public Student() {
super();
} 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 int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getGender() {
return gender;
} public void setGender(String gender) {
this.gender = gender;
} public int getCid() {
return cid;
} public void setCid(int cid) {
this.cid = cid;
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + cid;
result = prime * result + ((gender == null) ? 0 : gender.hashCode());
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (cid != other.cid)
return false;
if (gender == null) {
if (other.gender != null)
return false;
} else if (!gender.equals(other.gender))
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
} @Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + ", cid=" + cid + "]";
} }
 package com.bjsxt.service;

 import java.util.List;

 import com.bjsxt.pojo.Clazz;

 public interface ClazzService {

     List<Clazz> selAll();
}
 package com.bjsxt.service.impl;

 import java.util.List;

 import org.apache.ibatis.session.SqlSession;

 import com.bjsxt.mapper.ClazzMapper;
import com.bjsxt.pojo.Clazz;
import com.bjsxt.service.ClazzService;
import com.bjsxt.util.MyBatisUtil; public class ClazzServiceImpl implements ClazzService { public List<Clazz> selAll() {
SqlSession session = MyBatisUtil.getSession(); ClazzMapper mapper = session.getMapper(ClazzMapper.class);
List<Clazz> list = mapper.selAll(); session.close();
return list;
} }
 package com.bjsxt.test;

 import java.util.List;

 import com.bjsxt.pojo.Clazz;
import com.bjsxt.service.ClazzService;
import com.bjsxt.service.impl.ClazzServiceImpl; public class TestQuery { public static void main(String[] args) {
ClazzService cs = new ClazzServiceImpl();
List<Clazz> list = cs.selAll();
for (Clazz clazz : list) {
System.out.println(clazz);
}
} }
 package com.bjsxt.util;

 import java.io.IOException;
import java.io.InputStream; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtil { private static SqlSessionFactory factory = null; static {
try {
InputStream is = Resources.getResourceAsStream("mybatis-cfg.xml");
factory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}
} public static SqlSession getSession() {
SqlSession session = null;
if (factory != null) {
// true表示开启自动提交
// session = factory.openSession(true);
session = factory.openSession();
}
return session;
}
}
 jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/java505
jdbc.username=root
jdbc.password=root
 # Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=ERROR, CONSOLE
# log4j.rootCategory=DEBUG, CONSOLE, LOGFILE # 单独设置SQL语句的输出级别为DEBUG级别
log4j.logger.com.bjsxt.mapper=DEBUG # CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n # LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:/test.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=- %m %l%n

多表关联查询_resultMap_集合对象_关联方式实现

 package com.bjsxt.mapper;

 import java.util.List;

 import com.bjsxt.pojo.Clazz;

 public interface ClazzMapper {

     List<Clazz> selAll();
}
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjsxt.mapper.ClazzMapper">
<resultMap type="clazz" id="cmap">
<id property="id" column="cid" />
<result property="name" column="cname" />
<result property="room" column="room" />
<collection property="stus" javaType="list" ofType="student">
<id property="id" column="sid" />
<result property="name" column="sname" />
<result property="age" column="age" />
<result property="gender" column="gender" />
<result property="cid" column="cid" />
</collection>
</resultMap>
<select id="selAll" resultMap="cmap">
select c.id cid, c.name cname, c.room, s.id sid, s.name sname, s.age, s.gender
from t_student s
right join t_class c
on s.cid=c.id
</select>
</mapper>
 package com.bjsxt.pojo;

 import java.io.Serializable;
import java.util.List; public class Clazz implements Serializable { private int id;
private String name;
private String room;
private List<Student> stus; public Clazz() {
super();
} 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 String getRoom() {
return room;
} public void setRoom(String room) {
this.room = room;
} public List<Student> getStus() {
return stus;
} public void setStus(List<Student> stus) {
this.stus = stus;
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((room == null) ? 0 : room.hashCode());
result = prime * result + ((stus == null) ? 0 : stus.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Clazz other = (Clazz) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (room == null) {
if (other.room != null)
return false;
} else if (!room.equals(other.room))
return false;
if (stus == null) {
if (other.stus != null)
return false;
} else if (!stus.equals(other.stus))
return false;
return true;
} @Override
public String toString() {
return "Clazz [id=" + id + ", name=" + name + ", room=" + room + ", stus=" + stus + "]";
} }
 package com.bjsxt.pojo;

 import java.io.Serializable;

 public class Student implements Serializable {

     private int id;
private String name;
private int age;
private String gender;
private int cid; public Student() {
super();
} 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 int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getGender() {
return gender;
} public void setGender(String gender) {
this.gender = gender;
} public int getCid() {
return cid;
} public void setCid(int cid) {
this.cid = cid;
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + cid;
result = prime * result + ((gender == null) ? 0 : gender.hashCode());
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (cid != other.cid)
return false;
if (gender == null) {
if (other.gender != null)
return false;
} else if (!gender.equals(other.gender))
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
} @Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + ", cid=" + cid + "]";
} }
 package com.bjsxt.service;

 import java.util.List;

 import com.bjsxt.pojo.Clazz;

 public interface ClazzService {

     List<Clazz> selAll();
}
package com.bjsxt.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.bjsxt.mapper.ClazzMapper;
import com.bjsxt.pojo.Clazz;
import com.bjsxt.service.ClazzService;
import com.bjsxt.util.MyBatisUtil; public class ClazzServiceImpl implements ClazzService { public List<Clazz> selAll() {
SqlSession session = MyBatisUtil.getSession(); ClazzMapper mapper = session.getMapper(ClazzMapper.class);
List<Clazz> list = mapper.selAll(); session.close();
return list;
} }
 package com.bjsxt.test;

 import java.util.List;

 import com.bjsxt.pojo.Clazz;
import com.bjsxt.service.ClazzService;
import com.bjsxt.service.impl.ClazzServiceImpl; public class TestQuery { public static void main(String[] args) {
ClazzService cs = new ClazzServiceImpl();
List<Clazz> list = cs.selAll();
for (Clazz clazz : list) {
System.out.println(clazz);
}
} }
 package com.bjsxt.util;

 import java.io.IOException;
import java.io.InputStream; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtil { private static SqlSessionFactory factory = null; static {
try {
InputStream is = Resources.getResourceAsStream("mybatis-cfg.xml");
factory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}
} public static SqlSession getSession() {
SqlSession session = null;
if (factory != null) {
// true表示开启自动提交
// session = factory.openSession(true);
session = factory.openSession();
}
return session;
}
}
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/java505
jdbc.username=root
jdbc.password=root
# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=ERROR, CONSOLE
# log4j.rootCategory=DEBUG, CONSOLE, LOGFILE # 单独设置SQL语句的输出级别为DEBUG级别
log4j.logger.com.bjsxt.mapper=DEBUG # CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n # LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:/test.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=- %m %l%n
上一篇:c++primerplus(第六版)编程题——第5章(循环和关系表达式)


下一篇:RACSignal常见用法