Design and implementation of self defined persistent layer framework

Keywords: Java SQL JDBC xml Database

Traditional jdbc Code:

package com;

import java.sql.*;

/**
 * @author liuyj
 * @Title: JdbcTest
 * @create 2020-05-27 17:31
 * @ProjectName algorithm-Exercise
 * @Description: TODO
 */
public class JdbcTest {
    public static void main(String[] args) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            //Load driver
            Class.forName("com.mysql.jdbc.Driver");
            //Get database connection by driving management class
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis","root","root");
            //Define sql statement? Represents placeholder
            String sql="select * from user where username=?";
            //Get preprocessing statement
            preparedStatement=connection.prepareStatement(sql);
            //Set parameters
            preparedStatement.setString(1,"tom");
            //Execute query to get results
            resultSet = preparedStatement.executeQuery();
            //Traverse query result set
            while (resultSet.next()) {
                int id=resultSet.getInt("id");
                String username=resultSet.getString("username");
                System.out.println(username+id);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //Release resources
            if(resultSet !=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (preparedStatement != null) {

                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}


Problems with jdbc:

Custom frame design

User side (project): jar package introducing custom persistence layer framework

Two parts of configuration information are provided: database configuration information and sql configuration information: sql statement, parameter type and return value type

Use the configuration file to provide these two parts of information:

(1) sqlMapConfig.xml : store database configuration information, import mapper.xml (storage mapper.xml Full path of)

(2) Mapper.xml : store configuration file information of sql statement

Framework end (the essence of custom persistence layer framework is to encapsulate jdbc code):

1. Read configuration file

According to the path of the configuration file, load the configuration file into a byte input stream and store it in memory

Create Resource class
Method: InputStream getResourceAsStream(String path)

2. Create two JavaBeans to store: the stored content is the content parsed from the configuration file

(1) Configuration core configuration class: storage sqlMapConfig.xml Parsed content

(2) MappedStatement: mapping configuration class, storing mapper.xm Parsed content (sql statement, input parameter java type, output parameter java type)

3. Parsing configuration file: dom4j

Create SqlSessionFactoryBuilder method: build(InputStream in)

First, use dom4j to parse the content of the configuration file and encapsulate the parsed content into the container object

Second, create SqlSessionFactory object; production sqlSession: session object (factory mode)

4. Create SqlSessionFactory interface and implementation class DefaultSqlSessionFactory
First, openSession: produce sqlSession

5. Create SqlSession interface and implementation class DefaultSession
Define crud operations on the database: selectList()
selectOne()
update()
delete()

6. Create Executor interface and implementation class simpleexecutior implementation class:
query(Configuration,MappedStatement,Object... params): jdbc code is executed

Welcome to:

Water cold blog

Posted by mraiur on Wed, 27 May 2020 04:46:12 -0700