Write Java data applications like SQL-TINYSQLDSL

Source: Internet
Author: User

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

    1. <strong>public class Custom {
    2. Private String ID;
    3. private String name;
    4. private int age;
    5. Public String getId () {
    6. return ID;
    7. }
    8. public void SetId (String id) {
    9. This.id = ID;
    10. }
    11. Public String GetName () {
    12. return name;
    13. }
    14. public void SetName (String name) {
    15. THIS.name = name;
    16. }
    17. public int getage () {
    18. return age;
    19. }
    20. public void Setage (int.) {
    21. This.age = age;
    22. }
    23. }
    24. </strong>
Copy Code

Step two: Define the table structure definition file

    1. public class Customtable extends Table {
    2. public static final Customtable CUSTOM = new customtable ();
    3. Public final Column ID = new column (This, "id");
    4. Public final column NAME = new column (This, "NAME");
    5. Public final column Age = new column (This, ' age ');
    6. Private Customtable () {
    7. Super ("Custom");
    8. }
    9. }
Copy Code


Step three: Writing the DAO class

  1. public class Customdao {
  2. Private Dslsession dslsession;
  3. Public Dslsession getdslsession () {
  4. return dslsession;
  5. }
  6. public void Setdslsession (Dslsession dslsession) {
  7. This.dslsession = dslsession;
  8. }
  9. public void Insertcustom (custom custom) {
  10. Dslsession.execute (
  11. Insertinto (CUSTOM). VALUES (
  12. CUSTOM. Id.value (Custom.getid ()),
  13. CUSTOM. Name.value (Custom.getname ()),
  14. CUSTOM. Age.value (Custom.getage ())
  15. )
  16. );
  17. }
  18. public void Updatecustom (custom custom) {
  19. Dslsession.execute (
  20. Update (CUSTOM). Set (
  21. CUSTOM. Name.value (Custom.getname ()),
  22. CUSTOM. Age.value (Custom.getage ())). WHERE (
  23. CUSTOM. Id.eq (Custom.getid ())
  24. )
  25. );
  26. }
  27. public void Deletecustom (String id) {
  28. Dslsession.execute (
  29. Delete (CUSTOM). WHERE (
  30. CUSTOM. Id.eq (ID)
  31. )
  32. );
  33. }
  34. Public Custom Getcustombyid (String ID) {
  35. Return Dslsession.fetchoneresult (
  36. Selectfrom (CUSTOM). WHERE (
  37. CUSTOM. Id.eq (ID)
  38. )
  39. , Custom.class);
  40. }
  41. Public list<custom> Querycustom (custom custom) {
  42. Return Dslsession.fetchlist (
  43. Selectfrom (CUSTOM). WHERE (
  44. and (
  45. CUSTOM. Id.eq (Custom.getid ()),
  46. CUSTOM. Name.equal (Custom.getname ()),
  47. CUSTOM. Age.equal (Custom.getage ())
  48. )
  49. )
  50. , Custom.class);
  51. }
  52. }
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

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.