互联网开发手记SpringFrameworkJava学习笔记

mybatis从使用到了解(六)_mybatis的XML映射文件

2017-01-05  本文已影响356人  YONGSSU的技术站点

Mapper XML文件介绍

Mybatis的强大只处在于它提供的强大的映射语句,和具有相同功能的JDBC代码对比,能大大减少重复代码。

insert

<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyColumn="id">
    insert into student (student_name,student_age,student_phone,interests) values
    (#{student_name},#{student_age},#{student_phone},#{interests,javaType=[Ljava.lang.String;,jdbcType=VARCHAR})
</insert>

delete

<delete id="deleteById" parameterType="int">
   delete * from student where student_id = #{id}
</delete>

update

<update id = "updateById">
    update student set
        student_name = #{student_name},
        student_age = #{student_age},
        student_phone = #{student_phone},
        interests = #{interests}
    where student_id = #{student_id}
</update>

select

<select id="selectStudentById" parameterType="int" resultType="Student">
    <![CDATA[
        select * from student where student_id = #{id}
    ]]>
</select>

sql

这个元素可以被用来定义可重用的SQL代码段,可以包含在其他语句中。可以被静态的参数化。

<sql id = "studentSql">
    student_id,student_name,student_age,student_phone,interests,teacher_id
</sql>
<select id="findById" parameterType="int" resultMap="studentResult">
    select
    <include refid="studentSql"/>
    from student where student_id = #{id}
</select>

resultMap

结果映射集,将数据表列数据映射成java对象,下面将提供一个两表级联查询的例子作为参考。

数据格式.png
public class Teacher {
    private Integer teacherId;
    private String teacherName;
    private String teacherAge;
    private String teacherPhone;
    private List<Student> students;
    public Integer getTeacherId() {
        return teacherId;
    }
    public void setTeacherId(Integer teacherId) {
        this.teacherId = teacherId;
    }
    public String getTeacherName() {
        return teacherName;
    }
    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }
    public String getTeacherAge() {
        return teacherAge;
    }
    public void setTeacherAge(String teacherAge) {
        this.teacherAge = teacherAge;
    }
    public String getTeacherPhone() {
        return teacherPhone;
    }
    public void setTeacherPhone(String teacherPhone) {
        this.teacherPhone = teacherPhone;
    }
    public List<Student> getStudents() {
        return students;
    }
    public void setStudents(List<Student> students) {
        this.students = students;
    }
    @Override
    public String toString() {
        return "Teacher{" +
                "teacherId=" + teacherId +
                ", teacherName='" + teacherName + '\'' +
                ", teacherAge='" + teacherAge + '\'' +
                ", teacherPhone='" + teacherPhone + '\'' +
                ", students=" + students +
                '}';
    }
}
 <resultMap id="teacherResult" type="com.yongssu.mybatis.demo1.Teacher">
    <id column="teacher_id" property="teacherId"/>
    <result column="teacher_name" property="teacherName"/>
    <result column="teacher_age" property="teacherAge"/>
    <result column="teacher_phone" property="teacherPhone"/>
    <association property="students" javaType="com.yongssu.mybatis.demo1.Student">
        <id column="student_id" property="studentId"/>
        <result column="student_name" property="studentName"/>
        <result column="student_age" property="studentAge"/>
        <result column="student_phone" property="studentPhone"/>
        <result column="interests" property="interests" javaType="[Ljava.lang.String;"/>
    </association>
</resultMap>

用association表示teacher和学生是1对1的关系,在Teacher中的Student只有一个学生,应该声明为Student student。

<resultMap id="studentResult" type="Student">
    <id column="student_id" property="studentId"/>
    <result column="student_name" property="studentName"/>
    <result column="student_age" property="studentAge"/>
    <result column="student_phone" property="studentPhone"/>
    <result column="interests" property="interests" javaType="[Ljava.lang.String;"/>
</resultMap>
<resultMap id="teacherResult" type="com.yongssu.mybatis.demo1.Teacher">
    <id column="teacher_id" property="teacherId"/>
    <result column="teacher_name" property="teacherName"/>
    <result column="teacher_age" property="teacherAge"/>
    <result column="teacher_phone" property="teacherPhone"/>
    <collection property="students" ofType="Student" resultMap="studentResult"/>
</resultMap>

用collection表示老师和学生是1对N的关系,在Teacher类中学生是一个列表。

<select id="selectTeacher" parameterType="int" resultMap="teacherResult">
    select * from teacher
    left join student on student.teacher_id = teacher.teacher_id
    where teacher.teacher_id = #{id}
</select>

cache

mybatis包含了了一个非常强大的缓存特性,可以非常方便的配置和定制。在默认的情况下是没有开启缓存的。要开启缓存,需要在SQL映射文件中添加一行

<cache/>

缓存的效果如下

<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>

这个更高级的配置创建了一个 FIFO 缓存,并每隔 60 秒刷新,存数结果对象或列表的 512 个引用,而且返回的对象被认为是只读的,因此在不同线程中的调用者之间修改它们会 导致冲突。

上一篇 下一篇

猜你喜欢

热点阅读