zoukankan      html  css  js  c++  java
  • Mybatis的执行过程

    1、Mybatis的作用

    Mybatis的主要作用可以用下面的一段代码解释

    [java] view plain copy
     
    1. Class.forName("com.mysql.jdbc.Driver");  
    2. Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");  
    3. String sql = "select * from tab_user where id= ?";  
    4. // prepare sql  
    5. PreparedStatement pstmt = connection.prepareStatement(sql);  
    6. // set parameter  
    7. pstmt.setInt(1, 18);  
    8. ResultSet rs = pstmt.executeQuery();  
    9. User user = null;  
    10. // extract resultset  
    11. while (rs.next()) {  
    12.     user = new User(rs.getInt("id"), rs.getString("name"));  
    13.     System.out.println(user);  
    14. }  
    15. // release resource  
    16. rs.close();  
    17. pstmt.close();  
    18. connection.close();  

    2、Mybatis的demo

    Mysql数据库创建表tab_user

    [java] view plain copy
     
    1. CREATE TABLE `tab_user` (  
    2.   `id` int(11) NOT NULL AUTO_INCREMENT,  
    3.   `name` varchar(100) NOT NULL,  
    4.   `birthday` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  
    5.   PRIMARY KEY (`id`)  
    6. );  
    7. INSERT INTO `tab_user` VALUES ('18', 'jack', '2017-01-24 22:15:03');  

    新建demo工程在classpath下新建config/mybatis-config.xml配置文件和config/mapper/UserMapper.xml映射文件

    mybatis-config.xml文件内容

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8" ?>    
    2. <!DOCTYPE configuration    
    3.   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"    
    4.   "http://mybatis.org/dtd/mybatis-3-config.dtd">  
    5.   
    6. <configuration>  
    7.   
    8.     <environments default="development">  
    9.         <environment id="development">  
    10.             <transactionManager type="JDBC" />  
    11.             <dataSource type="POOLED">  
    12.                 <property name="driver" value="com.mysql.jdbc.Driver" />  
    13.                 <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />  
    14.                 <property name="username" value="root" />  
    15.                 <property name="password" value="root" />  
    16.             </dataSource>  
    17.         </environment>  
    18.     </environments>  
    19.   
    20.     <mappers>  
    21.         <mapper resource="config/mapper/UserMapper.xml" />  
    22.     </mappers>  
    23.   
    24. </configuration>    

    UserMapper.xml文件内容

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8" ?>    
    2. <!DOCTYPE mapper    
    3.   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    
    4.   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
    5.   
    6. <mapper namespace="UserMapper">  
    7.   
    8.     <select id="selectUser" parameterType="int" resultType="com.fit.bean.User">  
    9.         select * from tab_user where id = #{id}  
    10.     </select>  
    11.   
    12. </mapper>    

    Java代码(引入依赖jar包:mysql-connector.jar和mybatis-3.3.x.jar)

    [html] view plain copy
     
    1. SqlSession sqlSession = null;  
    2. try {  
    3.     String resource = "config/mybatis-config.xml";  
    4.     InputStream inputStream = Resources.getResourceAsStream(resource);  
    5.     SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //start  
    6.     sqlSession = sqlSessionFactory.openSession();  
    7.     User user = sqlSession.selectOne("UserMapper.selectUser", 18); //query db  
    8.     System.out.println(user);  
    9.     user = sqlSession.selectOne("UserMapper.selectUser", 18);  
    10.     System.out.println(user);  
    11. } catch (Exception e) {  
    12.     e.printStackTrace();  
    13. } finally {  
    14.     if (sqlSession != null)  
    15.         sqlSession.close(); //release resource  
    16. }  



    实体类User.java

    [java] view plain copy
     
    1. import java.io.Serializable;  
    2.   
    3. public class User implements Serializable {  
    4.   
    5.     private static final long serialVersionUID = 1L;  
    6.   
    7.     private int id;  
    8.   
    9.     private String name;  
    10.   
    11.     public User() {  
    12.         super();  
    13.     }  
    14.   
    15.     public User(int id, String name) {  
    16.         super();  
    17.         this.id = id;  
    18.         this.name = name;  
    19.     }  
    20.   
    21.     public int getId() {  
    22.         return id;  
    23.     }  
    24.   
    25.     public void setId(int id) {  
    26.         this.id = id;  
    27.     }  
    28.   
    29.     public String getName() {  
    30.         return name;  
    31.     }  
    32.   
    33.     public void setName(String name) {  
    34.         this.name = name;  
    35.     }  
    36.   
    37.     @Override  
    38.     public String toString() {  
    39.         return "{id=" + id + ", name=" + name + "}";  
    40.     }  
    41.   
    42. }  

    demo工程结构

    正常情况下就可以输出一个user对象信息

    3、Mybatis执行过程分析

    3.1启动

    [html] view plain copy
     
    1. String resource = "config/mybatis-config.xml";  
    2. InputStream inputStream = Resources.getResourceAsStream(resource);  
    3. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);  

    上面的demo可以看出实现过程是先读取mybatis配置文件,通过SqlSessionFactoryBuilder基于文件输入流创建SqlSessionFactory。

    具体实现过程:

    SqlSessionFactoryBuiler的build(inputStream)最终调用了下面的方法

    [html] view plain copy
     
    1. public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {  
    2.     try {  
    3.       XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);  
    4.       return build(parser.parse());  
    5.     } catch (Exception e) {  
    6.       throw ExceptionFactory.wrapException("Error building SqlSession.", e);  
    7.     } finally {  
    8.       ErrorContext.instance().reset();  
    9.       try {  
    10.         inputStream.close();  
    11.       } catch (IOException e) {  
    12.         // Intentionally ignore. Prefer previous error.  
    13.       }  
    14.     }  
    15.   }  

    XMLConfigBuilder的parse()方法主要就是解析mybatis-config.xml,通过configuraton根节点具体解析的属性如下

    [html] view plain copy
     
    1. private void parseConfiguration(XNode root) {  
    2.     try {  
    3.       //issue #117 read properties first  
    4.       propertiesElement(root.evalNode("properties"));  
    5.       Properties settings = settingsAsProperties(root.evalNode("settings"));  
    6.       loadCustomVfs(settings);  
    7.       typeAliasesElement(root.evalNode("typeAliases"));  
    8.       pluginElement(root.evalNode("plugins"));  
    9.       objectFactoryElement(root.evalNode("objectFactory"));  
    10.       objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));  
    11.       reflectionFactoryElement(root.evalNode("reflectionFactory"));  
    12.       settingsElement(settings);  
    13.       // read it after objectFactory and objectWrapperFactory issue #631  
    14.       environmentsElement(root.evalNode("environments"));  
    15.       databaseIdProviderElement(root.evalNode("databaseIdProvider"));  
    16.       typeHandlerElement(root.evalNode("typeHandlers"));  
    17.       mapperElement(root.evalNode("mappers"));  
    18.     } catch (Exception e) {  
    19.       throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);  
    20.     }  
    21.   }  

    根据配置文件生成Configuration对象,这个对象非常重要,是全局的配置信息,其中比较重要的包括根据plugin标签配置生成用户配置的自定义插件(比如用户可以实现mysql分页插件)、全局的setting属性配置(二级缓存配置等)、mapper映射文件信息读取。

    最后用生成的configuration对象生成DefaultSqlSessionFactory对象

    [html] view plain copy
     
    1. public SqlSessionFactory build(Configuration config) {  
    2.     return new DefaultSqlSessionFactory(config);  
    3.   }  

    3.2执行增删改查

    [html] view plain copy
     
    1. sqlSession = sqlSessionFactory.openSession();  
    2. User user = sqlSession.selectOne("UserMapper.selectUser", 18);  

    通过3.1已经获取了SqlSessionFactory,通过SqlSessionFactory创建SqlSession,Mybatis对数据库的操作主要也就是由SqlSession接口定义,由DefaultSqlSession实现。

    首先看一下SqlSession接口的定义(定义了增删改查等基本数据库操作和一些事务的操作)

    //TODO:解析sql文件、根据入参赋值预编译sql、处理结果集

    3.3释放资源

    [html] view plain copy
     
      1. sqlSession.close();  
  • 相关阅读:
    Gravatar注册
    global name 'validate_on_submit' is not defined错误
    TypeError: 'bool' object is not callable g.user.is_authenticated()
    no module named flask.ext.login
    No module named migrate.versioning
    Flask-WTF form doesn't have attribute 'validate_on_submit'问题
    can not import Flask错误
    跨站点请求伪造
    sqlserver插入时发生在“xxx”处关键发生错误
    python 爬虫实列
  • 原文地址:https://www.cnblogs.com/yang82/p/8018259.html
Copyright © 2011-2022 走看看