Last active
November 17, 2015 22:42
-
-
Save benelog/51df14d10802bbdb592b to your computer and use it in GitHub Desktop.
simple example
This file contains hidden or 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
import org.apache.commons.lang.StringUtils | |
class SqlMap { | |
public static String buildSelectSql(User user) { | |
StringBuilder sql = new StringBuilder(); | |
sql.append(""" | |
SELECT name, address | |
FROM user | |
WHERE 1=1 | |
""") | |
isNotEmpty(user.getName(), sql, """ | |
AND name = :name | |
"""); | |
isNotEmpty(user.getAddress(), sql, """ | |
AND address = :address | |
"""); | |
return sql.toString(); | |
} | |
private static isNotEmpty(String param, StringBuilder sql, String part) { | |
if(StringUtils.isNotEmpty(param) { | |
sql.append(part); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment