數據庫關係模型中有一對一,一對多,關係,
首先創建以下表 繼續用最經典的學生管理系統html
模型圖如圖所示
學生表爲中心
學生證表和其爲一對一關係
學生表和課程表爲一對多java
由於上圖中,學生和學生關係爲一對一關係.
因爲是數據庫的一對一關係,那麼對應的bean中,也應該是一對一關係
舉栗子的表爲學生表和學生證表
其中學生證表的中的學生編號的外鍵爲學生表的主鍵
那麼在學生表的POJO對象中,應該有學生證表的POJO的映射.即sql
package com.ming.MyBatis.POJO; /** * @author ming */ public class Student { private int uid; private String studentName; private String gender; private String studentIdNumber; private String remarks; private StudentCard studentCard; public void setStudentCard(StudentCard studentCard) { this.studentCard = studentCard; } public StudentCard getStudentCard() { return studentCard; } public void setUid(int uid) { this.uid = uid; } public void setStudentName(String studentName) { this.studentName = studentName; } public void setGender(String gender) { this.gender = gender; } public void setStudentIdNumber(String studentIdNumber) { this.studentIdNumber = studentIdNumber; } public void setRemarks(String remarks) { this.remarks = remarks; } public int getUid() { return uid; } public String getStudentName() { return studentName; } public String getGender() { return gender; } public String getStudentIdNumber() { return studentIdNumber; } public String getRemarks() { return remarks; } }
對於card的POJO以下所示數據庫
package com.ming.MyBatis.POJO; import java.util.Date; /** * @author ming */ public class StudentCard { private int uid; private String studentNumber; private String birthplace; private Date dateOfIssue; private Date endDate; private String remarks; public void setUid(int uid) { this.uid = uid; } public void setStudentNumber(String studentNumber) { this.studentNumber = studentNumber; } public void setBirthplace(String birthplace) { this.birthplace = birthplace; } public void setDateOfIssue(Date dateOfIssue) { this.dateOfIssue = dateOfIssue; } public void setEndDate(Date endDate) { this.endDate = endDate; } public void setRemarks(String remarks) { this.remarks = remarks; } public int getUid() { return uid; } public String getStudentNumber() { return studentNumber; } public String getBirthplace() { return birthplace; } public Date getDateOfIssue() { return dateOfIssue; } public Date getEndDate() { return endDate; } public String getRemarks() { return remarks; } }
先增長StudentCard的配置apache
<resultMap id="studentSelfCardMap" type="com.ming.MyBatis.POJO.StudentCard"> <id column="uid" property="uid"/> <result column="student_number" property="studentNumber"/> <result column="birthplace" property="birthplace" /> <result column="date_of_issue" property="dateOfIssue" jdbcType="DATE" javaType="java.util.Date"/> <result column="end_date" property="endDate" jdbcType="DATE" javaType="java.util.Date"/> <result column="remarks" property="remarks" /> </resultMap> <select id="findStudentSelfCardByStudentId" parameterType="int" resultMap="studentSelfCardMap"> SELECT student_card.uid, student_card.student_number, student_card.remarks, student_card.end_date, student_card.date_of_issue, student_card.birthplace FROM student_card WHERE student_card.uid = #{studentId}; </select>
再增長Student的映射session
<resultMap id="studentMap" type="com.ming.MyBatis.POJO.Student"> <id column="uid" property="uid"/> <result column="student_name" property="studentName"/> <result column="gender" property="gender"/> <result column="student_id_number" property="studentIdNumber"/> <result column="remarks" property="remarks"/> <!--將會調用接口表明的SQL 進行執行查詢 --> <association property="studentCard" column="uid" select="com.ming.MyBatis.RoleMapper.findStudentSelfCardByStudentId"/> </resultMap> <select id="getStudent" parameterType="int" resultMap="studentMap"> SELECT student.uid, student.gender, student.remarks, student.student_id_number, student.student_name FROM student WHERE student.uid = 1; </select>
能夠看到當查詢到studentCard的時候,將會調用com.ming.MyBatis.RoleMapper.findStudentSelfCardByStudentId sql語句的執行app
對於數據展現層來講性能
<%@ page import="org.apache.ibatis.session.SqlSession" %> <%@ page import="com.ming.Util.SqlSessionFactoryUtil" %> <%@ page import="com.ming.MyBatis.RoleMapper" %> <%@ page import="java.util.List" %> <%@ page import="java.util.Iterator" %> <%@ page import="com.ming.MyBatis.POJO.Student" %> <html> <body> <h2>Hello World!</h2> <% long startTime = System.currentTimeMillis(); //獲取開始時間 SqlSession sqlSession = null; List<Student> students = null; for(int i = 0; i < 10; i++) { try { sqlSession = SqlSessionFactoryUtil.openSqlSesion(); RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class); students = roleMapper.getStudent(1); sqlSession.commit(); } catch (Exception e) { e.printStackTrace(); sqlSession.rollback(); } finally { if (sqlSession != null) { sqlSession.close(); } } } long endTime = System.currentTimeMillis(); //獲取結束時間 %> <% Iterator iterator = students.iterator(); while(iterator.hasNext()){ %> <%=((Student)iterator.next()).getStudentCard().getEndDate()%> <% } %> </body> </html>
這個和上方同理,跳過ui
根據學生信息去關聯男性或者女性的健康指標this
兩個類只須要繼承同一個類,使用case語句便可.
跳過
在使用級聯的時候,會形成多條sql語句的執行,此時,使用延遲加載
一開始不使用sql語句,只有當使用了sql纔去發送數據
在setting這裏設置,能夠下降sql語句的執行問題
<settings> <setting name="lazyLoadingEnabled" value="true"/> </settings>
通常狀況下是層級加載,取消層級加載,使用aggressiveLazyLoading值設置爲false便可
寫原生sql 這個很簡單,跳過