1. 程式人生 > >MyBatis——配置、對映器、關係對映

MyBatis——配置、對映器、關係對映

MyBatis配置

第一節:environments

MyBatis支援多個環境,可以任意配置

第二節:transactionManager

mabatis支援兩種型別的事務管理:JDBC和MANAGED(託管)

JDBC:應用程式負責管理資料庫連線的宣告週期;

MANAGED:由應用伺服器負責管理資料庫連線的宣告週期(一般商用伺服器才有此功能,如JBOSS)

第三節 dataSource

用來配置資料來源:型別有UNPOOLED,POOLED,JDNI

UNPOOLED:沒有連線池,每次資料庫操作,mabatis都會建立一個新的連線,用完後,關閉;適合小併發專案

POOLED:用來連線池

JNDI:使用應用伺服器配置JNDI資料來源獲取資料庫連線

第四節:properties

配置屬性

第五節: typeAliases

給類的完成限定名取別名,方便使用

<typeAliases> <!-- 別名 -->
        <package name="com.fzhiy.entity"/>
<!--     	<typeAlias alias="Student" type="com.fzhiy.entity.Student"/> -->
    </typeAliases>

第六節:mappers

引入對映檔案

第七節:配置Log4j日誌

使用xml配置SQL對映器

第一節 insert對映語句

第二節 update對映語句

第三節 delete對映語句

第四節 update對映語句

常見問題:

寫查詢所有資料時,無法找到類

解決方法: 看自己的對映檔案結果型別時ResultType是否應該改為自定義型別ResultMap;類名是否拼寫錯誤;寫SQL語句時引用id是否錯誤;

MyBatis關係對映

第一節 一對一關係實現

一個學生類,一個地址類,一個學生對應一個地址。

四種寫法,推薦第四種,複用性較高。

StudentDao.xml

<?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.fzhiy.dao.StudentDao"> <!-- 第一種寫法: 物件級聯 ,複用性不高--> <!-- <resultMap type="Student" id="StudentResult"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> <result property="address.id" column="addressId"/> <result property="address.sheng" column="sheng"/> <result property="address.shi" column="shi"/> <result property="address.qu" column="qu"/> </resultMap>--> <!-- 第二種寫法: Address類獨立出來 使用關聯association --> <!-- <resultMap type="Address" id="AddressResult"> <result property="id" column="id"/> <result property="sheng" column="sheng"/> <result property="shi" column="shi"/> <result property="qu" column="qu"/> </resultMap> <resultMap type="Student" id="StudentResult"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> <association property="address" resultMap="AddressResult"/> </resultMap> --> <!-- 第三種寫法 巢狀association --> <!-- <resultMap type="Student" id="StudentResult"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> <association property="address" javaType="Address"> <result property="id" column="id"/> <result property="sheng" column="sheng"/> <result property="shi" column="shi"/> <result property="qu" column="qu"/> </association> </resultMap>--> <!-- 第四種寫法 --> <resultMap type="Student" id="StudentResult"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="age" column="age"/> <association property="address" column="addressId" select="com.fzhiy.dao.AddressDao.findById"></association> </resultMap> <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer"> select * from t_student as t1,t_address as t2 where t1.addressId=t2.id and t1.id=#{id} </select> <!-- parameterTpye對應typeAlias別名 --> <insert id="insertStudent" parameterType="Student"> insert into t_student values(null,#{name},#{age}) </insert> <update id="updateStudent" parameterType="Student"> update t_student set name=#{name},age=#{age} where id=#{id} </update> <delete id="deleteStudnet" parameterType="Integer"> delete from t_student where id=#{id} </delete> <select id="findById" parameterType="Integer" resultType="Student"> select * from t_student where id=#{id} </select> <select id="find" resultMap="StudentResult"> select * from t_student </select> </mapper>

AddressDao.xml

<?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.fzhiy.dao.AddressDao">
	  
    <resultMap type="Address" id="AddressResult">
    	<result property="id" column="id"/>
        <result property="sheng" column="sheng"/>
        <result property="shi" column="shi"/>
        <result property="qu" column="qu"/>
    </resultMap>
    <!-- 注意這裡resultType是Address -->
	<select id="findById" parameterType="Integer" resultType="Address">
	    select * from t_address where id=#{id}
	</select>
	
</mapper>

第二節 一對多關係實現

一個學生類,一個年級類,一個地址類,一個年級對應多個學生。

實體類

實體類中都省略了getter和setter省略、重寫toString

Grade.java

package com.fzhiy.entity;

import java.util.List;

public class Grade {

	private Integer id;
	private String gradeName;
	private List<Student> students;
	
}

Student.java

package com.fzhiy.entity;

public class Student {
	
	private Integer id;//自動生成
	private String name;
	private Integer age;
	private Address address;
	private Grade grade;
		
}

Address.java

package com.fzhiy.entity;

public class Address {
	
	private Integer id;
	private String sheng;
	private String shi;
	private String qu;
}

對映檔案

GradeDao.xml

<?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.fzhiy.dao.GradeDao">
	  
    <resultMap type="Grade" id="GradeResult">
    	<result property="id" column="id"/>
        <result property="gradeName" column="gradeName"/>
        <collection property="students" column="id" select="com.fzhiy.dao.StudentDao.findByGradeId"></collection>
    </resultMap>
    
	<select id="findById" parameterType="Integer" resultMap="GradeResult">
	    select * from t_grade where id=#{id}
	</select>
	
</mapper>

StudentDao.xml

<?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.fzhiy.dao.StudentDao">

    <!-- 第四種寫法 -->
    <resultMap type="Student" id="StudentResult">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
      	<association property="address" column="addressId" select="com.fzhiy.dao.AddressDao.findById"></association>
        <association property="grade" column="gradeId" select="com.fzhiy.dao.GradeDao.findById"></association>
    </resultMap>
    <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
        select * from t_student as t1,t_address as t2 where t1.addressId=t2.id and t1.id=#{id}
    </select>
    
    <select id="findByGradeId" resultMap="StudentResult" parameterType="Integer">
        select * from t_student where gradeId=#{gradeId}
    </select>
    
	<!-- parameterTpye對應typeAlias別名 -->
	<insert id="insertStudent" parameterType="Student">
		insert into t_student values(null,#{name},#{age})
	</insert>
	<update id="updateStudent" parameterType="Student">
	    update t_student set name=#{name},age=#{age} where id=#{id}
	</update>
	<delete id="deleteStudnet" parameterType="Integer">
	    delete from t_student where id=#{id}
	</delete>
	<select id="findById" parameterType="Integer" resultType="Student">
	    select * from t_student where id=#{id}
	</select>
	
	<select id="find" resultMap="StudentResult">
	    select * from t_student
	</select>
</mapper>

AddressDao.xml

<?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.fzhiy.dao.AddressDao">
	  
    <resultMap type="Address" id="AddressResult">
    	<result property="id" column="id"/>
        <result property="sheng" column="sheng"/>
        <result property="shi" column="shi"/>
        <result property="qu" column="qu"/>
    </resultMap>
    
	<select id="findById" parameterType="Integer" resultType="Address">
	    select * from t_address where id=#{id}
	</select>
	
</mapper>

多對多實現

使用兩個一對多實現