The difference between JDBC, datasource, connection pool, database driver, and persistence layer framework

1. Database

A database is a "warehouse" that organizes, stores and manages data according to the data structure.
Databases are divided into relational databases (sql databases) and non-relational databases (no-sql databases). The difference between the two is whether to use SQL statements as the method of operation. Most of our common databases are relational databases, such as MySql database, PostgreSql, oracle database.

2. Database driver

The database driver is a program developed by different database developers (such as oracle mysql, etc.) to enable database calls in a certain development language environment (such as java). The role of the database driver is equivalent to a translator, translating the database
in Java The calling language is translated into the database's own database language . Of course, this translation (database driver) is customized and developed by each developer for a unified interface.
Commonly used drivers:

1. MySQL
driver class name: com.mysql.jdbc.Driver
JDBC URL (connection address): jdbc:mysql://dbip:port/databasename

  • dbip: is the IP address of the database server. If it is local and writable: localhost or 127.0.0.1.
  • port: It is the listening port of the database. It depends on the configuration during installation. The default is 3306.
  • databasename: The name of the database.

2. SQL Server database
driver class name: com.microsoft.jdbc.sqlserver.SQLServerDriver
JDBC URL: jdbc:microsoft:sqlserver://dbip:port;DatabaseName=databasename

  • dbip: is the IP address of the database server. If it is local and writable: localhost or 127.0.0.1.
  • port: It is the listening port of the database. It depends on the configuration during installation. The default is 1433.
  • databasename: The name of the database.

3. Oracle database
driver class name: oracle.jdbc.driver.OracleDriver
JDBC URL: jdbc:oracle:thin:@dbip:port:databasename

  • dbip: is the IP address of the database server. If it is local and writable: localhost or 127.0.0.1.
  • port: It is the listening port of the database. It depends on the configuration during installation. The default is 1521.
  • databasename: is the SID of the database, usually the name of the global database.

As shown below, before JDBC, Java developers needed to maintain drivers for different databases in order to operate different databases.
image.png

3、JDBC

The full name of JDBC is Java Database connect, which is a set of Java API for executing SQL statements. Applications can connect to relational databases through this set of APIs and use SQL statements to complete operations such as querying, updating, and deleting data in the database. Simply put, it is a component that connects and operates the database.

public static void main(String[] args) throws Exception {
    
    
        //1. 加载数据库驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2. 通过驱动管理类获取数据库连接
        Connection conn = DriverManager.getConnection(url,username,password);
        //3. 通过Connection对象创建Statement对象
        Statement stmt = conn.createStatement();
        //4. 执行sql语句
        ResultSet rs = preparedStatement.executeQuery("SELECT * FROM ...") ;
        //5. 遍历结果集
        while(rs.next()){
    
        
         String name = rs.getString("name") ;      
         }   
        //6. 关闭数据库连接,回收数据库资源
        // 关闭顺序:依次关闭ResultSet、Statement、Connection等资源。
        if(rs !=null){
    
       // 关闭记录集    
           try {
    
    
              rs.close();
           } catch (SQLException e) {
    
    
              e.printStackTrace();
           }
        }    
        if(stmt !=null){
    
       // 关闭声明    
           try {
    
    
              stmt.close();
           } catch (SQLException e) {
    
    
              e.printStackTrace();
           }
        }
        if(conn !=null){
    
      // 关闭连接对象    
           try {
    
    
              conn.close();
           } catch (SQLException e) {
    
    
              e.printStackTrace();
           }
        }
}

The way an application uses JDBC to access the database is as shown in the figure below: As
image.png
can be seen from the above figure, when an application uses JDBC to access a specific database, it needs to connect with different database drivers. Since the database drivers provided by different database vendors are different, in order for the application to truly establish a connection with the database, JDBC needs to provide an API for accessing the database and encapsulate the details of communication with various database servers.

4、datasource

Data source technology is a key technology for Java to operate databases. Popular persistence frameworks are inseparable from the application of data sources.
The data source provides a simple way to obtain a database connection , and can reuse the database connection internally through a pool mechanism , which greatly reduces the number of database connection creations and improves system performance. Reasons for needing a data source: Because using jdbc to establish and destroy connections is too wasteful of resources, a set of efficient components for establishing connections with the database is needed, which is the data source. The data source can create a connection pool. Common ones include DBCP, C3P0, druid, hikariCP, etc.

5. Database connection pool

The database connection pool is responsible for allocating, managing and releasing database connections. It allows applications to reuse an existing database connection instead of establishing a new one. The connection pool technology reuses as many memory resources as possible, greatly saves memory, improves the service efficiency of the server, and can support more customer services. By using the connection pool, the program running efficiency will be greatly improved. At the same time, we can monitor the number and usage of database connections through its own management mechanism.
Take accessing MySQL as an example and execute a SQL command.
Not using connection pooling:

