MyBatis支持多个环境,可以任意配置
mabatis支持两种类型的事务管理:JDBC和MANAGED(托管)
JDBC:应用程序负责管理数据库连接的声明周期;
MANAGED:由应用服务器负责管理数据库连接的声明周期(一般商用服务器才有此功能,如JBOSS)
用来配置数据源:类型有UNPOOLED,POOLED,JDNI
UNPOOLED:没有连接池,每次数据库操作,mabatis都会创建一个新的连接,用完后,关闭;适合小并发项目
POOLED:用来连接池
JNDI:使用应用服务器配置JNDI数据源获取数据库连接
配置属性
给类的完成限定名取别名,方便使用
<typeAliases>
<package name="com.fzhiy.entity"/>
typeAliases>
引入映射文件
写查询所有数据时,无法找到类
解决方法: 看自己的映射文件结果类型时ResultType是否应该改为自定义类型ResultMap;类名是否拼写错误;写SQL语句时引用id是否错误;
一个学生类,一个地址类,一个学生对应一个地址。
四种写法,推荐第四种,复用性较高。
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>
使用两个一对多实现