In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to handwrite mybatis framework". In daily operation, I believe many people have doubts about how to handwrite mybatis framework. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to handwrite mybatis framework". Next, please follow the editor to study!
Handwritten mybatis Framework simple-ibatis Preface:
I haven't updated the source code parsing of mybatis for a long time, because I have been putting my understanding of mybatis into practice recently. In the process of learning the source code of mybatis, I built an ORM framework according to the idea of mybatis. The whole code is constructed manually, not a single word of code is Copy, which is certainly not as good as Google's code, but it has basically achieved the implementation of SQL statements and object-relational mapping functions. If you are interested in the source code, you can join me to write this project. The address will be attached later, if you think it's good, I hope you can star it manually. Project address: simple-ibatis
Project profile:
Simple-batis is a simple ORM framework written by yourself. When learning the mybatis source code, I want to express my opinion. It takes about 3 weeks, which basically satisfies some commonly used Sql operations. the code involved in this project is written by individuals, without a sentence of copy, it is certainly not perfect. We understand that it will be updated all the time in the future. If you are interested in the source code, you can also join us and turn your understanding into code will really impress you. The code runs on java8 by default, because parameter reflection is used, so remember to turn on parameters in idea
The opening steps are as follows: file-> Settings- > Build,Execution,Deployment- > Compiler- > Java Compiler 2. Fill in-parameters after Additional command line parameters:
Code introduction: comment com.simple.ibatis.annotation
@ Dao is annotated on the mapper class. Indicates that the class is a mapper class that is parsed when it is parsed.
@ Daopublic interface App1 {}
@ Select is annotated on the methods in the mapper class. Indicates that the method is a Select method and writes a specific sql statement inside the Select method. In the case of parameter injection, the parameter is replaced by {}.
@ Select ("SELECT name from sys_user where name = {user.name} and id = {id}") List test1 (User user, int id)
@ Param is annotated on the method parameters in the mapper class. Rename the parameter name once. If you do not use this comment, it defaults to the parameter name as the injected element.
@ Select ("SELECT name from sys_user where id = {userId}") List test2 (@ Param ("userId") int id)
@ Update is annotated on the methods in the mapper class. Indicates that the method is a Update method.
@ Update ("update sys_user set name = {user.name} where id = {user.id}") void update3 (User user)
@ Insert is annotated on the methods in the mapper class. Mark that the method is an Insert method
@ Insert ("insert into sys_user (id,name) values ({user.id}, {user.name})") int insert4 (@ Param ("user") User user)
@ Delete is annotated on the methods in the mapper class. Mark that the method is a Delete method
@ Delete ("delete from sys_user where id = {user.id}") int delete5 (@ Param ("user") User user); database registration com.simple.ibatis.driver
DriverRegister provides database registration function. Duplicate registration is not avoided and a cache is used internally.
Data source com.simple.ibatis.datasource
NormalDataSource ordinary data source, no pooling function, provides the ability to obtain database connections. PoolDataSource pooled data source, storing active connection list and idle connection list. And a series of operations are done to obtain and release the connection. The wrapper class of the PoolConnection connection not only stores the real connection, but also stores the time when the connection was obtained, which is used to determine whether the connection has timed out.
Core class com.simple.ibatis.core
Config global core class, store data source, mapper package address, mapper class parsing file MapperCore mapper class parsing file SqlSource specific sql statement encapsulation
Proxy class com.simple.ibatis.mapping
MapperProxy mapper interface proxy class. Using dynamic proxy technology
Actuator class com.simple.ibatis.execute
Executor executor interface SimpleExecutor specific executor, execute specific sql method. Generate the resulting ExecutorFactory generate the factory class of Executor
Reflective com.simple.ibatis.reflect
The ClassWrapper class enhancer that encapsulates the get and set methods of Object. ObjectWrapper object wrapper class. You can set and get properties by calling ObjectWrapper.setVal and getVal. Call the object's getxxx and setxxx methods that do not need to be displayed. ObjectWrapperFactory object wrapper class generator
Processor class com.simple.ibatis.statement
PreparedStatementHandle PreparedStatement generator. Change the java property to a jdbc property and inject it. ResultSetHandle parses the query structure ResultSet and converts it to Java type
Tool class com.simple.ibatis.util
PackageUti parsing package tool class TypeUtil type judgment tool class
Operation example: 1. Build the pojo file (and create the table in the database) public class User {private int id;private String name;public int getId () {return id;} public void setId (int id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;}} 2. Build the mapper file package com.simple.ibatis.mapper;@Daopublic interface App1 {@ Select ("SELECT * from sys_user") List select2 (); @ Select ("SELECT name from sys_user where name = {user.name} and id = {id}") List select3 (User user, @ Param ("id") int id); @ Update ("update sys_user set name = {user.name} where id = {user.id}") void update4 (User user) Insert ("insert into sys_user (id,name) values ({user.id}, {user.name})") int insert5 (@ Param ("user") User user);} 3. Build the data source and actuator factory class: PoolDataSource poolDataSource = new PoolDataSource ("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/auth", "root", "root"); / * enter the package where the mapper file is located and the data source * / ExecutorFactory executorFactory = new ExecutorFactory ("com/simple/ibatis/mapper", poolDataSource); 4. Operation: / * * get the specific mapper proxy class * / SimpleExecutor executor = executorFactory.getExecutor (); App1 app1 = executor.getMapper (App1.class); / * * build query conditions * / User user = new User (); user.setName ("xiabing"); user.setId (1); / * * call insert method * / int count = app1.insert5 (user) / * * call the update method * / user.setName ("root"); app1.update4 (user); / * * query the user name and return the character * / List users = app1. Select3 (user,3); System.out.println (users.get (0)); / * * query user and return object * / List userLists = app1.select2 (); System.out.println (userLists.get (0) .getName ())
5. The framework needs to be improved:
At present, the framework only supports annotation injection SQL statements, but not XML injection SQL statements.
Currently, collection classes are not supported by object properties, and nested non-basic data types are not supported in objects.
No caching is added to the framework
Parsing mapper files only supports mapper files under one package.
At this point, the study on "how to handwrite mybatis Framework" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.