Steps to not use database connection pooling:

TCP建立连接的三次握手
MySQL认证的三次握手
真正的SQL执行
MySQL的关闭
TCP的四次握手关闭

It can be seen that in order to execute a SQL, there are many more network interactions that we don't care about.

advantage shortcoming
Simple to implement There is a lot of network IO and the load on the database is high.
Long response time Long response time
Application frequently creates and closes connections
After closing the connection, a large number of TIME_WAIT TCP states will appear

Process of using connection pool:

Steps to use database connection pool:

第一次访问的时候,需要建立连接。 但是之后的访问,均会复用之前创建的连接,直接执行SQL语句。

advantage:

  1. Less network overhead
  2. System performance will be substantially improved
  3. No more troublesome TIME_WAIT states

6. Persistence layer framework

6.1、ORM

Object-Relational Mapping (ORM for short), the object-oriented development method is the mainstream development method in today's enterprise-level application development environment, and the relational database is the mainstream data storage system that permanently stores data in the enterprise-level application environment. Objects and relational data are two forms of expression of business entities. Business entities are represented as objects in memory and as relational data in the database . There are associations and inheritance relationships between objects in memory, but in the database, relational data cannot directly express many-to-many associations and inheritance relationships. Therefore, object-relational mapping (ORM) systems generally exist in the form of middleware, which mainly implements the mapping of program objects to relational database data . Object-relational mapping explained: A. Simple: ORM models data in its most basic form. For example, ORM will map a MySQL table into a Java class (model), and the fields of the table are member variables B of this class. Accurate: ORM enables all mysql data tables to be accurately mapped into java classes according to unified standards. Keep the system accurate and unified at the code level C. Easy to understand: ORM documents the database structure. For example, the MySQL database is converted by ORM into java classes that java programmers can understand. Java programmers can only focus on the java level they are good at (of course it is better to be proficient in MySQL) D. Ease of use: ORM includes APIs for CRUD operations on persistent objects, such as create(), update(), save(), load(), find(), find_all(), where(), etc. In other words, all SQL queries are encapsulated into programming languages. The functions in the function generate the final SQL statement through chain combination of functions. Through this kind of encapsulation, non-standard, redundant, and non-uniform SQL statements can be avoided, many human-made bugs can be avoided, and the unification of coding style and later maintenance can be facilitated.





6.2、JdbcTemplate

JDBC also provides a set of interfaces for operating the database for additions, deletions, modifications, and queries, but it is too cumbersome.
JdbcTemplate is a template provided by spring to access jdbc. It is Spring's encapsulation of JDBC and aims to make JDBC easier to use.
JdbcTemplate is a part of Spring that handles the creation and release of resources, helping us avoid some common mistakes, such as forgetting to always close the connection. It runs the core JDBC workflow, such as the establishment and execution of Statement, and we only need to provide SQL statements and extract results, which is much simpler than traditional JDBC operations.
Usage of JdbcTemplate:

JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSourceUtils.getDataSource());
String sql = "INSERT INTO product VALUES (NULL, ?, ?);";
jdbcTemplate.update(sql, "iPhone3GS", 3333);

6.3、MyBatis

Mybatis is an excellent persistence layer (semi-)ORM
framework that supports ordinary SQL queries, stored procedures and advanced mapping.
Mybatis avoids almost all JDBC code and manual setting of parameters and obtaining result sets.
In SpringBoot, MybatisAutoConfiguration class. You can see that the data source DataSource is injected into the SqlSessionFactory, and then the SqlSessionTemplate is created based on the SqlSessionFactory. This completes the automatic assembly, and we can use @Autowired directly.

  @Bean
  @ConditionalOnMissingBean
  public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    
    
    SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
    factory.setDataSource(dataSource);
    //...
  }
  
  @Bean
  @ConditionalOnMissingBean
  public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
    
    
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
    
    
      return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
    
    
      return new SqlSessionTemplate(sqlSessionFactory);
    }
  }

Components of Mybatis:

