java反射拼接自动生成sql语句,实现增删改,要求可以读任意类和任意属性生效
时间: 2023-08-05 20:05:53 浏览: 98
Java反射可以帮助我们在运行时获取类的信息,包括属性名、类型等,进而可以使用这些信息来生成SQL语句。
下面是一个示例代码,实现了根据任意类和任意属性名生成增删改SQL语句:
```java
import java.lang.reflect.Field;
public class SqlGenerator {
/**
* 生成插入语句
* @param obj 待插入的对象
* @param tableName 表名
* @return
*/
public static String generateInsertSql(Object obj, String tableName) {
Class<?> clazz = obj.getClass();
Field[] fields = clazz.getDeclaredFields();
StringBuilder sql = new StringBuilder("INSERT INTO " + tableName + "(");
for (int i = 0; i < fields.length; i++) {
sql.append(fields[i].getName());
if (i != fields.length - 1) {
sql.append(",");
}
}
sql.append(") VALUES (");
for (int i = 0; i < fields.length; i++) {
fields[i].setAccessible(true);
Object value = null;
try {
value = fields[i].get(obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (value == null) {
sql.append("NULL");
} else if (value instanceof String) {
sql.append("'" + value + "'");
} else {
sql.append(value);
}
if (i != fields.length - 1) {
sql.append(",");
}
}
sql.append(")");
return sql.toString();
}
/**
* 生成更新语句
* @param obj 待更新的对象
* @param tableName 表名
* @param pkName 主键名
* @return
*/
public static String generateUpdateSql(Object obj, String tableName, String pkName) {
Class<?> clazz = obj.getClass();
Field[] fields = clazz.getDeclaredFields();
StringBuilder sql = new StringBuilder("UPDATE " + tableName + " SET ");
for (int i = 0; i < fields.length; i++) {
fields[i].setAccessible(true);
if (fields[i].getName().equals(pkName)) {
continue;
}
sql.append(fields[i].getName() + "=");
Object value = null;
try {
value = fields[i].get(obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (value == null) {
sql.append("NULL");
} else if (value instanceof String) {
sql.append("'" + value + "'");
} else {
sql.append(value);
}
if (i != fields.length - 1) {
sql.append(",");
}
}
sql.append(" WHERE " + pkName + "=");
Field pkField = null;
try {
pkField = clazz.getDeclaredField(pkName);
pkField.setAccessible(true);
Object pkValue = pkField.get(obj);
if (pkValue instanceof String) {
sql.append("'" + pkValue + "'");
} else {
sql.append(pkValue);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
return sql.toString();
}
/**
* 生成删除语句
* @param clazz 类对象
* @param pkName 主键名
* @param pkValue 主键值
* @return
*/
public static String generateDeleteSql(Class<?> clazz, String pkName, Object pkValue) {
StringBuilder sql = new StringBuilder("DELETE FROM " + clazz.getSimpleName() + " WHERE " + pkName + "=");
if (pkValue instanceof String) {
sql.append("'" + pkValue + "'");
} else {
sql.append(pkValue);
}
return sql.toString();
}
}
```
使用示例:
```java
public class User {
private Integer id;
private String name;
private Integer age;
// getter and setter methods
}
public class Main {
public static void main(String[] args) {
User user = new User();
user.setId(1);
user.setName("Tom");
user.setAge(18);
String insertSql = SqlGenerator.generateInsertSql(user, "user_table");
System.out.println(insertSql);
String updateSql = SqlGenerator.generateUpdateSql(user, "user_table", "id");
System.out.println(updateSql);
String deleteSql = SqlGenerator.generateDeleteSql(User.class, "id", 1);
System.out.println(deleteSql);
}
}
```
输出结果:
```
INSERT INTO user_table(id,name,age) VALUES (1,'Tom',18)
UPDATE user_table SET name='Tom',age=18 WHERE id=1
DELETE FROM User WHERE id=1
```
需要注意的是,以上仅仅是一个简单的示例,实际使用中还需要考虑很多细节,比如空值、SQL注入等问题。
阅读全文