Tom bomb architecture 2022-01-26 14:59:04 阅读数:941
This article is excerpted from 《Spring 5 The core principle 》
Speaking of ResultSet, Yes Java Development experience “ buddy ” Nature is the most familiar , But I believe for most people, it's also “ The most familiar stranger ”. from ResultSet Value taking operation, everyone will , such as :
The above is our routine operation before using the framework . With the increase of business and development , In the data persistence layer, such repeated code occurs very frequently . therefore , We thought of separating non functional code from business code . The first thing we think about is ResultSet Code logic separation of encapsulated data , Add one more mapperRow() Method , Specialized in the encapsulation of results , The code is as follows :
But in a real business scenario , Such code logic repetition rate is too high , The above transformation can only be applied Member class , Another entity class has to be re encapsulated , A smart programmer certainly won't write one for every entity class through pure manual labor mapperRow() Method , You will think of a code reuse scheme . We might as well do such a transformation .
First create Member class :
Optimize JDBC operation :
The above skillfully uses the reflection mechanism to read Class Information and Annotation Information , Associatively map and assign values to the columns in the database table and the fields in the class , To reduce duplicate code .
Through the previous explanation , We already know ORM The basic implementation principle of the framework .ORM Refers to object relational mapping (Object Relation Mapping), Mapping is not just object values , And the relationship between objects , For example, one to many 、 Many to many 、 A one-to-one table relationship . Now on the market ORM There are also many frameworks , There are well-known Hibernate、Spring JDBC、MyBatis、JPA etc. . Here's a brief summary , As shown in the following table . |
name | features | describe |
---|---|---|---|
Hibernate | Fully automatic ( block ) | You don't need to write a sentence SQL | |
MyBatis | semi-automatic ( block ) | Hands are one , Support simple mapping , Complex relationships need to be written by yourself SQL | |
Spring JDBC | Pure manual ( block ) | be-all SQL Write it yourself , It helps us design a standard process |
Since there are so many choices on the market , Why should I write it myself ORM What about the framework ?
This has to start with my experience as an architect in an airborne . The biggest problem faced by airborne is how to get the team “ friends ” The trust of the . at that time , The team has a total of 8 people , Everyone's level is uneven , Some people haven't even touched MySQL, Such as Redis Wait for caching middleware, let alone . Basically only use Hibernate Of CRUD, And it has affected the system performance . Due to the tight schedule , No time and energy to do systematic training for the team , Also in order to give consideration to controllability , So there is self research ORM The idea of a framework . I made such a top-level design , To reduce team “ friends ” The cost of saving interest , Unified parameters of top-level interface 、 Uniform return value , As follows .
(1) The interface model of the specified query method is :
(2) The interface model that specifies the deletion method is :
(3) The interface model that specifies the insertion method is :
(4) The interface model specifying the modification method is :
Take advantage of this basic API, Later, I based on Redis、MongoDB、ElasticSearch、Hive、HBase Each package has a set , So as to reduce the learning cost of the team , It also greatly improves the controllability of the program , More convenient for unified monitoring .
Definition Page The main purpose of class is to provide top-level support for the unified return results of later paging queries , Its main functions include the encapsulation of paging logic 、 Paging data .
ResultMsg Class is mainly the top-level design for unified return results , It mainly includes the status code 、 Result description and return data .
As all BaseDao The top-level interface of the persistence framework , The main definition is 、 Delete 、 Change 、 Check the unified parameter list and return value .
If you use QueryRule Class to build query criteria , Users do not need to write when making conditional queries SQL, Implement business code and SQL decoupling .
Order Class is mainly used to encapsulate sorting rules , The code is as follows :
Because of the length , Specific operation classes continue in the next article .
Pay attention to WeChat public number 『 Tom Bomb architecture 』 reply “Spring” The complete source code is available .
This paper is about “Tom Bomb architecture ” original , Reprint please indicate the source . Technology is about sharing , I share my happiness ! If you have any suggestions, you can also leave comments or private letters , Your support is the driving force for me to adhere to my creation . Pay attention to WeChat public number 『 Tom Bomb architecture 』 More technical dry goods are available !
Originality is not easy. , Persistence is cool , You can see it here , Little buddy, remember to praise 、 Collection 、 Looking at , One click three times plus attention ! If you think the content is too dry , You can share and forward it to friends !
copyright:author[Tom bomb architecture],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261459029324.html