java sqlhelper_SQLHelper 代码细节

SQLHelper 具有如下功能:

用于获取 sql.properties 配置文件中定义的 SQL 语句(模板)。

生成基于单表的 SQL 语句。

以下是代码细节:

public class SQLHelper {

private static final Properties sqlProperties = FileUtil.loadPropertiesFile("sql.properties");

public static String getSQL(String key) {

String value = "";

if (sqlProperties.containsKey(key)) {

value = sqlProperties.getProperty(key);

} else {

System.err.println("Can not get property [" + key + "] in sql.properties file.");

}

return value;

}

public static String generateSelectSQL(Class> cls, String condition, Object... params) {

StringBuilder sql = new StringBuilder("select * from ");

String tableName = getTableName(cls);

if (StringUtil.isNotEmpty(tableName)) {

sql.append(tableName).append(" ");

}

if (StringUtil.isNotEmpty(condition)) {

if (ArrayUtil.isNotEmpty(params)) {

condition = getWhereCondition(condition, params);

}

sql.append("where ").append(condition);

}

return sql.toString();

}

public static String generateInsertSQL(Class> cls, Map fieldMap) {

StringBuilder sql = new StringBuilder("insert into ");

String tableName = getTableName(cls);

if (StringUtil.isNotEmpty(tableName)) {

sql.append(tableName).append(" ");

}

if (MapUtil.isNotEmpty(fieldMap)) {

int i = 0;

StringBuilder columns = new StringBuilder();

StringBuilder values = new StringBuilder(" values ");

for (Map.Entry fieldEntry : fieldMap.entrySet()) {

String columnName = StringUtil.toUnderline(fieldEntry.getKey());

Object columnValue = fieldEntry.getValue();

if (i == 0) {

columns.append("(").append(columnName);

values.append("('").append(columnValue).append("'");

} else if (i == fieldMap.size() - 1) {

columns.append(", ").append(columnName).append(")");

values.append(", '").append(columnValue).append("')");

} else {

columns.append(", ").append(columnName);

values.append(", '").append(columnValue).append("'");

}

i++;

}

sql.append(columns).append(values);

}

return sql.toString();

}

public static String generateDeleteSQL(Class> cls, String condition, Object... params) {

StringBuilder sql = new StringBuilder("delete from ");

String tableName = getTableName(cls);

if (StringUtil.isNotEmpty(tableName)) {

sql.append(tableName).append(" ");

}

if (StringUtil.isNotEmpty(condition)) {

if (ArrayUtil.isNotEmpty(params)) {

condition = getWhereCondition(condition, params);

}

sql.append(" where ").append(condition);

}

return sql.toString();

}

public static String generateUpdateSQL(Class> cls, Map fieldMap, String condition, Object... params) {

StringBuilder sql = new StringBuilder("update ");

String tableName = getTableName(cls);

if (StringUtil.isNotEmpty(tableName)) {

sql.append(tableName).append(" ");

}

if (MapUtil.isNotEmpty(fieldMap)) {

sql.append("set ");

int i = 0;

for (Map.Entry fieldEntry : fieldMap.entrySet()) {

String columnName = StringUtil.toUnderline(fieldEntry.getKey());

Object columnValue = fieldEntry.getValue();

if (i == 0) {

sql.append(columnName).append(" = '").append(columnValue).append("'");

} else {

sql.append(", ").append(columnName).append(" = '").append(columnValue).append("'");

}

i++;

}

}

if (StringUtil.isNotEmpty(condition)) {

if (ArrayUtil.isNotEmpty(params)) {

condition = getWhereCondition(condition, params);

}

sql.append(" where ").append(condition);

}

return sql.toString();

}

private static String getTableName(Class> cls) {

String tableName;

if (cls.isAnnotationPresent(Table.class)) {

tableName = cls.getAnnotation(Table.class).value();

} else {

tableName = cls.getSimpleName().toLowerCase();

}

return tableName;

}

private static String getWhereCondition(String condition, Object... params) {

StringBuffer buffer = new StringBuffer();

Matcher matcher = Pattern.compile("\\?").matcher(condition);

for (int i = 0; matcher.find(); i++) {

matcher.appendReplacement(buffer, "'" + params[i].toString() + "'");

}

matcher.appendTail(buffer);

return buffer.toString();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值