Introduction to the functions of each component:

  • Mybatis configuration file: SqlMapConfig.xml is the global configuration file of Mybatis. It mainly configures data sources, transactions, loading mapping files, etc. Its name can be anything (it is best to know the meaning by name). Mapper.xml mainly configures Statement-related information, such as SQL statements.
  • SqlSessionFactoryBuilder: will generate SqlSessionFactory objects based on XML configuration or Java configuration. Use the builder mode (simply speaking, it is to build a large object step by step, such as building a big house, using the steps of buying bricks, laying bricks, and painting the walls) Construction, in which the big house is the big object, and the series of construction steps is the step-by-step construction).
  • SqlSessionFactory: used to generate SqlSession. SqlSession objects can be created through the SqlSessionFactory.openSession() method. Use the factory pattern (in simple terms, we obtain the object through a class, and this class creates the instance we need and returns it, instead of creating it ourselves through new).
  • SqlSession: Equivalent to the Connection object in JDBC. You can use the SqlSession instance to directly execute the mapped SQL statement or obtain the corresponding Mapper.
  • Executor: All Mapper statements in MyBatis are executed through Executor. (Mapper: It consists of XML files and Java interfaces. It executes the corresponding SQL statements according to the mapping information configured in the XML and returns the execution results.)
  • Mapper interface: The data operation interface is also commonly referred to as the DAO interface. It must correspond one-to-one with the methods in the Mapper configuration file, that is, it must be consistent with the add, delete, modify, check tag ID in Mapper.xml.
  • Mapper configuration: used to organize specific query business and map field relationships of the database, which can be implemented using XML format (Mapper.xml) or Java annotation format.
  • MappedStatement: Its function is to encapsulate Statement-related information, including SQL statements, input parameters, output results, etc.

Mybatis execution process:

  1. First, the global configuration file of Mybatis is loaded, and then the SQL mapping file or the related SQL content of the annotation is loaded.
  2. Create a session factory. MyBatis constructs a session factory (SqlSessionFactory) by reading the information in the configuration file.
  3. Create a session. According to the session factory, MyBatis can create a session object (SqlSession) through it. The session object is an interface that contains methods for adding, deleting, modifying, and querying database operations.
  4. Create an executor. Because the session object itself cannot directly operate the database, it uses an interface called the database executor (Executor) to help it perform operations.
  5. Encapsulate the SQL object. In this step, the executor encapsulates the SQL information to be processed into an object (MappedStatement), which includes the SQL statement, input parameter mapping information (Java simple type, HashMap or POJO) and output result mapping information ( Java simple types, HashMap or POJO).
  6. To operate the database, once you have the executor and SQL information encapsulation object, use them to access the database, and finally return the operation result to end the process.

6.4、JPA

The full name of JPA is Java Persistence API, which is a set of ORM-based specifications
launched by SUN . The JPA specification is essentially an ORM specification. Note that it is not an ORM framework - because JPA does not provide an ORM implementation, it It only formulates some specifications and provides some programming API interfaces, but the specific implementation is provided by service vendors. The relationship between JPA and Hibernate is like the relationship between JDBC and JDBC drivers. JPA is the specification. In addition to being an ORM framework, Hibernate is also a JPA implementation.

Spring Data JPA is a set of JPA application frameworks encapsulated by Spring based on the ORM framework and JPA specifications. The underlying layer still uses the hibernate technology that implements jpa, allowing developers to access and operate the database with minimalist code.
Usage of JPA framework:
Define the interface for object operations and inherit the core interface of JpaRepository.

import com.boot.jpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
    
    

    // 但条件查询
    User findByAge(Integer age);
    // 多条件查询
    User findByNameAndAge(String name, Integer age);
    // 自定义查询
    @Query("from User u where u.name=:name")
    User findSql(@Param("name") String name);
}

7. Summary

JDBC: Native API for connecting and operating databases, close to the underlying code.
JDBC and data source: Data source is the implementation of jdbc to connect to the database.
Data sources and database drivers: To connect to different databases, you need to use different drivers.
Data source and connection pool: The data source will use the underlying jdbc to generate some idle connections and put them into the connection pool for use when accessing the database.
To operate JdbcTemplate, MyBatis and data source, you need to connect to the database first, so you need to set the data source for jdbcTemplate and MyBatis so that jdbcTemplate and MyBatis can use the connection pool of the data source.
jpa and Hibernate: Hibernate is implemented according to the jpa specification. The bottom layer also uses data sources and JDBC to access the database.
JDBC and jpa: Essentially, jdbc and jpa are not on the same level.

JDBC是数据库的统一接口标准。
jpa是ORM框架的统一接口标准。
用法有区别: jdbc更注重数据库,orm则更注重于java代码,
但是实际上jpa实现的框架底层还是用jdbc去和数据库打交道。

reference:

  1. https://www.cnblogs.com/yanze/p/9714703.html
  2. https://www.zhihu.com/question/45993333
  3. https://juejin.cn/post/6844903475239714823
  4. https://blog.csdn.net/An1090239782/article/details/107159335
  5. https://www.cnblogs.com/tanghaorong/p/13856465.html#:~:text=MyBatis%20%E6%98%AF%E6%94%AF%E6%8C%81%E5%AE%9A%E5%88%B6%E5%8C%96,%E5%8F%82%E6%95%B0%E4%BB%A5%E5%8F%8A%E8%8E%B7%E5%8F%96%E7%BB%93%E6%9E%9C%E9%9B%86%E3%80%82
  6. https://blog.csdn.net/u013541707/article/details/113182157
  7. https://blog.csdn.net/papima/article/details/78219000

Guess you like

Origin blog.csdn.net/hansome_hong/article/details/124320410