/**
* 获取Entity class注释的数据库表名
*
* @param clazz
* @return table name
*/
private static String getTableName(Class> clazz) {
String tableName = null;
if (clazz.isAnnotationPresent(Table.class)) {
Table table = clazz.getAnnotation(Table.class);
tableName = table.value();
}
return tableName;
}
/**
* 获取属性注释的列名
*
* @param field 属性
* @return column name
*/
private static String getColumnName(Field field) {
String colName = null;
if (field.isAnnotationPresent(Column.class)) {
Column column = field.getAnnotation(Column.class);
colName = column.value();
}
return colName;
}
/**
* 判断属性是否为id
*
* @param field 属性
* @return boolean
*/
private static boolean isId(Field field) {
boolean flag = false;
if (field.isAnnotationPresent(Id.class)) {
flag = true;
}
return flag;
}
/**
* 查询Entity对应数据库表所有数据
*
* @param clazz Entity
* @param sql queryString
* @param args parameters
* @return the result list
*/
private static List get(Class> clazz, String sql, Object... args) {
List arrList = new ArrayList<>();
Connection conn = DBHelper.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
Field[] fields = clazz.getDeclaredFields();
try {
pstmt = conn.prepareStatement(sql);
int len = (args == null) ? 0 : args.length;
for (int i = 0; i < len; ++i) {
pstmt.setObject(i + 1, args[i]);
}
rs = pstmt.executeQuery();
String colName;
while (rs.next()) {
Object ob = clazz.newInstance();
for (Field fi : fields) {
fi.setAccessible(true);
colName = getColumnName(fi);
if (colName == null) {
colName = fi.getName();
}
fi.set(ob, rs.getObject(colName));
}
arrList.add(ob);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.closeRes(conn, pstmt, rs);
}
return arrList;
}
/**
* 查询表所有数据
*
* @param clazz
* @return the result class
*/
public static List getList(Class> clazz) {
String tableName = getTableName(clazz);
if (tableName == null) {
tableName = clazz.getSimpleName();
}
String sql = "select * from " + tableName;
return get(clazz, sql);
}
/**
* 查询Entity对应数据库表所有数据
*
* @param clazz Entity
* @param sql queryString
* @param args parameters
* @return the result list
*/
public static List getListBySome(Class> clazz, String sql, Object... args) {
return get(clazz, sql, args);
}
/**
* 插入数据
*
* @param obj a persistent class
* @return boolean
*/
public static boolean save(Object obj) {
boolean flag = false;
Connection conn = DBHelper.getConnection();
PreparedStatement pstmt = null;
Class clazz = obj.getClass();
Field[] fields = clazz.getDeclaredFields();
String tableName = getTableName(clazz);
if (tableName == null) {
tableName = clazz.getSimpleName();
}
StringBuilder sb = new StringBuilder();
sb.append("insert into ");
sb.append(tableName);
sb.append(" (");
int len = fields.length;
int id = 0;
for (int i = 0; i < len; ++i) {
if (isId(fields[i])) {
id = i;
continue;
}
String colName = getColumnName(fields[i]);
if (colName == null) {
colName = fields[i].getName();
}
sb.append(colName);
if (i != len - 1) {
sb.append(" , ");
}
}
sb.append(") values (");
for (int i = 1; i < len; ++i) {
sb.append(" ? ");
if (i != len - 1) {
sb.append(" , ");
}
}
sb.append(")");
try {
pstmt = conn.prepareStatement(sb.toString());
for (int i = 0, j = 1; i < len; ++i) {
if (i == id) {
continue;
}
fields[i].setAccessible(true);
pstmt.setObject(j++, fields[i].get(obj));
}
int res = pstmt.executeUpdate();
if (res > 0) {
flag = true;
}
} catch (Exception sqle) {
sqle.printStackTrace();
} finally {
DBHelper.closeRes(conn, pstmt);
}
return flag;
}
/**
* 更新entity数据
*
* @param obj an entity class to update
* @return boolean
*/
public static boolean update(Object obj) {
boolean flag = false;
Connection conn = DBHelper.getConnection();
PreparedStatement pstmt = null;
Class clazz = obj.getClass();
Field[] fields = clazz.getDeclaredFields();
String tableName = getTableName(clazz);
if (tableName == null) {
tableName = clazz.getSimpleName();
}
StringBuilder sb = new StringBuilder();
sb.append("update ");
sb.append(tableName);
sb.append(" set ");
int len = fields.length;
String idName = "id";
int id = 0;
for (int i = 0; i < len; ++i) {
if (isId(fields[i])) {
id = i;
String tmp = getColumnName(fields[i]);
if (tmp != null) {
idName = tmp;
} else {
idName = fields[i].getName();
}
continue;
}
fields[i].setAccessible(true);
String colName = getColumnName(fields[i]);
if (colName == null) {
colName = fields[i].getName();
}
sb.append(colName);
sb.append(" = ? ");
if (i != len - 1) {
sb.append(", ");
}
}
sb.append(" where ");
sb.append(idName);
sb.append(" = ?");
try {
pstmt = conn.prepareStatement(sb.toString());
for (int i = 0, j = 1; i < len; ++i) {
if (i == id) {
continue;
}
fields[i].setAccessible(true);
pstmt.setObject(j++, fields[i].get(obj));
}
fields[id].setAccessible(true);
pstmt.setObject(len, fields[id].get(obj));
int res = pstmt.executeUpdate();
if (res > 0) {
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.closeRes(conn, pstmt);
}
return flag;
}
/**
* 更新数据库表数据某几项
*
* @param sql updateString
* @param args values
* @return boolean
*/
public static boolean updateSome(String sql, Object... args) {
boolean flag = false;
Connection conn = DBHelper.getConnection();
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0, len = args.length; i < len; ++i) {
pstmt.setObject(i + 1, args[i]);
}
int res = pstmt.executeUpdate();
if (res > 0) {
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.closeRes(conn, pstmt, null);
}
return flag;
}
/**
* 删除数据库表某几条记录
*
* @param tableName 表名
* @param name 属性名
* @param value 属性值
* @return boolean
*/
public static boolean delete(String sql) {
boolean flag = false;
Connection conn = DBHelper.getConnection();
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
int res = pstmt.executeUpdate();
if (res > 0) {
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBHelper.closeRes(conn, pstmt);
}
return flag;
}
/**
* 注释Entity对应的数据库表名
* Created by lezg on 15/11/21.
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
String value();
}
/**
* 注释Entity属性对应数据库的列名
* Created by lezg on 15/11/21.
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
String value();
}
/**
* id标识
* Created by lezg on 15/11/22.
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Id {
}