XML映射文件介绍(实现简单的CRUD)

XML映射文件介绍(实现简单的CRUD)

关于XML映射文件,在之前也提到过,Mybatis根据映射文件,创建一个接口的动态代理。使用者可以使用这个代理对象对数据库进行操作。

标签简介

insertupdatedelete标签

分别映射插入、更新、删除语句。

id属性

用于在这个名称空间(名称空间由<mapper/>namespace属性指定,指定为接口的全限定类名),唯一标识这个元素,可以被引用(这个后面再讲)。id通常被命名为对应的方法名。

parameterType属性

用于指定传入这条语句的参数的类型。Mybatis会自动推断,所以这个属性可以不指定

select标签

映射查询语句。

它的id属性和parameterType属性的作用和上面的三个标签完全一致。

resultType属性

用于指定返回结果的类型。如果要返回一个集合,其中包含了多个javabean,则parameterType的类型为javabean的全限定类名(而不是集合类型),根据接口中方法的返回值,可以确定返回的时单个的javabean还时javabean的集合

与resultMap属性只能设置其中一个

resultMap属性

用来引用一个<resultMap/>
与resultType属性只能设置其中一个

resultMap标签

描述如何将查询到的结果集封装到javabean中。

在这里,先只做简单的介绍

id属性

该名称空间中的唯一标识,被其他标签的resultMap属性引用。

type属性

封装后得到的javabean的全限定类名。

id子标签

用于将记录中的主键映射到javabean中的一个字段。

  • column属性用于指定列名
  • property属性用于指定字段名

column指定的列于property指定的字段相对应

result子标签

用于将记录中的一列映射到javabean中的一个字段。

  • column属性用于指定列名
  • property属性用于指定字段名

column指定的列于property指定的字段相对应

id和result的用法很相似,id用来指定主键列,result用来指定列(也可以用于主键那一列)。1

关于传参的问题

对于简单的CRUD,不涉及到多个参数,所以仅作简单的说明。

使用#{参数名}获取参数,如果参数是一个对象的引用(且仅有一个参数),想要获取其中的字段,可以使用#{字段}

映射文件的配置

<?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.test.dao.ICollegeDao">

    <insert id="addCollege">
        insert into college(college_name) values (#{name})
    </insert>

    <update id="updateCollege">
        update college set college_name = #{name} where college_id = #{id}
    </update>

    <delete id="deleteCollegeById">
        delete from college where id = #{id}
    </delete>

    <select id="getCollegeById" resultMap="college">
        select * from college where college_id = #{id};
    </select>

    <select id="getAll" resultMap="college">
        select * from college
    </select>
    <resultMap id="college" type="com.test.entity.College">
        <id column="college_id" property="id"></id>
        <result column="college_name" property="name"></result>
    </resultMap>
</mapper>

问题:没有提交

    public void getCollege() throws IOException {
		//自己封装了一个获取sqlSessionFactory的方法
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ICollegeDao collegeDao = sqlSession.getMapper(ICollegeDao.class);
        College college = collegeDao.getCollegeById(1);

        collegeDao.addCollege(new College(null, "经济管理学院"));
        System.out.println(collegeDao.getAll());
    }

执行上述代码后,输出结果如下

在这里插入图片描述

查看数据表

在这里插入图片描述

问题产生原因:没有提交

解决方法:手动提交或设置sqlSession自动提交
先来介绍手动提交的方法:

    public void getCollege() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ICollegeDao collegeDao = sqlSession.getMapper(ICollegeDao.class);
        College college = collegeDao.getCollegeById(1);

        collegeDao.addCollege(new College(null, "经济管理学院"));
        System.out.println(collegeDao.getAll());

        sqlSession.commit();
        sqlSession.close();
    }

关于sqlSession要注意的地方

sqlSession是否自动提交

sqlSessionFactory.openSession()可以用来传递一个boolean参数,用来指定是否自动提交。

非自动提交(需手动提交):

SqlSession sqlSession = sqlSessionFactory.openSession(false);
/*******等价于*******/

SqlSession sqlSession = sqlSessionFactory.openSession();

自动提交:


SqlSession sqlSession = sqlSessionFactory.openSession(true);

关闭sqlSession

使用完sqlSession后,一定要关闭sqlSession.close(),因为关闭之后就不能在使用,所以不可以线程共享(如果共享,没办法保证其他线程不会关闭)。也就是说,每个事务,都需要一个“新的”sqlSession

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章