Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
*
* @author xingwx
* @version $Id: $Id
*/
public abstract class BaseDao implements RowMapper, IBaseListService,
SimpleDaoInterface {
/**
* Constant SELECT_FROM="select * from "
*/
public static final String SELECT_FROM = "select * from ";
/**
* Constant WHERE=" where "
*/
protected static final String WHERE = " where ";
/**
* Constant LOG
*/
protected static final Logger LOG = LoggerFactory.getLogger(BaseDao.class);
private static final String SELECT_COUNT_FROM = "select count(*) from ";
private static final Set> BUILT_IN_SET = new HashSet<>();
static {
ConvertUtils.register(new DateConverter(null), Date.class);
ConvertUtils.register(new SqlDateConverter(null), java.sql.Date.class);
ConvertUtils.register(new SqlTimestampConverter(null), Timestamp.class);
ConvertUtils.register(new SqlTimeConverter(null), java.sql.Time.class);
ConvertUtils.register(new BooleanConverter(null), Boolean.class);
}
static {
BUILT_IN_SET.add(Integer.class);
BUILT_IN_SET.add(Long.class);
BUILT_IN_SET.add(Double.class);
BUILT_IN_SET.add(Float.class);
BUILT_IN_SET.add(Boolean.class);
BUILT_IN_SET.add(Character.class);
BUILT_IN_SET.add(Byte.class);
BUILT_IN_SET.add(Short.class);
}
protected final PojoValidator pojoValidator = new PojoValidator<>();
public AfterChangeEventHandler getAfterChangeEventHandler() {
return afterChangeEventHandler;
}
public void setAfterChangeEventHandler(AfterChangeEventHandler afterChangeEventHandler) {
this.afterChangeEventHandler = afterChangeEventHandler;
}
AfterChangeEventHandler afterChangeEventHandler;
protected IDbHelper dbHelper = null;
private int dbtype = UNKNOWN;
@Override
public String getDatabaseName() {
return databaseName;
}
private String databaseName;
/**
*
Constructor for BaseDao.
*
* @param dbHelper a {@link IDbHelper} object.
*/
public BaseDao(IDbHelper dbHelper) {
this.dbHelper = dbHelper;
}
public BaseDao(DataSource ds) {
this(ds, ds);
}
public BaseDao(DataSource master, DataSource slave) {
MSDbHelper dbHelper0 = new MSDbHelper();
dbHelper0.setMasterDatasource(master);
dbHelper0.setDataSource(slave);
if (LOG.isDebugEnabled()) {
ProxyFactory obj = new ProxyFactory(dbHelper0);
obj.addAdvice(new DbHelperAroundAdvice());
dbHelper = (IDbHelper) obj.getProxy();
} else {
dbHelper = dbHelper0;
}
}
/**
*
Constructor for BaseDao.
*/
public BaseDao() {
}
/**
*
sql.
*
* @param src a {@link java.lang.String} object.
* @return a {@link SqlString} object.
*/
protected static SqlString sql(String src) {
return new SqlString(src);
}
/**
*
fields.
* Comma separated column names, used for select statement when you don't want return all fields,
* in case to optimize the index usage.
*
* @param fields a {@link java.lang.String} object.
* @return an array of {@link java.lang.String} objects.
*/
protected static String[] fields(String... fields) {
return fields;
}
/**
*
params.
*
* @param params a {@link java.lang.Object} object.
* @return an array of {@link java.lang.Object} objects.
*/
protected static Object[] params(Object... params) {
return params;
}
/**
* MS SQL Server 2000
*
* @param selectSql 不带分页的基本语句
* @param startNum 起始行,第一页从零开始
* @param endNum 结束行,每页数+startNum
* @return 完整分页SQL
*/
private static String getPagingSqlSqlServer(String selectSql, int startNum, int endNum) {
return SqlServer2000PageHepler.getLimitString(selectSql, startNum, endNum - startNum + 1);
}
/**
*
makeQuotedStr.
*
* @param s a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public static String makeQuotedStr(String s) {
return "'" + SQLUtil.escapeString(s) + "'";
}
/**
*
detectDbType.
*
* @param driverName a {@link java.lang.String} object.
* @return a int.
*/
public static int detectDbType(String driverName) {
int res = UNKNOWN;
String name = driverName.toUpperCase();
if (name.contains("ORACLE")) {
res = ORACLE;
} else if (name.contains("DERBY")) {
res = DERBY;
} else if (name.contains("MYSQL")) {
res = MYSQL;
} else if (name.contains("SQLSERVER")) {
// sqlserver 2000
res = SQLSERVER;
} else if (name.contains("SQL SERVER")) {
// sql server 2005
res = SQLSERVER_2005_AND_UP;
} else if (name.contains("POSTGRESQL")) {
res = POSTGRESQL;
} else if (name.contains("H2")) {
res = H2;
} else if (name.contains("ACCESS")) {
res = ACCESS;
}
return res;
}
/**
* MS SQL Server 2005 and up
*
* @param selectSql 不带分页的基本语句
* @param startNum 起始行,第一页从零开始
* @param endNum 结束行,每页数+startNum
* @return 完整分页SQL
*/
public static String getPagingSqlSqlServer2005(String selectSql, int startNum, int endNum) {
return SqlServer2005PageHepler.getLimitString(selectSql, startNum, endNum);
}
/**
* Paging with PostgreSQL is super easy! select * from table where (x=y)
* offset 0 limit 30; Gives you the first 30 matches, then do select * from
* table where (x=y) offset 30 limit 30; This will give the next 30, super
* easy!
*
* @param selectSql 不带分页的基本语句
* @param startNum 起始行,第一页从零开始
* @param endNum 结束行,每页数+startNum
* @return 完整分页SQL
*/
private static String getPagingSqlPostgresql(String selectSql, int startNum, int endNum) {
return selectSql + " offset " + startNum + " limit " + (endNum - startNum);
}
/**
* Oracle
*
* @param selectSql 不带分页的基本语句
* @param startNum 起始行,第一页从零开始
* @param endNum 结束行,每页数+startNum
* @return 完整分页SQL
*/
private static String getPagingSqlOracle(String selectSql, int startNum, int endNum) {
return SELECT_FROM + "(select FR.*,ROWNUM RN from (" + selectSql + ") FR where rownum<=" + endNum
+ ") where RN>" + startNum;
}
/**
* sql2005
*
* WITH, ROW_NUMBER (行数)and OVER
*
* 这对SQL Server
* 2005来说非常新鲜并且看上去非常有用。下面一个例子显示从一个结果集得到20至19条记录。刚开始有一点惊奇,但是浏览了查询器后发现它是如此简单。
*
*
* With Customer AS ( SELECT CustomerID, CompanyName, ROW_NUMBER() OVER (order
* by CompanyName) as RowNumber FROM Customers ) select * from Customer Where
* RowNumber Between 20 and 30
*
* SQL Server 2005的WITH指定了一个临时命名的结果,很像SQL
* Server以前版本中的临时表。但是,输入部分是ROW_NUMBER和OVER声明
* ,它根据公司的名称在每组中创建行数。这就像通过命令条文向临时表添加一个身份种子。
*
* DB2 上用 FETCH FIRST n ROW ONLY 代替可行啦,即是
*
* SELECT tRANGE.* FROM (SELECT tTOP.* FROM (SELECT * FROM t ORDER BY k ASC
* FETCH FIRST endNum ROW ONLY) tTOP ORDER BY tTOP.k DESC FETCH FIRST
* (endNum-startNum+1) ROW ONLY) tRANGE ORDER BY tRANGE.k ASC
*
* @param selectSql 不带分页的基本语句
* @param startNum 起始行,第一页从零开始
* @param endNum 结束行,每页数+startNum
* @return 完整分页SQL
*/
private static String getPagingSqlMySql(String selectSql, int startNum, int endNum) {
return selectSql + " LIMIT " + startNum + "," + (endNum - startNum);
}
/**
* SELECT * FROM ( SELECT ROW_NUMBER() OVER() AS rownum, myLargeTable.* FROM
* myLargeTable ) AS tmp WHERE rownum > 200000 AND rownum <= 200005;
*
* @param selectSql 不带分页的基本语句
* @param startNum 起始行,第一页从零开始
* @param endNum 结束行,每页数+startNum
* @return 完整分页SQL
*/
private static String getPagingSqlDerby(String selectSql, int startNum, int endNum) {
String orderBy = selectSql.indexOf("order by") > 0 ? selectSql.substring(selectSql.indexOf("order by")) : "";
selectSql = selectSql.indexOf("order by") > 0 ? selectSql.substring(0, selectSql.indexOf("order by")) : selectSql;
return SELECT_FROM + "(select FR.*,ROW_NUMBER() OVER() AS RN from (" + selectSql
+ ")as FR " + orderBy + ") as ttt where RN<=" + endNum + " and RN>" + startNum;
}
private static String getDateSqlDerby(String dt) {
return "date('" + dt + "')";
}
private static String getTimestampSqlDerby(String dt) {
return "TIMESTAMP('" + dt + "')";
}
private static String getDateSqlOracle(String dt) {
return "to_date('" + dt + "','yyyy-mm-dd')";
}
private static String getDateSqlMySql(String dt) {
return "'" + dt + "'";
}
private static String getDateSqlServer(String dt) {
return "'" + dt + "'";
}
private static String getDateSqlDerby(java.util.Date dt) {
return "date('" + formatDate(dt) + "')";
}
/**
* 使用SimpleDateFormat需要同步才行 For more information on this see Sun Bug
* #6231579 and Sun Bug #6178997.
*
* @param dt 日期
* @return 格式化的日期字符串
*/
private static String formatDate(Date dt) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
return fmt.format(dt);
}
private static String getDateSqlOracle(Date dt) {
return "to_date('" + formatDate(dt) + "','yyyy-mm-dd')";
}
private static String getDateSqlPostgresql(Date dt) {
return "to_date('" + formatDate(dt) + "','YYYY-MM-DD')";
}
private static String getDateSqlMySql(Date dt) {
return "'" + formatDate(dt) + "'";
}
private static String getDateSqlServer(Date dt) {
return "'" + formatDate(dt) + "'";
}
/**
*
registerConverters.
*
* @param convertors a {@link java.util.Map} object.
*/
public static void registerConverters(Map convertors) {
BeanUtilsBean b = BeanUtilsBean.getInstance();
for (Entry entry : convertors.entrySet()) {
b.getConvertUtils().register(entry.getValue(), entry.getKey());
}
}
/**
*
filterForSQL.
*
* @param sql a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public static String filterForSql(String sql) {
if (sql == null || sql.length() == 0) {
return "";
}
int nLen = sql.length();
char[] srcBuff = sql.toCharArray();
StringBuilder retBuff = new StringBuilder((int) (nLen * 1.5D));
for (int i = 0; i < nLen; ++i) {
char cTemp = srcBuff[i];
switch (cTemp) {
case '\'':
retBuff.append("''");
break;
case ';':
boolean bSkip = false;
for (int j = i + 1; (j < nLen) && (!(bSkip)); ++j) {
char cTemp2 = srcBuff[j];
if (cTemp2 == ' ') {
continue;
}
if (cTemp2 == '&') {
retBuff.append(';');
}
bSkip = true;
}
if (!(bSkip)) {
retBuff.append(';');
}
break;
default:
retBuff.append(cTemp);
}
}
return retBuff.toString();
}
/**
* contain field check
*
* @param field
* @param fields
* @return true if fields is empty or contains field, ignore
*/
protected static boolean containsIgnoreCase(String field, Set fields) {
return fields == null || fields.isEmpty() || fields.contains(getFieldAlias(field).toLowerCase(Locale.ROOT));
}
/**
* deal with alias and `` quotes
*
* @param field
* @return
*/
public static String getFieldAlias(String field) {
field = field.strip();
if (!field.startsWith("`") && StringUtils.containsWhitespace(field)) {
StringTokenizer tokenizer = new StringTokenizer(field);
while (tokenizer.hasMoreTokens()) {
field = tokenizer.nextToken();
}
} else if (field.startsWith("`")) {
field = StringUtils.strip("`");
}
return field;
}
public List getAllFields() {
return emptyList();
}
/**
*
beforeChange.
*
* @param info a T object.
*/
protected void beforeChange(T info) {
pojoValidator.asureValid(info);
if (info instanceof IModifiedTime) {
((IModifiedTime) info).setModifiedTime(new Timestamp(
System.currentTimeMillis()
));
}
}
private void beforeChange(T info, long time) {
pojoValidator.asureValid(info);
if (info instanceof IModifiedTime) {
((IModifiedTime) info).setModifiedTime(new Timestamp(
time
));
}
}
private void beforeChange(T[] infos) {
long time = System.currentTimeMillis();
for (T info : infos) {
beforeChange(info, time);
}
}
/**
*
Getter for the field dbHelper.
*
* @return a {@link IDbHelper} object.
*/
@Override
public IDbHelper getDbHelper() {
return dbHelper;
}
/**
*
create.
*
* @return a T object.
*/
@Override
public T create() {
return null;
}
/**
*
getCount.
*
* @param tableName a {@link java.lang.String} object.
* @param whereClause a {@link java.lang.String} object.
* @return a int.
*/
public int getCount(String tableName, String whereClause) {
String strSql = SELECT_COUNT_FROM + addAlias(tableName) + " " + whereClause;
if (this.getDbType() == ORACLE) {
return ((BigDecimal) getDbHelper().executeScalar(strSql)).intValue();
} else {
return Integer.parseInt(getDbHelper().executeScalar(strSql).toString());
}
}
/**
*
getPagingSql.
*
* @param selectSql a {@link java.lang.String} object.
* @param startNum a int.
* @param endNum a int.
* @return a {@link java.lang.String} object.
*/
public String getPagingSql(String selectSql, int startNum, int endNum) {
detectDbType();
if (dbtype == DERBY) {
return getPagingSqlDerby(selectSql, startNum, endNum);
} else if (dbtype == SQLSERVER) {
return getPagingSqlSqlServer(selectSql, startNum, endNum);
} else if (dbtype == SQLSERVER_2005_AND_UP) {
return getPagingSqlSqlServer2005(selectSql, startNum, endNum);
} else if (dbtype == MYSQL || dbtype == H2) {
return getPagingSqlMySql(selectSql, startNum, endNum);
} else if (dbtype == POSTGRESQL) {
return getPagingSqlPostgresql(selectSql, startNum, endNum);
} else if (dbtype == ACCESS) {
return getPagingSqlSqlServer(selectSql, startNum, endNum);
} else {
return getPagingSqlOracle(selectSql, startNum, endNum);
}
}
private String getDateTimeSql(String dt, boolean isTimestamp) {
detectDbType();
if (dbtype == DERBY) {
if (isTimestamp) {
return getTimestampSqlDerby(dt);
}
return getDateSqlDerby(dt);
} else if (dbtype == SQLSERVER || dbtype == SQLSERVER_2005_AND_UP || dbtype == ACCESS) {
return getDateSqlServer(dt);
} else if (dbtype == MYSQL) {
return getDateSqlMySql(dt);
} else {
return getDateSqlOracle(dt);
}
}
/**
*
getDateSql.
*
* @param dt a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public String getDateSql(String dt) {
return getDateTimeSql(dt, false);
}
/**
*
getTimestampSql.
*
* @param dt a {@link java.lang.String} object.
* @return a {@link java.lang.String} object.
*/
public String getTimestampSql(String dt) {
return getDateTimeSql(dt, true);
}
/**
*
*
* @return a int.
*/
public int getDbType() {
detectDbType();
return dbtype;
}
/**
*
getKeyName.
*
* @return a {@link java.lang.String} object.
*/
public abstract String getKeyName();
/**
* {@inheritDoc}
*
* Counts the number of entries for this table in the database.
*/
@Override
public int count(String where) {
return getCount(getTableName(), where);
}
/**
*
count.
*
* @return a int.
*/
public int count() {
return getCount(getTableName(), "");
}
/**
*
count.
*
* @param sql a {@link java.lang.String} object.
* @param params a {@link java.lang.Object} object.
* @return a int.
*/
public int count(String sql, Object[] params) {
Object o = this.getDbHelper().executeScalar(sql, params);
return o == null ? 0 : Integer.parseInt(o.toString());
}
/**
*
exists.
*
* @param where a {@link java.lang.String} object.
* @param params a {@link java.lang.Object} object.
* @return a boolean.
*/
public boolean exists(String where, Object[] params) {
Object o = this.getDbHelper().executeScalar("select 1 from " + getTableName() + " " + where, params);
return o != null && Integer.parseInt(o.toString()) == 1;
}
public boolean exists(String tableName, String where, Object[] params) {
Object o = this.getDbHelper().executeScalar("select 1 from " + getTableName(tableName) + " " + where, params);
return o != null && Integer.parseInt(o.toString()) == 1;
}
/**
*