Readme
After learning the ORM chapter of Spring 5 core principles and 20 class handwriting practice, I want to write a simple ORM framework myself. The first few chapters are moved to the editor according to the code in Tom's book, and have a general understanding of the core principles of Spring. When I finished reading the ORM part, I thought the code was too enumerative, so I wanted to write a semi-automatic ORM instead of the fully automatic orm in the book, so I had this note.
Before that, I've seen some of the source code of MyBatis, but I don't know much about it. I just have a general reading of its data source. In this framework, I simply implement the data connection pool.
In the process of writing, I realized the charm of TDD and guided me to complete these functions. Since my ORM refers to the Mapper style of MyBatis, it's called LittleBatis.
Section 0 development environment
IDEA 2018 + Maven 3.6.1 + JDK 1.8.231 + Git.
Section I objective description
The goal of littlebattis is relatively simple. CRUD a basic data table. The returned results support Map, List, Integer and a common object. If you enter a parameter, you can support an object or direct parameter name. Mainly through the following XML validation.
<?xml version="1.0" encoding="UTF-8" ?> <mapper namespace="website.adagoto.orm.demo.dao.UserDao"> <insert id="addUser" parameterType="website.adagoto.orm.demo.pojo.User" resultType="java.lang.Integer"> insert into USER(ID, NAME , PASSWORD) values(#{user.id},#{user.name}, #{user.password}) </insert> <update id="updateUser" parameterType="website.adagoto.orm.demo.pojo.User" resultType="java.lang.Integer"> update USER set NAME = #{user.name} , PASSWORD = #{user.password} where ID = #{user.id} </update> <delete id="deleteById" parameterType="java.lang.String" resultType="java.lang.Integer"> delete from USER where ID = #{id} </delete> <select id="queryById" parameterType="website.adagoto.orm.demo.pojo.User" resultType="java.util.Map"> select * from USER where id = #{id} </select> <select id="queryListByName" parameterType="website.adagoto.orm.demo.pojo.User" resultType="java.util.Map"> select * from USER where name = #{name} </select> </mapper>
Section II step conception
For the use cases in Section 1, I split them as follows:
Step 1: I need a data Connection pool to manage connections.
Step 2: I want to use a configuration reader to read JDBC and Mapper storage information.
Step 3: scan the information in Mapper, store the information in the Map, and generate the corresponding method processor.
Step 4: implement the interface proxy with JDK dynamic proxy.
Step 5: implement the SQL input parameter processor and the type wrapper after executing SQL.
Step 6: implement the SQL executor.
Step 7: debugging.
Section III frame design
Figure 3.1 architecture
The implementation of littlebattis is mainly composed of nine classes in Figure 3.1.
ConfigurationReader is a class that reads the database.properties configuration.
MapperMapping is a class of proxy Dao layer interface, which contains the Map of MethodHandler of each method.
MapperReader scans the address of Mapper.xml file and generates corresponding MapperMapping and MethodMapping according to the configuration of Mapper.xml.
ParameterHandler is a class that handles Dao interface input parameters.
MethodMapping records the configuration information of the method of Mapper.xml.
MethodHandler is the method handler.
ResultHandler is the processor that returns results, mainly type conversion.
SQLHandler is the processing of SQL and SQL parameters. It is used to generate SQLCommand required by SQLRunner.
SQLRunner is mainly responsible for executing sql and returning results.
Section 4 coding implementation
https://gitee.com/cisnotcpp/learining-orm
Section V test and verification
Run the tests in EnvironmentTest in your code.