MYJDBC Reverse Proxy simple frame

Source: Internet
Author: User

Source Address: Https://github.com/beijing-penguin/Myjdbc

  Why should I write this myjdbc?

After graduating for a period of time has been using Hibernate, and MyBatis framework, and later felt that this framework is too cumbersome, for example, now to use Java to read a customer to an Excel table, read the data inside the database inserted into the databases, such a small demand, I need to configure hibernate files, configuration MyBatis files, for me, too much trouble, because I am lazy people, do not like too much things, just like easy to use.

I remember one time with the Hibernate framework to manage multiple data sources, there is a problem (man-made problem), when the first connection is submitted, the second submission will be error, then how to do, project tight, insufficient manpower, hard Baidu to find the answer, then only to sit down? Or find someone else to help? How much manpower and financial resources does it cost? How much time did I spend studying hibernate? Wait a minute. In summary, once a third-party framework goes wrong, it means that the root cause cannot be found and is difficult to debug. In my myjdbc, debugging will become very simple.

  Talk about my myjdbc.

I am glad that I told the group of friends yesterday that I would be able to get feedback from some of my friends and say that I would like to study (or see) my framework. In fact, if my framework needs to be "researched" before it can be used, then it means that I failed the design. JDBC Framework, there are so many people encapsulation, why I have to build wheels, I am a lazy, I do not want to build wheels, I just want to innovate a development idea, or I just want to quickly develop, complete the project, and then drink, play mobile phone sitting in the company Sasa, even if there is a bug, I can also locate the root cause of the problem in seconds and resolve it. Then for this myjdbc was born, a frame that belongs to itself and belongs to everyone. Framework Features: Database connection and database operations (add-and-revise), providing multi-data source management and weak transaction operations under multiple data sources (commit and rollback). Framework benefits: Highly flexible and scalable, reusable components, easy to use, self-defining other functions, very convenient debugging. Framework Disadvantages: 1, does not support dynamic sql,2, temporarily does not support the Sub-table sub-Library, is under development ... 3,

  Myjdbc How to use and what are the characteristics

Recommend, when developing, try to copy the source code to the project. After the line stabilized, you can consider packaging into Myjdbc.jar

To understand how to debug and how to use it, first look at the directory structure I designed

  

Anno is the annotation class,

Config is the global JDBC configuration, any configuration can be defined here, such as whether to print SQL logs before executing SQL?

The core package, which encapsulates the original JDBC operation, and any method of manipulating the class has only 3 parameters, such as the following Deleteoper.java source code,

/** * Delete operation * @author DC * @time 2015-8-17 */public class Deleteoper extends opersuper{    private static Deleteoper Delet Eoper = new Deleteoper ();    public static Deleteoper getinstance () {        return deleteoper;    } public  int Delete (Connection conn,string sql,object[] params) throws Exception{return Super.preparedandexcutesql ( conn, SQL, params);}}

If I encapsulated this code does not meet your company's project requirements, please write a method, but there are only 3 parameters, if 3 parameters are not enough, please give me feedback, and explain the reason.

The entity package, which is a temporary medium for information related to SQL elements. For example, it can be used to save the SQL string and the array of object parameters of the string.

Because Java syntax does not support executing a method, it returns two parameters, like this in the form of object Sql,param = GetUser () (Lua language support).

The Hepler package, which is equivalent to the most superficial application-level tool, can be understood as the view layer. As a way to make the underlying method transparent and visible

Init package. Then JDBC can selectively perform init initialization of the Operations tool class under this package, such as the need to load the XML file defined in the SQL file

Under the Utils package, currently the main definitions are the Reflection acquisition method name, Java type matching and conversion tools.

Use cases such as the following code

 Packagetest;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportOrg.dc.jdbc.core.ConnectionManager;ImportOrg.dc.jdbc.helper.DBHelper;Importorg.junit.Test; Public classJdbctest {Private StaticDBHelper Accdbhelper =NewDBHelper (Configure.accsource); Private StaticDBHelper Testdbhelper =NewDBHelper (Configure.testsource); @Test Public voidSelect () {Try{ConnectionManager.isTransaction.set (false); /*Start*/Map<String,Object> map = Testdbhelper.selectone ("SELECT * from user Limit 1"); List<Map<String,Object>> maplist = testdbhelper.selectlist ("SELECT * from User"); /*End*/Map<String,Object> map_withparam1 = Testdbhelper.selectone ("SELECT * from user where name=?") Age=? Limit 1 "," DC ", 20); User User= Testdbhelper.selectone ("Select Name,age from user where name=?") Age=? Limit 1 ", User.class, "DC", 20); //return only Age            intAge = Testdbhelper.selectone ("Select age from User where name=?") Age=? Limit 1 ", Integer.class, "DC", 20); String name= Testdbhelper.selectone ("Select name from user where name=?") Age=? Limit 1 ", String.class, "DC", 20); Map<String,Object> map_withparam1_1 = Testdbhelper.selectone ("SELECT * from user where name=?") Age=? Limit 1 ",Newobject[]{"DC", 20}); //Incoming MapMap<string,object> Mapparams =NewHashmap<string, object>(); Mapparams.put ("Name", "DC"); Mapparams.put ("Age", 12); Map<String,Object> map_withparam1_2 = Testdbhelper.selectone ("SELECT * from user where name=#{name} age=#{age} limit 1 ", Mapparams); Map<String,Object> map_withparam1_2_1 = Testdbhelper.selectone ("$user. Getoneuser", Mapparams); //incoming objects, or objects can be passed in as parameters along with the mapUser userobj =NewUser (); Userobj.setname ("DC"); Userobj.setage (12); Map<String,Object> map_withparam1_3 = Testdbhelper.selectone ("SELECT * from user where name=#{name} age=#{age} limit 1 ", Userobj); List<Map<String,Object>> maplist_withparam1 = testdbhelper.selectlist ("SELECT * from User"); List<User> maplist_withparam1_1 = testdbhelper.selectlist ("Select Name,age from User", user.class); List<String> maplist_withparam1_2 = testdbhelper.selectlist ("Select name from User", String.class); List<Integer> maplist_withparam1_3 = testdbhelper.selectlist ("Select name from User", Integer.class); } Catch(Exception e) {e.printstacktrace (); }finally{connectionmanager.closeconnection (); }} @Test Public voidInsert () {ConnectionManager.isTransaction.set (true); Try{Testdbhelper.insert (Insert into User (Name,age) values (?,?), "DC", 12);        Connectionmanager.commit (); } Catch(Exception e) {e.printstacktrace ();        Connectionmanager.rollback (); }finally{connectionmanager.closeconnection (); }    }}classuser{PrivateString name; Private intAge ;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }}

MYJDBC Reverse Proxy simple frame

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.