On the Baidu encyclopedia to find a paragraph definition orm words:Object Relational Mappings (English:Object Relational Mapping, ShortORM, oro/rm, orO/R mapping), is a program technology that enables the conversion of data between different types of systems in an object-oriented programming language. In effect, it actually creates a "virtual object database" that can be used in a programming language. Then the ORM Framework is a set of programming models that provide object-to-database relational mapping. Now the popular mybaits, Hibernate are the framework. At the beginning of this chapter, we analyze the analysis of the source code of the two frameworks in order to further understand what ORM is.
Before we begin to analyze the source code of these currently more popular ORM frameworks, let's take a look at JDBC, which is the basis for all of these mybaits3,hibernate4 frameworks. When it comes to JDBC, let's start by looking at what classes are under the java.sql package.
Let's take a look at the complete process of JDBC accessing data.
Connection con = null; Statement stmt = null; ResultSet rs = null; try{class.forname ("Com.mysql.jdbc.Driver"); con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/database", "root", "123456"); stmt = Con.createstatement (); String sql = "SELECT * from ' table '"; rs = stmt.executequery (SQL); ResultSetMetaData RSMD = Rs.getmetadata (); int j = 0; j = Rsmd.getcolumncount (); for (int k = 0; k<j; k++) {System.out.print (Rsmd.getcatalogname (k+1)); System.out.print ("\ t"); } while (Rs.next ()) {for (int i=0;i<j;i++) {System.out.print (rs.getstring (i+1)); System.out.print ("\ t"); }}} catch (Exception E1) {System.out.println (e1.tostring ()); } finally{try {if (rs! = null) rs.close (); if (stmt! = null) stmt.close (); if (con! = null) con.close (); } catch (SQLException e) {System.out.println (e.tostring ()); } }
Class.forName (String) is used in Java reflection to load a class, where a driver is loaded, and then Drivermanager.getconnection () Gets the database connection, Let's take a look at the DriverManager class.
DriverManager as the driver manager, there is an internal class driverinfo for accessing the driver information, maintaining a driver object. After the driver is loaded, you need to get the database connection from the DriverManager.
public static Connection getconnection (string url, string user, String password) throws SQLException { java.util . Properties info = new Java.util.Properties (); if (user! = null) { info.put ("user", user); } if (password! = null) { info.put ("password", password); } Return (getconnection (URL, info, Reflection.getcallerclass ())); }
private static Connection getconnection (String url, java.util.Properties info, class<?> caller) throws Sqlex ception {/* * If CALLERCL is empty, we have to check the application's ClassLoader so that we can load the JDBC driver outside of Rt.jar */ClassLoader CALLERCL = CA Ller! = null? Caller.getclassloader (): null; Synchronized (Drivermanager.class) {//Synchronize loading of the correct classloader. if (CALLERCL = = null) {CALLERCL = Thread.CurrentThread (). Getcontextclassloader (); }} if (url = = null) {throw new SQLException ("The URL cannot be null", "08001"); } println ("Drivermanager.getconnection (\" "+ URL +" \ ")"); SQLException reason = null; for (Driverinfo adriver:registereddrivers) {//If the ClassLoader does not have permission to load the driver, skip the IF (isdriverallowed (adriver.drive R, Callercl)) {try {println ("trying" + ADriver.driver.getClass (). GetName ()); PhilipAttempt to connect the database with the driver Connection con = aDriver.driver.connect (URL, info); if (con! = null) {println ("getconnection returning" + ADriver.driver.getClass (). GetName ()); return (con); }} catch (SQLException ex) {if (reason = = null) {reason = ex; }}} else {println ("skipping:" + adriver.getclass (). Get Name ()); }}//Omit part of the code ...}
DriverManager calls a static block of code at class load to initialize the database driver, and when the user uses getconnection, driver tries to connect to the database, and once a driver can connect to the current database, the connection is returned. With MySQL'sMySQL-connector-JavaFor example, if you want your database to be connected to a JDBC database, you must implement the connection interface. Connection not only provides transactional operations but also an abstract method createstatement can create a SQL declaration, and here is the second more important class statement for JDBC. Also this is an interface, need different database developers themselves to provide implementation, the main function of this object is to execute SQL return resultset, the same resultset is also an interface need to provide a third-party implementation, it is similar to a table data structure, each call to next ( It points to the next database record, calling different GetXXX (String columnName) for different types of fields to get the value of the corresponding field for that record. In addition to these primary interfaces, JDBC provides a DatabaseMetaData interface that describes database properties and some type classes such as Date and exception classes.
From the above analysis, we can see that the JDBC API is actually very simple, it is a Java-to-database bridge, only provides a rough specification, all operations on a particular database need a third party to provide the implementation.
ORM Framework Source Code Analysis: Introduction of Java JDBC