package com.tntxia.db.dao;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Map;
import com.tntxia.db.util.DBUtil;
public class BaseDao {
public String dbSource;
public String getDbSource() {
return dbSource;
}
public void setDbSource(String dbSource) {
this.dbSource = dbSource;
}
/**
* 获取实体的ID值
*
* @param entity
* @return
*/
public String getIdField(Object entity) {
String idField = null;
try {
Field[] fields = entity.getClass()
.getDeclaredFields();
System.out.println(fields.length);
Annotation[] annotations;
for (Field field : fields) {
annotations = field.getDeclaredAnnotations();
for (Annotation a : annotations) {
if (a.annotationType().getSimpleName().equals("Id")) {
idField = field.getName();
}
}
}
} catch (Exception ex) {
}
return idField;
}
/**
* 获取实体的ID值
*
* @param entity
* @return
*/
public String getIncrement(Object entity) {
String idField = null;
try {
Field[] fields = entity.getClass()
.getDeclaredFields();
System.out.println(fields.length);
Annotation[] annotations;
for (Field field : fields) {
annotations = field.getDeclaredAnnotations();
for (Annotation a : annotations) {
if (a.annotationType().getSimpleName().equals("Increment")) {
idField = field.getName();
}
}
}
} catch (Exception ex) {
}
return idField;
}
// 查看实体有多少个
public int getCount(Object entity) {
Field[] fields = entity.getClass().getDeclaredFields();
String sqlWhere = "";
ArrayList<String> cols = new ArrayList<String>();
cols.add("c");
String sql = "";
String className = entity.getClass().getSimpleName();
Class cl = entity.getClass();
boolean firstFlag = true;
for (int i = 0; i < fields.length; i++) {
String fieldName = fields[i].getName();
String methodName = "get" + fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1, fieldName.length());
try {
if (cl.getMethod(methodName).invoke(entity) != null) {
if (firstFlag) {
sqlWhere += " where " + fieldName + "='"
+ cl.getMethod(methodName).invoke(entity) + "'";
firstFlag = false;
} else {
sqlWhere += " and " + fieldName + "='"
+ cl.getMethod(methodName).invoke(entity) + "'";
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
sql += "select count(*) as c from " + className + sqlWhere;
Map<String, String> result = DBUtil.getSingleResult(cols, sql);
// 判断
if (result.get("c") != null)
return Integer.valueOf(result.get("c"));
else
return 0;
}
/**
* 对象查询,输入对应表名的对象,分页大小,当前页数和排序类型
*
* @param entity
* @param pageNo
* @param pageSize
* @param orderType
* @return
* @throws Exception
*/
public Map<String, ArrayList<String>> query(Object entity, int pageNo,
int pageSize, String orderType) {
Field[] fields = entity.getClass().getDeclaredFields();
String sqlSelect = "";
String sqlWhere = "";
String sql = "";
Class cl = entity.getClass();
boolean firstFlag = true;
ArrayList<String> cols = new ArrayList<String>();
String id = this.getIdField(entity);
String className = entity.getClass().getSimpleName();
for (int i = 0; i < fields.length; i++) {
String fieldName = fields[i].getName();
if (i != fields.length - 1) {
sqlSelect += fieldName + ",";
} else {
sqlSelect += fieldName;
}
cols.add(fieldName);
String methodName = "get" + fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1, fieldName.length());
try {
if (cl.getMethod(methodName).invoke(entity) != null) {
if (firstFlag) {
sqlWhere += " where " + fieldName + "='"
+ cl.getMethod(methodName).invoke(entity) + "'";
firstFlag = false;
} else {
sqlWhere += " and " + fieldName + "='"
+ cl.getMethod(methodName).invoke(entity) + "'";
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (pageNo == 1) {
sqlWhere += " order by " + id + " " + orderType;
sql = "select top " + pageSize + " " + sqlSelect + " from "
+ className + sqlWhere;
} else {
String sqlGetBottom = "";
String sqlTopAll = "";
sqlTopAll = "select top " + (pageNo * pageSize) + " " + sqlSelect
+ " from " + className + sqlWhere + " order by " + id + " "
+ orderType;
if (orderType.equalsIgnoreCase("desc")) {
sqlGetBottom = "select Min(" + id + ") from (select top "
+ ((pageNo - 1) * pageSize) + " " + id + " from "
+ className + " " + sqlWhere + " order by " + id + " "
+ orderType + ") as T1";
sql = "select * from (" + sqlTopAll + ") as T where " + id
+ "<(" + sqlGetBottom + ") order by " + id + " "
+ orderType;
} else {
sqlGetBottom = "select Max(" + id + ") from (select top "
+ ((pageNo - 1) * pageSize) + " " + id + " from "
+ className + " " + sqlWhere + " order by " + id + " "
+ orderType + ") as T1";
sql = "select * from (" + sqlTopAll + ") as T where " + id
+ ">(" + sqlGetBottom + ") order by " + id + " "
+ orderType;
}
System.out.println("sqlGetBottom:" + sqlGetBottom);
}
System.out.println("the final sql :" + sql);
Map<String, ArrayList<String>> result = DBUtil
.getArrayResult(cols, sql);
return result;
}
/**
* 对象查询,不分页
*
* @param entity
* @param pageNo
* @param pageSize
* @param orderType
* @return
* @throws Exception
*/
public Map<String, ArrayList<String>> query(Object entity) {
Field[] fields = entity.getClass().getDeclaredFields();
String sqlSelect = "";
String sqlWhere = "";
String sql = "";
Class cl = entity.getClass();
boolean firstFlag = true;
ArrayList<String> cols = new ArrayList<String>();
String id = this.getIdField(entity);
String className = entity.getClass().getSimpleName();
for (int i = 0; i < fields.length; i++) {
String fieldName = fields[i].getName();
if (i != fields.length - 1) {
sqlSelect += fieldName + ",";
} else {
sqlSelect += fieldName;
}
cols.add(fieldName);
String methodName = "get" + fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1, fieldName.length());
try {
if (cl.getMethod(methodName).invoke(entity) != null) {
if (firstFlag) {
sqlWhere += " where " + fieldName + "='"
+ cl.getMethod(methodName).invoke(entity) + "'";
firstFlag = false;
} else {
sqlWhere += " and " + fieldName + "='"
+ cl.getMethod(methodName).invoke(entity) + "'";
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
sql = "select " + sqlSelect + " from " + className + sqlWhere;
System.out.println("the final sql :" + sql);
Map<String, ArrayList<String>> result = DBUtil
.getArrayResult(cols, sql);
return result;
}
public void update(Object entity) {
Field[] fields = entity.getClass().getDeclaredFields();
Class cl = entity.getClass();
boolean firstFlag = true;
ArrayList<String> params = new ArrayList<String>();
String id = this.getIdField(entity);
String className = entity.getClass().getSimpleName();
String sqlUpdate = "";
String idValue = null;
for (int i = 0; i < fields.length; i++) {
String fieldName = fields[i].getName();
String methodName = "get" + fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1, fieldName.length());
if (!fieldName.equals(id)) {
if (i != fields.length - 1) {
sqlUpdate += fieldName + "=? , ";
} else {
sqlUpdate += field