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"/>

    typeAliases>

第六节:mappers

引入映射文件

第七节:配置Log4j日志

使用xml配置SQL映射器

第一节 insert映射语句

第二节 update映射语句

第三节 delete映射语句

第四节 update映射语句

常见问题:

写查询所有数据时,无法找到类

解决方法: 看自己的映射文件结果类型时ResultType是否应该改为自定义类型ResultMap;类名是否拼写错误;写SQL语句时引用id是否错误;

MyBatis关系映射

第一节 一对一关系实现

一个学生类,一个地址类,一个学生对应一个地址。

四种写法,推荐第四种,复用性较高。

StudentDao.xml



<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>
    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>
    
	
	<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



<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>

第二节 一对多关系实现

一个学生类,一个年级类,一个地址类,一个年级对应多个学生。

实体类

实体类中都省略了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



<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



<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>
    
	
	<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



<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>

多对多实现

使用两个一对多实现

你可能感兴趣的:(MyBatis)