Last active
May 12, 2017 08:51
-
-
Save drinkjava2/c2d302c1f2c0f0f6147b81151ff3d838 to your computer and use it in GitHub Desktop.
A demo shows how to let Java support multiple line Strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here is a method to let Java suppor multiple line Strings, and to make it run on product server, need copy source code to | |
class path and change ".java" to ".sql" suffix: | |
https://github.com/drinkjava2/jSqlBox/blob/master/jsqlbox/src/test/java/test/examples/multipleLineSQL/SqlTemplateDemo.java | |
This demo shows how to use Java file as the SQL template and inject sql parameters by use Threadlocal. | |
public class SqlTemplateDemo extends TestBase { | |
//@formatter:off | |
public static class InsertUser extends SqlTemplate { | |
public InsertUser(Object name, Object address, Object age){ | |
/* insert into | |
users | |
(username, address, age) */ | |
empty(name,address,age); | |
/* values (?,?,?)*/ | |
} | |
} | |
public static class FindUsers extends SqlTemplate { | |
public FindUsers(Object name, Object age){ | |
/* select count(*) | |
from | |
users | |
where */ | |
/* username=? */empty(name); | |
/* and age>? */empty(age); | |
/* order by username */ | |
} | |
} | |
public static class GetUserCount extends SqlTemplate { | |
/* select count(*) | |
from users */ | |
} | |
public static class SqlTemplateEndTag{} | |
@Test | |
public void doTest() { | |
Dao.getDefaultContext().setShowSql(true); | |
Dao.executeInsert(new InsertUser("Tom","BeiJing",10).toString()); | |
Dao.executeInsert(new InsertUser("Sam","ShangHai",20).toString()); | |
Assert.assertEquals((Integer) 1, Dao.queryForInteger(new FindUsers("Sam",15).toString())); | |
Assert.assertEquals((Integer) 2, Dao.queryForInteger(new GetUserCount().toString())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment