MyBaties source parsing

       Find a way to view the source code really is a learning because many times people put us to the core code we have a good package can come and collect direct use very often they themselves would ask why you can get through this I think it is a method pieces of curiosity I calculated on these three parts also spent a few hours pondering but it feels worth it, I think more clearly the principles of the mybaties 

       Mybaties also learned some time today taking the time to talk about how to mybaties is why it can easily operate the database we did not have to focus on how to create a connection from a prepared statement on how it is to achieve it? We all know that to operate the database database data source must first execute the statement also have operations

How 1. Data source is used to connect to the database, he is to get a connection to the database do we know that there is a core mybatais mybatis-config.xml configuration file and this file inside there are many database connection information

Where we took a look at the source code is how to get to the data source

    org.apache.ibatis.session.SqlSessionFactoryBuilder.build(InputStream)

       org.apache.ibatis.session.SqlSessionFactoryBuilder.build(InputStream, String, Properties)

            org.apache.ibatis.builder.xml.XMLConfigBuilder.XMLConfigBuilder(InputStream, String, Properties)

                 org.apache.ibatis.builder.xml.XMLConfigBuilder.parse()

                      org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XNode)

   Finally, we can get information about this method can be connected by a data source parseConfiguration (XNode)           

. 1  <? XML Version = "1.0" encoding = "UTF-. 8" ?> 
2  <! DOCTYPE Configuration
 . 3    the PUBLIC "- // 3.0 // EN mybatis.org//DTD Config" 
 . 4    "http://mybatis.org /dtd/mybatis-3-config.dtd " > 
. 5  
. 6  < configuration > 
. 7      <-! configuration environment information, i.e. the data source information -> 
. 8      < environments default =" D1 " > 
. 9          < environment ID =" D1 " > 
10              <! - configure the transaction manager -> 
11              <transactionManager type="JDBC" /> 
12              <! - configuration database and the data source connection information -> 
13 is              < the dataSource type = "the POOLED" > 
14                  < Property name = "Driver" value = "com.mysql.jdbc.Driver"  /> 
15                  < Property name = "URL" 
16                      value = "JDBC: MySQL: // localhost: 3306 / Fresh = UTF-characterEncoding. 8?"  /> 
. 17                  < Property name = "username" value = "the root"  /> 
18 is                  <property name="password" value="root" />
19             </dataSource>
20         </environment>
21     </environments>
22     <!-- 关联隐射文件 -->
23     <mappers>
24         <mapper resource="com/newroad/dao/StudentMapper.xml" />
25     </mappers>
26 </configuration> 
27   

 

     

2. Execute statement first to understand the four following statement is how they execute it

      org.apache.ibatis.session.SqlSession. (Class<T>)

           org.apache.ibatis.session.Configuration.getMapper(Class<T>, SqlSession) 

                     org.apache.ibatis.binding.MapperRegistry.getMapper(Class<T>, SqlSession)

                           org.apache.ibatis.binding.MapperProxyFactory.newInstance(SqlSession)

                                org.apache.ibatis.binding.MapperProxy.MapperProxy(SqlSession, Class, Map)

                                        java.lang.reflect.Method.invoke(Object, Object...)

                                              org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(Method)

                                                    org.apache.ibatis.binding.MapperMethod.execute(SqlSession, Object[])

                                                              org.apache.ibatis.session.SqlSession.selectOne(String, Object)
                                                 
                         
DDL DML DQL DCL
数据定义语言 数据操纵语言 数据查询语言 数据控制语言,定义访问权限、取消访问权限,安全设置
create、drop、alter insert、update、delete select grant

3.操作  

        org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession()

           org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(ExecutorType, TransactionIsolationLevel, boolean)     
                  org.apache.ibatis.session.Configuration.newExecutor(Transaction, ExecutorType)
                     org.apache.ibatis.executor.SimpleExecutor
                         org.apache.ibatis.executor.BaseExecutor.query(MappedStatement, Object, RowBounds, ResultHandler, CacheKey, BoundSql)
                              org.apache.ibatis.mapping.SqlSource.getBoundSql(Object)
                                  org.apache.ibatis.executor.SimpleExecutor.doQuery(MappedStatement, Object, RowBounds, ResultHandler, BoundSql)
                                          org.apache.ibatis.executor.statement.StatementHandler
                                             org.apache.ibatis.executor.SimpleExecutor.prepareStatement(StatementHandler, Log)

     

    

 

Guess you like

Origin www.cnblogs.com/hengly/p/10992652.html