A method of supporting multi-line string using Java annotations

Source: Internet
Author: User
Tags assert

From the Beetlsql project, the SQL is all put in the Beetl template to get inspired, but also think of a more biased use of the door. The following code is measured by, see Jsqlbox Project Test\examples\multiplelinesql\sqltemplatedemo.java source program, this example special point is: The runtime must copy this source program in the class root directory ( or Maven managed resource directory), and replace the suffix ". Java" with ". SQL"

PublicClass Sqltemplatedemo Extends Testbase{@formatter: OffPublicStaticClass Insertuser Extends Sqltemplate{PublicInsertuser(Object name,Object address,Object age) {/* INSERT into users (username, address, age) */empty (name,address,age);/* VALUES (?,?,?) */ }}PublicStaticClass Findusers Extends Sqltemplate{PublicFindusers(Object name,Object age) {/* Select COUNT (*) from users where *//* username=? */empty (name);/* and age>? */empty (age);/* ORDER BY Username */}}PublicStaticClass Getusercount Extends Sqltemplate{/* Select COUNT (*) from Users */}PublicStaticClass sqltemplateendtag{}@TestPublicvoidDotest() {Dao.executeinsert (NewInsertuser ("Tom," "Beijing", 10). toString ()); dao.executeinsert (new InsertUser ( Span class= "hljs-string" > "Sam",  "Shanghai", 20). toString ()); Span class= "Hljs-type" >assert.assertequals ((integer) 1, dao.queryforinteger (new FindUsers ( "Sam", 15). ToString ())); assert.assertequals ((integer) 2, dao.queryforinteger (new GetUserCount (). ToString ())); } } 

The code above is to use the Java source program as a template file for unified SQL placement for DBA management. The advantages are:

1) without introducing a third-party template, use Java directly as a template file
2) Support multi-line string, and do not introduce IDE plug-in or maven compile plug-in, etc., good versatility.
3) Support Java method and template mix, template is responsible for storing Sql,java is responsible for assigning values (using threadlocal), do not work.
4) The class name of public static class is the SQL ID, which makes it easy to rename the refactoring and SQL Quick positioning (CTRL + left mouse button), which is the text mode template cannot do, the latter must use the text search function to locate SQL.
The disadvantages are:
1) After each template change, you must also manually copy a copy of the same file in the class root as a resource file
2) Because SQL is written in comments, you must use the tag//@formatter: Off to turn off the formatting of the IDE (I use Eclipse) to prevent the IDE from mis-formatting.
3) from the security point of view, the template file should be careful not to put anything other than SQL.

The above method is mainly used in SQL templates, but other occasions if you need to use Java support multi-line string, you can also borrow.

A method of supporting multi-line string using Java annotations

Related Article

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.