Mybatis的SQLSession,配置文件和映射文件解析

    今天开始正式的学习了伟大的SSM框架,首先下手的位置是"M",即Mybatis,以下内容仅是个人在学习中的笔记和心得,如有错,纯属正常,还望大牛们加以改正.废话不多说,直接上内容.

    注意:在一下两个配置文件中,'|' 表示的是或 , '*'表示的是>=0 ,  "+"表示的是>=1,  "?"表示的是<=1

    首先,我们来谈以下这个MyBatis的配置文件,

    mybatis.xml          主配置文件,作用:主要是保存连接数据库的四大要素(驱动类,URL,用户名,密码),

                                 位置:在Dao接口所在的包

                               命名:,这个文件的文件名是根据作者的个人喜好进行定义的,当然我为了方便,就把这个文件的命名和Jar包中    的相应 的dtd约束文件保持了一致,

    mapper.xml         映射文件 作用:主要是用来封装开发者这SQL语句,

                                位置:位于src目录下 

                                命名:这个文件的命名也是很随意的,和主配置文件的命名规则一样

映射文件会在主配置文件中注册,当我们的框架把主配置文件从内存中加载到内存的时候,会同时吧映射文件加载到内存中.

约束文件是如何对主配置文件和映射文件进行约束呢?

主要是在主配置文件和映射文件添加响应的dtd文件的配置文件头,首先应该在Jar包中拷贝到相应的dtd文件的全名,然后我们可以去mybatis的官方文档上进行搜索,(官网上下载的mybatis的框架都会包含官方文档,另外搜索的快捷键是ctrl+f).

首先是映射文件的配置文件头,

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

在mapper.xml文件中,我们可以通过以下这种方式进行SQL语句的编写:

<mapper namespace="xxx">
 <insert id="insertStudent" parameterType="com.abc.bean.Student">
  insert into student (name,age,score) values(#{name},#{age},#{score})
 </insert>
</mapper>

对于上面的分析如下:id是整个<Mapper>标签的唯一标识,由于<Mapper>标签可以是多个,当我们在主配置文件中通过映射文件操作数据库的时候,我们可以通过指定不同的id,从而使用不同的标签,达到我们操作数据库的目的.namespace的值随意些,但是不能删除,否则程序在运行的时候会报错,parameterType,表示的是接受参数的类型,如果接受的参数的类型是对象,那么SQL语句中的对象的属性,就应该是"#{}"表示,括号中可以直接写对象的属性名称.

下面是主配置文件饿配置头信息,我们也要进行解读

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
 <!-- environments标签的作用就是配置当前的运行环境 -->
 <environments default="development">
  <environment id="development">
   <transactionManager type="JDBC" />
   
   <!-- DataSource是说明了当前的数据源,类型为pooled.常见的数据源有c3p0连接池 -->
   <dataSource type="POOLED">
    <!-- 连接数据所需要的四大参数 -->

    <property name="driver" value="com.mysql.jdbc.Driver" />
    注意:当我们访问的数据库在我们的本机上时,且端口号保持3306,我们便可以不写localhost:3306,或者127.0.0.1

    <property name="url" value="jdbc:mysql:///test" />
    <property name="username" value="root" />
    <property name="password" value="123" />
   </dataSource>
  </environment>
 </environments>

<!-- <mapper>标签的作用就是为了注册映射文件,所以这个值应该写的是映射文件的全名称 -->
 <mappers>
  <mapper resource="com/abc/dao/mapper.xml" />
 </mappers>
</configuration>


=============================================

下面,我们来说一下,这个SQLSession的来历,首先我们先来阅读一下源码,源码如下:

The primary Java interface for working with MyBatis is the SqlSession. Through this interface you
can execute commands, get mappers and manage transactions. We'll talk more about SqlSession
itself shortly, but first we have to learn how to acquire an instance of SqlSession. SqlSessions
are created by a SqlSessionFactory instance. The SqlSessionFactory contains methods for
creating instances of SqlSessions all different ways. The SqlSessionFactory itself is created by the
SqlSessionFactoryBuilder that can create the SqlSessonFactory from XML, Annotations or hand
coded Java configuration.

个人翻译如下:

    实现mybatis功能的java基本接口是SQLSession,通过这个接口你可以执行命令,得到映射,并且管理事务,接下来我们多多的谈谈它,但是首先我们应该了解如何创建一个SQLSession的实例,SQLSession可以被SQLSessionFactory实例创建,这个SQLSessionFactory包括了所有的创建SQLSession实例的方法,SQLSessionFactoryBuilder可以通过XML文档,注释文件,或者手工编写的类来创建SQLSessionFactory实例.

Every MyBatis application centers around an instance of SqlSessionFactory. A SqlSessionFactory
instance can be acquired by using the SqlSessionFactoryBuilder. SqlSessionFactoryBuilder can build
a SqlSessionFactory instance from an XML configuration file, or from a custom prepared instance of
the Configuration class.
Building a SqlSessionFactory instance from an XML file is very simple. It is recommended that you
use a classpath resource for this configuration, but you could use any InputStream instance, including
one created from a literal file path or a file:// URL. MyBatis includes a utility class, called Resources,
that contains a number of methods that make it simpler to load resources from the classpath and other
locations

个人翻译如下:

    每一个Mybatis应用集中于一个SQLSessionFactory实例,每一个SQLSessionFactory实例能通过使用SQLSessionFactoryBuilder来创建,而一个SQLSessionFactoryBuilder可以通过XML文档,注释文件,或者手工编写的类来创建SQLSessionFactory实例.创建一个SqlSessionFactory实例非常简单,他要求你使用为此配置的类资源路径,但是你可以使用任何输入流实例,包括在本地已经创建的文件路径,或者一个文件的URL,Mybatis包含了一个名字叫做Resources的静态类,这个类包含了很多使得从类路径或者其他的地方加载资源更加简单的方法.

下面我们根据源码来创建SQLSession实例,从源码中我们也可以得知,SQLSession的功能在于,执行命令,得到映射和管理事务.

package com.abc.dao;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.abc.bean.Student;
public class StudentDaoImpl implements IStudentDao {
 @Override
 public void insertStudent(Student stu) {
  SqlSession session = null;
  try {
  //创建SQLSessionFactoryBuilder对象
  SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
  
  //该方法采用的是Resources的静态方法来获取指定文件的IO流对象
  //加载主配置文件
  InputStream is = Resources.getResourceAsStream("mybatis.xml");
  
  //该方法中的参数是IO流的引用,得到factory对象
  SqlSessionFactory factory = builder.build(is);
  
  //得到SqlSesion对象
   session = factory.openSession();
  
  
  //SQLsession对象可以执行对数据库的操作的命令,得到映射,管理事务的功能
  //对于这两个参数,我要简单的说一下,第一个arg0是你要执行的映射文件中的SQL语句所在的标签的ID,
  //第二个参数是你的SQL语句所需要的对象,本SQL所需要的就是一个Student类型的数据.
  session.insert("insertStudent", stu);
  
  //提交事务
   //一定要提交,否则数据可能不会写入到数据库
    session.commit();
    
  } catch (IOException e) {
   e.printStackTrace();
  }finally {
   if(session != null) {
    //关闭SQLsession
//这个close方法的功能非常强大,当以上操作没有异常第,它便会执行SQLSession的关闭的操作,如果出现异常,它会执行回滚然后关闭的操作.
    session.close();
   }
  }
  
 }
}





猜你喜欢

转载自blog.csdn.net/dawiebazhanlang/article/details/81036492
今日推荐