I don't know jdbc, what about Mybatis source parsing?

Keywords: Java Database SQL JDBC MySQL

This article is mainly intended to show the use of jdbc to make it easier to read MyBatis source code and to warm up for source analysis.

Many of the key information can be found in the MyBatis source code. This article does not analyze the MyBatis source code.

Because the MyBatis source code is a huge and complex project, it can't be said in just a few words in a while.

jdbc Demo: 

 1 public static void main(String[] args) throws Exception {
 2        test1();
 3     }
 4 
 5     public static void test1() throws Exception {
 6         ResultSet rs=null;
 7         PreparedStatement pst=null;
 8         Connection conn=null;
 9         try {
10             String sql="select id as sid,name,age,sex from user where id!=? order by id asc ";
11             //Register Driver 1 Use Reflection Load Database Driver
12             //Class.forName("com.mysql.cj.jdbc.Driver");
13             //Registration Driver 2 is also available new MySql Of Driver Class-style registration driver
14             //new Driver();
15             //Register Drive Mode 3 Use Reflection Mode new An anonymous object
16             Driver.class.getConstructor().newInstance();
17             conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/gys?serverTimezone=UTC","root","gys");
18             System.out.println("======Before Query========");
19             DatabaseMetaData dmd=conn.getMetaData();
20             System.out.println("Database Name:"+dmd.getDatabaseProductName());
21             System.out.println("Database Version:"+dmd.getDatabaseProductVersion());
22             System.out.println("Supports transactions:"+dmd.supportsTransactions());
23             //DriverManager.setLogWriter();
24             pst=conn.prepareStatement(sql);
25             //to sql Statement?assignment
26             pst.setString(1,"5");
27 
28             System.out.println("======When querying========");
29             ParameterMetaData pmd=pst.getParameterMetaData();
30             System.out.println("Number of parameters" + pmd.getParameterCount());
31             //1 Represents an entry, 2 an exit, and 3 an exit (mainly for stored procedures)
32             System.out.println("First parameter mode:"+pmd.getParameterMode(1));
33 
34             //Database operation mode 1
35             boolean res=pst.execute();
36             rs=null;
37             if(res){
38                 rs=pst.getResultSet();
39             }else{
40                 System.out.println("Number of rows returned affected:"+pst.getUpdateCount());
41                 return;
42             }
43 
44             //Database Operation Mode 2
45             //int resCount= pst.executeUpdate();
46             //Database Operation Mode 3
47             //rs= pst.executeQuery();
48 
49             System.out.println("======After Query========");
50             ResultSetMetaData rsm=rs.getMetaData();
51             System.out.println("Number of columns:"+rsm.getColumnCount());
52             System.out.println("Column 1 Alias:"+rsm.getColumnLabel(1));
53             System.out.println("Column 1 field name:"+rsm.getColumnName(1));
54 
55             User user=null;
56             while(rs.next()){
57                 user=new User();
58                 user.setId(rs.getLong("sid"));
59                 user.setName(rs.getString("name"));
60                 user.setAge(rs.getInt("age"));
61             }
62             System.out.println("Query data:"+user.toString());
63         }catch (Exception e){
64             e.printStackTrace();
65         }finally {
66             //6.Release Resources
67             if(rs!=null){
68                 rs.close();
69             }
70             if(pst!=null){
71                 pst.close();
72             }
73             if(conn!=null){
74                 conn.close();
75             }
76         }
77 
78     }

Execution results:

 

 

demo parsing:

Driven Registration (lines 12, 14, 16):

Don't be confused by this tall noun; it's simply loading the Driver class from the MySql package into the virtual machine and performing the appropriate actions.

So what is the action?

MySql Driver.java See Source:

 1 package com.mysql.cj.jdbc;
 2 
 3 import java.sql.SQLException;
 4 
 5 public class Driver extends NonRegisteringDriver implements java.sql.Driver {
 6    
 7     static {
 8         try {
 9             java.sql.DriverManager.registerDriver(new Driver());
10         } catch (SQLException E) {
11             throw new RuntimeException("Can't register driver!");
12         }
13     }
14 
15     
16     public Driver() throws SQLException {
17         // Required for Class.forName().newInstance()
18     }
19 }

The static block executes when the class is loaded, executing the method of registering the service with DriverManager.registerDriver() in jdk.

Driver also inherits what java.sql.Driver says everywhere on the web: jdbc provides interfaces, database vendors provide implementations.

From the above driver source analysis line 12,14,16 three ways, which register writing is better?

Line 12 loads the driver class directly into the virtual machine through reflection, but does not create any objects;

Line 14 loads the driver class with a new anonymous object and requires a memory space in the heap memory.

Line 16 also creates an anonymous object, which is simply lost slightly slower than new by reflection, requiring the virtual machine to open up memory in the heap as well.

Because we don't need this anonymous object, it will be recycled by the virtual machine after a while.

The analysis or the writing on line 12 is the most perfect way to register a service, and in fact it is very slight in terms of performance and time.

 

Three ways to operate a database:

int executeUpdate(): Performs sql write operations, such as insert,update,delete; returns the number of rows affected.Delete table returns 0 for database creation
ResultSet executeQuery(): Method of executing select query operation.Return Query Result Set
boolean execute(); contains both operations.Returns true after the query, indicating that pst.getResultSet() has a value, otherwise it has no value.


Posted by juma929 on Fri, 10 Apr 2020 21:13:28 -0700