com.j2mvc.framework.dao.DaoSupport Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of j2mvc-framework-app Show documentation
Show all versions of j2mvc-framework-app Show documentation
强烈建议使用J2mvc 2.1以后的版本。
version 2.1.01
1.更换JSON依赖包.
version 2.1.02
1.移除com.j2mvc.StringUtils.getUtf8()方法调用.
更改为getCharset()
version 2.1.03
1.更新JNDI连接设置
version 2.1.04
1.修改works.xml配置url-pkg-prefixes改为pkg
package com.j2mvc.framework.dao;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.List;
import org.apache.log4j.Logger;
import com.j2mvc.framework.dao.callback.CallbackArrayList;
import com.j2mvc.framework.dao.callback.CallbackInteger;
import com.j2mvc.framework.dao.callback.CallbackList;
import com.j2mvc.framework.dao.callback.CallbackObject;
import com.j2mvc.framework.dao.callback.CallbackString;
import com.j2mvc.framework.dao.callback.Creator;
import com.j2mvc.framework.dao.callback.MutilCreator;
import com.j2mvc.framework.dao.callback.ObjectFieldsValue;
import com.j2mvc.framework.dao.callback.PreparedStatementCallBack;
import com.j2mvc.util.mapping.Column;
import com.j2mvc.util.mapping.DataSourceName;
import com.j2mvc.util.mapping.PrimaryKey;
import com.j2mvc.util.mapping.Table;
import com.j2mvc.util.mapping.Transient;
import com.j2mvc.util.FieldUtil;
/**
* 数据层访问
*
* 传入实体类名,实现实体类关联数据表增、删、改、查。
*
* 2014-3-29 创建@杨朔
*/
public class DaoSupport extends JdbcDaoSupport implements Serializable{
private static final long serialVersionUID = -6480408267186862188L;
Logger log = Logger.getLogger(getClass().getName());
private Class> clazz;
private Table table;
private PrimaryKey primaryKey;
private Column keyColumn;
public DaoSupport() {
super();
}
public DaoSupport(String dataSourceName) {
this.dataSourceName = dataSourceName;
}
public DaoSupport(Class> clazz){
DataSourceName dsn = clazz.getAnnotation(DataSourceName.class);
if(dsn!=null){
this.dataSourceName = dsn.value();
}
this.clazz = clazz;
initialize();
}
public DaoSupport(Class> clazz,String dataSourceName) {
this.clazz = clazz;
this.dataSourceName = dataSourceName;
initialize();
}
private void initialize(){
// 表名
table = clazz.getAnnotation(Table.class);
// 主键
primaryKey = clazz.getAnnotation(PrimaryKey.class);
keyColumn = FieldUtil.getColumn(primaryKey.name(), clazz);
}
/**
* 生成插入语句
* @return String
*/
private String getInsertSql(){
String insertSql = "";
String fields_sql = "";
String values_sql = "";
List fields = FieldUtil.getFields(null,clazz);
for(int i=0;i fields = FieldUtil.getFields(null,clazz);
for(int i=0;i0){
return object;
}else{
return null;
}
}
/**
* 预编译语句更新
* @param sql 预编译语句
* @param params 相对应的值
* @return Integer
*/
public Integer update(String sql,Object[] params){
Creator creator = new Creator(sql,params);
CallbackInteger callback = new CallbackInteger(PreparedStatementCallBack.EXECUTE_UPDATE);
return execute(creator,callback);
}
/**
* 预编译语句执行
* @param sql 预编译语句
* @param params 相对应的值
* @return Integer
*/
public Integer execute(String sql,Object[] params){
Creator creator = new Creator(sql,params);
CallbackInteger callback = new CallbackInteger(PreparedStatementCallBack.EXECUTE_UPDATE);
return execute(creator,callback);
}
/**
* 执行多条语句
* @param sqls
*
*/
public int[] execute(List sqls){
if(sqls!=null && sqls.size()>0)
return new MutilCreator(sqls,dataSourceName).execute();
else {
return null;
}
}
/**
* 根据主键删除
* @param key
* @return Integer
*/
public Integer delete(Object key){
if(!check())
return 0;
Creator creator = new Creator(getDeleteSql(),new Object[]{key});
CallbackInteger callback = new CallbackInteger(PreparedStatementCallBack.EXECUTE_UPDATE);
return execute(creator,callback);
}
/**
* 根据主键删除
* @param key
* @return Integer
*/
public Integer delete(Object...key){
if(!check())
return 0;
if(!(key!=null && key.length>0))
return 0;
String sql = "DELETE FROM " + table.value() + " WHERE " + "`" + keyColumn.name() + "`" + " in(";
for(int i=0;i0?",?":"?";
}
sql += ")";
Creator creator = new Creator(sql,key);
CallbackInteger callback = new CallbackInteger(PreparedStatementCallBack.EXECUTE_UPDATE);
return execute(creator,callback);
}
/**
* 根据主键获取指定条目
* @param key
* @return Object
*/
public Object get(Object key){
if(!check())
return null;
String sql = "SELECT * FROM " + table.value() + " WHERE " + "`" + keyColumn.name() + "`" + "=?";
Object[] params ={key};
Creator creator = new Creator(sql,params);
CallbackObject callback = new CallbackObject(PreparedStatementCallBack.EXECUTE_QUERY,clazz,dataSourceName);
return execute(creator,callback);
}
/**
* 预编译语句查询列表
* @param preSql 预编译语句
* @param params 相对应的值
* @return List>
*/
public List> query(String preSql,Object[] params){
if(!check())
return null;
Creator creator = new Creator(preSql,params);
CallbackList callback = new CallbackList(clazz,dataSourceName);
return execute(creator,callback);
}
/**
* 预编译语句查询对象
* @param preSql 预编译语句
* @param params 相对应的值
* @return Object
*/
public Object get(String preSql,Object[] params){
if(!check())
return null;
List> list = query(preSql, params);
return list!=null && list.size()>0?list.get(0):null;
}
/**
* 预编译语句查询数量
* @param sql 预编译语句
* @param params 相对应的值
* @return Integer
*/
public Integer number(String sql,Object[] params){
Creator creator = new Creator(sql,params);
CallbackInteger callback = new CallbackInteger(PreparedStatementCallBack.EXECUTE_QUERY);
return execute(creator,callback);
}
/**
* 根据SQL查询,返回LIST集合
* @param sql
* @return List>
*/
public List> query(String sql){
if(!check())
return null;
Creator creator = new Creator(sql,null);
CallbackList callback = new CallbackList(clazz,dataSourceName);
return execute(creator,callback);
}
/**
* 根据SQL查询查询对象
* @param sql
* @param params
* @return Object
*/
public Object queryForObject(String sql){
if(!check())
return null;
List> list = query(sql);
return list!=null && list.size()>0?list.get(0):null;
}
/**
* 根据SQL查询查询对象
* @param sql
* @param params
* @return Object
*/
public Object queryForObject(String sql,Object[] params){
if(!check())
return null;
List> list = query(sql,params);
return list!=null && list.size()>0?list.get(0):null;
}
/**
* 返回所有
* @param sorterField 排序字段
* @param esc 是否顺序,否则倒序
* @return List>
*/
public List> all(String sorterField,boolean esc){
if(!check())
return null;
String sql = "SELECT * FROM " + table.value() + " order by " + "`" + sorterField + "`" + (!esc ? " desc" : "");
Creator creator = new Creator(sql,null);
CallbackList callback = new CallbackList(clazz,dataSourceName);
return execute(creator,callback);
}
/**
* 返回所有
* @param sql
* @return List>
*/
public List> all(){
if(!check())
return null;
String sql = "SELECT * FROM "+table.value();
Creator creator = new Creator(sql,null);
CallbackList callback = new CallbackList(clazz,dataSourceName);
return execute(creator,callback);
}
/**
* 根据SQL查询,返回LIST集合
* @param sql
* @return List