Preface
Words of enterprise applications, generally inseparable from the database. To do a database, there can be n scenarios, such as: directly using the JDBC layer in its own package, with some framework, such as: ibatis,hiberate,spring JDBC template and so on (this is too many, so not enumerated), These programs also show their own characteristics in their respective fields, solve a considerable part of the technical problems, and achieved a fairly good application effect.
But no matter what kind of solution, its advantages and disadvantages are often linked together, the reason is because the SQL and Java programming is fragmented, if the package is not in place, the Java people are too difficult to use, if the package too much, when doing some complex SQL is very cumbersome. Hibernate, for example, uses the encapsulation hql approach to solve this problem. Ibatis is good for SQL support, but there is a sense of fragmentation, but also in the resolution of the introduction of dynamic SQL to solve the problems that need to be handled according to some runtime conditions, to a certain extent, increase the complexity of the use.
So the question is, is there a better way to solve the problem in the database application development process? The root cause is how to solve the problem of the fragmentation between SQL and Java code in database development, if we can solve this problem, theoretically there will be a good solution.
We know that SQL is actually a domain of data for the DSL language, if we can write SQL directly in Java, and then execute the results can be directly returned to the Java object, this problem does not have a good solution?
TINYSQLDSL Solutions
In fact, there are some ready-made solutions, but some are not open source, and some support is not very in place, so leisurely decided to try to write, write a half-day time to see the effect, seerestful style of support practiceArticle, internal discussion, feel good, so formally decided to take the time to write a tinysqldsl, of course, the actual writing, there are a lot of problems, so that the final style and the above article there are some inconsistencies, of course, this is normal, easy to understand, Otherwise it would be too divine.
Our common SQL statements have Select, Insert, Update, Delete, so our scenario also implements how these statements are written.
First look at the TINYSQLDSL version of DAO is how to write.
First step: Define Pojo
- <strong>public class Custom {
- Private String ID;
- private String name;
- private int age;
- Public String getId () {
- return ID;
- }
- public void SetId (String id) {
- This.id = ID;
- }
- Public String GetName () {
- return name;
- }
- public void SetName (String name) {
- THIS.name = name;
- }
- public int getage () {
- return age;
- }
- public void Setage (int.) {
- This.age = age;
- }
- }
- </strong>
Copy Code
Step two: Define the table structure definition file
- public class Customtable extends Table {
- public static final Customtable CUSTOM = new customtable ();
- Public final Column ID = new column (This, "id");
- Public final column NAME = new column (This, "NAME");
- Public final column Age = new column (This, ' age ');
- Private Customtable () {
- Super ("Custom");
- }
- }
Copy Code
Step three: Writing the DAO class
- public class Customdao {
- Private Dslsession dslsession;
- Public Dslsession getdslsession () {
- return dslsession;
- }
- public void Setdslsession (Dslsession dslsession) {
- This.dslsession = dslsession;
- }
- public void Insertcustom (custom custom) {
- Dslsession.execute (
- Insertinto (CUSTOM). VALUES (
- CUSTOM. Id.value (Custom.getid ()),
- CUSTOM. Name.value (Custom.getname ()),
- CUSTOM. Age.value (Custom.getage ())
- )
- );
- }
- public void Updatecustom (custom custom) {
- Dslsession.execute (
- Update (CUSTOM). Set (
- CUSTOM. Name.value (Custom.getname ()),
- CUSTOM. Age.value (Custom.getage ())). WHERE (
- CUSTOM. Id.eq (Custom.getid ())
- )
- );
- }
- public void Deletecustom (String id) {
- Dslsession.execute (
- Delete (CUSTOM). WHERE (
- CUSTOM. Id.eq (ID)
- )
- );
- }
- Public Custom Getcustombyid (String ID) {
- Return Dslsession.fetchoneresult (
- Selectfrom (CUSTOM). WHERE (
- CUSTOM. Id.eq (ID)
- )
- , Custom.class);
- }
- Public list<custom> Querycustom (custom custom) {
- Return Dslsession.fetchlist (
- Selectfrom (CUSTOM). WHERE (
- and (
- CUSTOM. Id.eq (Custom.getid ()),
- CUSTOM. Name.equal (Custom.getname ()),
- CUSTOM. Age.equal (Custom.getage ())
- )
- )
- , Custom.class);
- }
- }
Copy Code
Look at the above example, will it feel a little strange, how can you write this? Oh, don't worry about the actual implementation mechanism, we first taste this DSL-style database writing method, um, specifically, like writing SQL in the same way to write SQL.
Write Java data applications like SQL-TINYSQLDSL