标签:iat over new pack code mybatis tun import nes
关联查询:
一对一:
a.业务扩展类
核心:用resultType指定类的属性包含多表查询的所有字段
/MyBatisProject3/src/org/myy/mapper/studentMapper.xml
<select id="queryStudentByNoWithOO" parameterType="int" resultType="StudentBusiness"> select s.*,c.* from student1 s inner join studentcard c on s.cardid=c.cardid where s.stuno=#{stuNo} </select>
/MyBatisProject3/src/org/myy/mapper/StudentMapper.java
StudentBusiness queryStudentByNoWithOO(int stuno);
/MyBatisProject3/src/org/myy/test/Test.java
// Connection - SqlSession操作Mybatis // conf.xml->reader Reader reader = Resources.getResourceAsReader("conf.xml"); // reader->sqlSession // 可以通过build的第二参数 指定数据库环境 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader, "development"); SqlSession session = sessionFactory.openSession(); StudentMapper studentMapper = session.getMapper(StudentMapper.class); StudentBusiness studentBusiness=studentMapper.queryStudentByNoWithOO(1); System.out.println(studentBusiness); session.close();
/MyBatisProject3/src/org/myy/entity/StudentBusiness.java
package org.myy.entity; public class StudentBusiness extends Student{//学生业务扩展类 private int cardId; private String cardInfo; public int getCardId() { return cardId; } public void setCardId(int cardId) { this.cardId = cardId; } public String getCardInfo() { return cardInfo; } public void setCardInfo(String cardInfo) { this.cardInfo = cardInfo; } @Override public String toString() { return super.toString()+","+this.cardId+","+this.cardInfo; } }
b.resultMap
i.通过属性成员将2个类建立起联系
/MyBatisProject3/src/org/myy/entity/Student.java
package org.myy.entity; //学生类包含 1.学生信息 2.学生证信息 public class Student { //学生信息 private int stuNo; private String stuName; private int stuAge; private String graName; private Boolean stuSex; //学生证信息 private StudentCard card; public StudentCard getCard() { return card; } public void setCard(StudentCard card) { this.card = card; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } private Address address;//家庭、学校 public int getStuNo() { return stuNo; } public void setStuNo(int stuNo) { this.stuNo = stuNo; } public Boolean getStuSex() { return stuSex; } public void setStuSex(Boolean stuSex) { this.stuSex = stuSex; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public int getStuAge() { return stuAge; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } public String getGraName() { return graName; } public void setGraName(String graName) { this.graName = graName; } public Student() { super(); } public Student(int stuNo, String stuName, int stuAge, String graName) { super(); this.stuNo = stuNo; this.stuName = stuName; this.stuAge = stuAge; this.graName = graName; } public Student(int stuNo, String stuName, int stuAge, String graName, Boolean stuSex) { super(); this.stuNo = stuNo; this.stuName = stuName; this.stuAge = stuAge; this.graName = graName; this.stuSex = stuSex; } @Override public String toString() { return this.stuNo+"-"+this.stuName+"-"+this.stuAge+"-"+this.graName+"-性别:"+this.stuSex +","+this.card.getCardId()+"-"+this.card.getCardInfo(); } }
ii.
/MyBatisProject3/src/org/myy/mapper/studentMapper.xml
<!--利用resultMap实现一对一 --> <select id="queryStudentByNoWithOO2" parameterType="int" resultMap="student_card_map"> select s.*,c.* from student1 s inner join studentcard c on s.cardid=c.cardid where s.stuno=#{stuNo} </select> <resultMap type="student" id="student_card_map"> <id property="stuNo" column="stuNo"/> <result property="stuName" column="stuName"/> <result property="stuAge" column="stuAge"/> <result property="graName" column="graname" /> <result property="stuSex" column="stusex" javaType="boolean" jdbcType="INTEGER" /> <!--一对一时,对象成员使用association映射 ; javaType指定该属性的类型 --> <association property="card" javaType="StudentCard"> <id property="cardId" column="cardId"/> <result property="cardInfo" column="cardInfo"/> </association> </resultMap>
/MyBatisProject3/src/org/myy/mapper/StudentMapper.java
Student queryStudentByNoWithOO2(int stuno);
/MyBatisProject3/src/org/myy/test/Test.java
// Connection - SqlSession操作Mybatis // conf.xml->reader Reader reader = Resources.getResourceAsReader("conf.xml"); // reader->sqlSession // 可以通过build的第二参数 指定数据库环境 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader, "development"); SqlSession session = sessionFactory.openSession(); StudentMapper studentMapper = session.getMapper(StudentMapper.class); Student student = studentMapper.queryStudentByNoWithOO2(2); System.out.println(student); session.close();
一对一:association
一对多:collection
一对多(多对一,多对多的本质就是一对多的变化)
表:student studentclass
类:Student StudentClass
/MyBatisProject3/src/org/myy/entity/StudentClass.java
package org.myy.entity; import java.util.List; public class StudentClass { private int classId; private String className; //增加学生属性(通过该字段让Student类和StudentClass建立起关联) List<Student> students; public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } public int getClassId() { return classId; } public void setClassId(int classId) { this.classId = classId; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } }
/MyBatisProject3/src/org/myy/mapper/studentMapper.xml
<!--一对多 --> <select id="queryClassAndStudents" parameterType="int" resultMap="class_student_map"> <!--查询某一个班级的班级信息和哪个班的所有学生的信息 --> select c.*,s.* from student1 s inner join studentclass c on c.classid=s.classid where c.classid=#{classId} </select> <!--类和表的对应关系 --> <resultMap type="studentclass" id="class_student_map"> <!--因为type的主类是班级,因此先配置班级的信息 --> <id property="classId" column="classId"/> <result property="className" column="className"/> <!--配置成员属性,一对多;属性类型:javaType,属性的元素类型odType --> <collection property="students" ofType="student"> <id property="stuNo" column="stuNo"/> <result property="stuName" column="stuName"/> <result property="stuAge" column="stuAge"/> </collection> </resultMap>
/MyBatisProject3/src/org/myy/mapper/StudentMapper.java
StudentClass queryClassAndStudents(int classId);
/MyBatisProject3/src/org/myy/test/Test.java
// Connection - SqlSession操作Mybatis // conf.xml->reader Reader reader = Resources.getResourceAsReader("conf.xml"); // reader->sqlSession // 可以通过build的第二参数 指定数据库环境 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader, "development"); SqlSession session = sessionFactory.openSession(); StudentMapper studentMapper = session.getMapper(StudentMapper.class); //班级 StudentClass studentClass = studentMapper.queryClassAndStudents(1); System.out.println(studentClass.getClassId()+","+studentClass.getClassName()); //班级对应的学生 List<Student> students=studentClass.getStudents(); for(Student student:students) { System.out.println(student.getStuNo()+","+student.getStuName()+","+student.getStuAge()); } session.close();
标签:iat over new pack code mybatis tun import nes
原文地址:https://www.cnblogs.com/mayouyou/p/13226673.html