多表关联查询_resultMap_集合对象
多表关联查询_resultMap_集合对象_N+1方式实现
1 package com.bjsxt.mapper; 2 3 import java.util.List; 4 5 import com.bjsxt.pojo.Clazz; 6 7 public interface ClazzMapper { 8 9 List<Clazz> selAll(); 10 }
1 package com.bjsxt.mapper; 2 3 import java.util.List; 4 5 import com.bjsxt.pojo.Student; 6 7 public interface StudentMapper { 8 9 List<Student> selByCid(int cid); 10 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.bjsxt.mapper.ClazzMapper"> 6 <resultMap type="clazz" id="cmap"> 7 <id property="id" column="id" /> 8 <collection property="stus" select="com.bjsxt.mapper.StudentMapper.selByCid" column="id"></collection> 9 </resultMap> 10 <select id="selAll" resultMap="cmap"> 11 select * from t_class 12 </select> 13 </mapper>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.bjsxt.mapper.StudentMapper"> 6 <select id="selByCid" resultType="student" parameterType="int"> 7 select * from t_student where cid=#{0} 8 </select> 9 </mapper>
1 package com.bjsxt.pojo; 2 3 import java.io.Serializable; 4 import java.util.List; 5 6 public class Clazz implements Serializable { 7 8 private int id; 9 private String name; 10 private String room; 11 private List<Student> stus; 12 13 public Clazz() { 14 super(); 15 } 16 17 public int getId() { 18 return id; 19 } 20 21 public void setId(int id) { 22 this.id = id; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public String getRoom() { 34 return room; 35 } 36 37 public void setRoom(String room) { 38 this.room = room; 39 } 40 41 public List<Student> getStus() { 42 return stus; 43 } 44 45 public void setStus(List<Student> stus) { 46 this.stus = stus; 47 } 48 49 @Override 50 public int hashCode() { 51 final int prime = 31; 52 int result = 1; 53 result = prime * result + id; 54 result = prime * result + ((name == null) ? 0 : name.hashCode()); 55 result = prime * result + ((room == null) ? 0 : room.hashCode()); 56 result = prime * result + ((stus == null) ? 0 : stus.hashCode()); 57 return result; 58 } 59 60 @Override 61 public boolean equals(Object obj) { 62 if (this == obj) 63 return true; 64 if (obj == null) 65 return false; 66 if (getClass() != obj.getClass()) 67 return false; 68 Clazz other = (Clazz) obj; 69 if (id != other.id) 70 return false; 71 if (name == null) { 72 if (other.name != null) 73 return false; 74 } else if (!name.equals(other.name)) 75 return false; 76 if (room == null) { 77 if (other.room != null) 78 return false; 79 } else if (!room.equals(other.room)) 80 return false; 81 if (stus == null) { 82 if (other.stus != null) 83 return false; 84 } else if (!stus.equals(other.stus)) 85 return false; 86 return true; 87 } 88 89 @Override 90 public String toString() { 91 return "Clazz [id=" + id + ", name=" + name + ", room=" + room + ", stus=" + stus + "]"; 92 } 93 94 }
1 package com.bjsxt.pojo; 2 3 import java.io.Serializable; 4 5 public class Student implements Serializable { 6 7 private int id; 8 private String name; 9 private int age; 10 private String gender; 11 private int cid; 12 13 public Student() { 14 super(); 15 } 16 17 public int getId() { 18 return id; 19 } 20 21 public void setId(int id) { 22 this.id = id; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public int getAge() { 34 return age; 35 } 36 37 public void setAge(int age) { 38 this.age = age; 39 } 40 41 public String getGender() { 42 return gender; 43 } 44 45 public void setGender(String gender) { 46 this.gender = gender; 47 } 48 49 public int getCid() { 50 return cid; 51 } 52 53 public void setCid(int cid) { 54 this.cid = cid; 55 } 56 57 @Override 58 public int hashCode() { 59 final int prime = 31; 60 int result = 1; 61 result = prime * result + age; 62 result = prime * result + cid; 63 result = prime * result + ((gender == null) ? 0 : gender.hashCode()); 64 result = prime * result + id; 65 result = prime * result + ((name == null) ? 0 : name.hashCode()); 66 return result; 67 } 68 69 @Override 70 public boolean equals(Object obj) { 71 if (this == obj) 72 return true; 73 if (obj == null) 74 return false; 75 if (getClass() != obj.getClass()) 76 return false; 77 Student other = (Student) obj; 78 if (age != other.age) 79 return false; 80 if (cid != other.cid) 81 return false; 82 if (gender == null) { 83 if (other.gender != null) 84 return false; 85 } else if (!gender.equals(other.gender)) 86 return false; 87 if (id != other.id) 88 return false; 89 if (name == null) { 90 if (other.name != null) 91 return false; 92 } else if (!name.equals(other.name)) 93 return false; 94 return true; 95 } 96 97 @Override 98 public String toString() { 99 return "Student [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + ", cid=" + cid + "]"; 100 } 101 102 }
1 package com.bjsxt.service; 2 3 import java.util.List; 4 5 import com.bjsxt.pojo.Clazz; 6 7 public interface ClazzService { 8 9 List<Clazz> selAll(); 10 }
1 package com.bjsxt.service.impl; 2 3 import java.util.List; 4 5 import org.apache.ibatis.session.SqlSession; 6 7 import com.bjsxt.mapper.ClazzMapper; 8 import com.bjsxt.pojo.Clazz; 9 import com.bjsxt.service.ClazzService; 10 import com.bjsxt.util.MyBatisUtil; 11 12 public class ClazzServiceImpl implements ClazzService { 13 14 public List<Clazz> selAll() { 15 SqlSession session = MyBatisUtil.getSession(); 16 17 ClazzMapper mapper = session.getMapper(ClazzMapper.class); 18 List<Clazz> list = mapper.selAll(); 19 20 session.close(); 21 return list; 22 } 23 24 }
1 package com.bjsxt.test; 2 3 import java.util.List; 4 5 import com.bjsxt.pojo.Clazz; 6 import com.bjsxt.service.ClazzService; 7 import com.bjsxt.service.impl.ClazzServiceImpl; 8 9 public class TestQuery { 10 11 public static void main(String[] args) { 12 ClazzService cs = new ClazzServiceImpl(); 13 List<Clazz> list = cs.selAll(); 14 for (Clazz clazz : list) { 15 System.out.println(clazz); 16 } 17 } 18 19 }
1 package com.bjsxt.util; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 11 public class MyBatisUtil { 12 13 private static SqlSessionFactory factory = null; 14 15 static { 16 try { 17 InputStream is = Resources.getResourceAsStream("mybatis-cfg.xml"); 18 factory = new SqlSessionFactoryBuilder().build(is); 19 } catch (IOException e) { 20 e.printStackTrace(); 21 } 22 } 23 24 public static SqlSession getSession() { 25 SqlSession session = null; 26 if (factory != null) { 27 // true表示开启自动提交 28 // session = factory.openSession(true); 29 session = factory.openSession(); 30 } 31 return session; 32 } 33 }
1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/java505 3 jdbc.username=root 4 jdbc.password=root
1 # Set root category priority to INFO and its only appender to CONSOLE. 2 log4j.rootCategory=ERROR, CONSOLE 3 # log4j.rootCategory=DEBUG, CONSOLE, LOGFILE 4 5 # 单独设置SQL语句的输出级别为DEBUG级别 6 log4j.logger.com.bjsxt.mapper=DEBUG 7 8 # CONSOLE is set to be a ConsoleAppender using a PatternLayout. 9 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 10 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 11 log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n 12 13 # LOGFILE is set to be a File appender using a PatternLayout. 14 log4j.appender.LOGFILE=org.apache.log4j.FileAppender 15 log4j.appender.LOGFILE.File=d:/test.log 16 log4j.appender.LOGFILE.Append=true 17 log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout 18 log4j.appender.LOGFILE.layout.ConversionPattern=- %m %l%n
多表关联查询_resultMap_集合对象_关联方式实现
1 package com.bjsxt.mapper; 2 3 import java.util.List; 4 5 import com.bjsxt.pojo.Clazz; 6 7 public interface ClazzMapper { 8 9 List<Clazz> selAll(); 10 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.bjsxt.mapper.ClazzMapper"> 6 <resultMap type="clazz" id="cmap"> 7 <id property="id" column="cid" /> 8 <result property="name" column="cname" /> 9 <result property="room" column="room" /> 10 <collection property="stus" javaType="list" ofType="student"> 11 <id property="id" column="sid" /> 12 <result property="name" column="sname" /> 13 <result property="age" column="age" /> 14 <result property="gender" column="gender" /> 15 <result property="cid" column="cid" /> 16 </collection> 17 </resultMap> 18 <select id="selAll" resultMap="cmap"> 19 select c.id cid, c.name cname, c.room, s.id sid, s.name sname, s.age, s.gender 20 from t_student s 21 right join t_class c 22 on s.cid=c.id 23 </select> 24 </mapper>
1 package com.bjsxt.pojo; 2 3 import java.io.Serializable; 4 import java.util.List; 5 6 public class Clazz implements Serializable { 7 8 private int id; 9 private String name; 10 private String room; 11 private List<Student> stus; 12 13 public Clazz() { 14 super(); 15 } 16 17 public int getId() { 18 return id; 19 } 20 21 public void setId(int id) { 22 this.id = id; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public String getRoom() { 34 return room; 35 } 36 37 public void setRoom(String room) { 38 this.room = room; 39 } 40 41 public List<Student> getStus() { 42 return stus; 43 } 44 45 public void setStus(List<Student> stus) { 46 this.stus = stus; 47 } 48 49 @Override 50 public int hashCode() { 51 final int prime = 31; 52 int result = 1; 53 result = prime * result + id; 54 result = prime * result + ((name == null) ? 0 : name.hashCode()); 55 result = prime * result + ((room == null) ? 0 : room.hashCode()); 56 result = prime * result + ((stus == null) ? 0 : stus.hashCode()); 57 return result; 58 } 59 60 @Override 61 public boolean equals(Object obj) { 62 if (this == obj) 63 return true; 64 if (obj == null) 65 return false; 66 if (getClass() != obj.getClass()) 67 return false; 68 Clazz other = (Clazz) obj; 69 if (id != other.id) 70 return false; 71 if (name == null) { 72 if (other.name != null) 73 return false; 74 } else if (!name.equals(other.name)) 75 return false; 76 if (room == null) { 77 if (other.room != null) 78 return false; 79 } else if (!room.equals(other.room)) 80 return false; 81 if (stus == null) { 82 if (other.stus != null) 83 return false; 84 } else if (!stus.equals(other.stus)) 85 return false; 86 return true; 87 } 88 89 @Override 90 public String toString() { 91 return "Clazz [id=" + id + ", name=" + name + ", room=" + room + ", stus=" + stus + "]"; 92 } 93 94 }
1 package com.bjsxt.pojo; 2 3 import java.io.Serializable; 4 5 public class Student implements Serializable { 6 7 private int id; 8 private String name; 9 private int age; 10 private String gender; 11 private int cid; 12 13 public Student() { 14 super(); 15 } 16 17 public int getId() { 18 return id; 19 } 20 21 public void setId(int id) { 22 this.id = id; 23 } 24 25 public String getName() { 26 return name; 27 } 28 29 public void setName(String name) { 30 this.name = name; 31 } 32 33 public int getAge() { 34 return age; 35 } 36 37 public void setAge(int age) { 38 this.age = age; 39 } 40 41 public String getGender() { 42 return gender; 43 } 44 45 public void setGender(String gender) { 46 this.gender = gender; 47 } 48 49 public int getCid() { 50 return cid; 51 } 52 53 public void setCid(int cid) { 54 this.cid = cid; 55 } 56 57 @Override 58 public int hashCode() { 59 final int prime = 31; 60 int result = 1; 61 result = prime * result + age; 62 result = prime * result + cid; 63 result = prime * result + ((gender == null) ? 0 : gender.hashCode()); 64 result = prime * result + id; 65 result = prime * result + ((name == null) ? 0 : name.hashCode()); 66 return result; 67 } 68 69 @Override 70 public boolean equals(Object obj) { 71 if (this == obj) 72 return true; 73 if (obj == null) 74 return false; 75 if (getClass() != obj.getClass()) 76 return false; 77 Student other = (Student) obj; 78 if (age != other.age) 79 return false; 80 if (cid != other.cid) 81 return false; 82 if (gender == null) { 83 if (other.gender != null) 84 return false; 85 } else if (!gender.equals(other.gender)) 86 return false; 87 if (id != other.id) 88 return false; 89 if (name == null) { 90 if (other.name != null) 91 return false; 92 } else if (!name.equals(other.name)) 93 return false; 94 return true; 95 } 96 97 @Override 98 public String toString() { 99 return "Student [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + ", cid=" + cid + "]"; 100 } 101 102 }
1 package com.bjsxt.service; 2 3 import java.util.List; 4 5 import com.bjsxt.pojo.Clazz; 6 7 public interface ClazzService { 8 9 List<Clazz> selAll(); 10 }
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; } }
1 package com.bjsxt.test; 2 3 import java.util.List; 4 5 import com.bjsxt.pojo.Clazz; 6 import com.bjsxt.service.ClazzService; 7 import com.bjsxt.service.impl.ClazzServiceImpl; 8 9 public class TestQuery { 10 11 public static void main(String[] args) { 12 ClazzService cs = new ClazzServiceImpl(); 13 List<Clazz> list = cs.selAll(); 14 for (Clazz clazz : list) { 15 System.out.println(clazz); 16 } 17 } 18 19 }
1 package com.bjsxt.util; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 11 public class MyBatisUtil { 12 13 private static SqlSessionFactory factory = null; 14 15 static { 16 try { 17 InputStream is = Resources.getResourceAsStream("mybatis-cfg.xml"); 18 factory = new SqlSessionFactoryBuilder().build(is); 19 } catch (IOException e) { 20 e.printStackTrace(); 21 } 22 } 23 24 public static SqlSession getSession() { 25 SqlSession session = null; 26 if (factory != null) { 27 // true表示开启自动提交 28 // session = factory.openSession(true); 29 session = factory.openSession(); 30 } 31 return session; 32 } 33 }
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
个人学习笔记,记录日常学习,便于查阅及加深,仅为方便个人使用。