All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.j2mvc.framework.dao.DaoSupport Maven / Gradle / Ivy

Go to download

强烈建议使用J2mvc 2.1以后的版本。 version 2.1.01 1.优化路径跳转,Servlet和Filter方式的路径设置 2.优化内存销毁 3.更换JSON依赖包 4.优化接收方法RequestMethod,封装不同ContentType格式提交 封装JSON,XML数据提交模块 JSON请求示例 { "id":"JSON134851", "title":"JSON提交的标题", "price":65.1, "stock":100 } XML请求示例 <!DOCTYPE root [ <!ELEMENT root ANY> <!ATTLIST Product SSN ID #REQUIRED>]> <root> <Product SSN='id'>XMLID12354</Product> <Product SSN='title'>XML提交的标题 </Product> <Product SSN='price'>55 </Product> <Product SSN='stock'>32 </Product> </root> version 2.1.02 1.解决URL无后缀情况无法加载静态资源,解决无法渲染CSS文件。 version 2.1.03 1.移除com.j2mvc.StringUtils.getUtf8()方法调用 更改为getCharset() version 2.1.04 1.去除Servlet和Filter的全局变量销毁,只交给Listener处理。 version 2.1.05,2.1.06,2.1.07 1.完善POST提交的JSON数据 支持接收基础数据类型、任意对象类型、任意数组类型。 不支持接收参数为集合类型或Map类型,但可以定义为接收对象类型的元素。 version 2.1.05,2.1.06,2.1.07 1.修改连接池变量 version 2.1.09 增加上传功能,修改RequestMethod,ContentType设置方式 version 2.1.10,2.1.11 更改上传文件名格式为UUID格式,移除JSON映射类,更改接收多文件上传。 version 2.1.12 删除文件列有的空对象 version 2.1.13 增加配置文件目录/conf,加载上传功能配置/conf/upload.properties version 2.1.18 拦截器也能获取ActionBean version 2.1.20 添加上传文件只读权限 version 2.1.21 支持同时接收文件和文本数据 version 2.1.22 增加文件接收类型media version 2.1.23 删除upload类printJson方法

The newest version!
package com.j2mvc.framework.dao;

import java.io.InputStream;
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.CallbackInputStream;
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.framework.mapping.Column;
import com.j2mvc.framework.mapping.DataSourceName;
import com.j2mvc.framework.mapping.PrimaryKey;
import com.j2mvc.framework.mapping.Table;
import com.j2mvc.framework.mapping.Transient;
import com.j2mvc.framework.util.FieldUtil;

/**
 * 数据层访问
 * 
 * 传入实体类名,实现实体类关联数据表增、删、改、查。
 * 
 * 2014-3-29 创建@杨朔
 */
public class DaoSupport extends JdbcDaoSupport implements Serializable{
	private static final long serialVersionUID = -6480408267186862188L;

	static final Logger log = Logger.getLogger(DaoSupport.class);

	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);
		if(primaryKey == null)
			log.error("主键未设置");
		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 相对应的值
	 */
	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 相对应的值
	 */
	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
	 */
	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
	 */
	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;
		if(keyColumn == null)
			log.error("未在"+clazz+"类声明位置注解primaryKey,"
					+ "如:@PrimaryKey(name = \"id\",  autoIncrement = false).");
		if(table == null)
			log.error("未在"+clazz+"内注解Table,如:@Table(\"users\")");
		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);
	}

	/**
	 * 预编译语句查询输入流
	 * @param sql 预编译语句
	 * @param params 相对应的值
	 * @return InputStream
	 */
	public InputStream inputStream(String sql,Object[] params){
		Creator creator = new Creator(sql,params);
		CallbackInputStream callback = new CallbackInputStream(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 
	 * @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);
	}

	/**
	 * 返回所有
	 * @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
	 */
	public List queryOnlyInteger(String sql){
		if(!check())
			return null;
		Creator creator = new Creator(sql,null);
		CallbackList callback = new CallbackList(Integer.class,dataSourceName);
		return execute(creator,callback);
	}

	/**
	 * 获取数值
	 * @param sql
	 * @return Integer
	 */
	public Integer number(String sql){
		Creator creator = new Creator(sql,null);
		CallbackInteger callback = new CallbackInteger(PreparedStatementCallBack.EXECUTE_QUERY);
		return execute(creator,callback);
	}

	/**
	 * 获取单字段
	 * @param sql
	 * @return Integer
	 */
	public String string(String sql){
		Creator creator = new Creator(sql,null);
		CallbackString callback = new CallbackString(PreparedStatementCallBack.EXECUTE_QUERY);
		return execute(creator,callback);
	}

	/**
	 * 预编译语句查询字符
	 * @param sql 预编译语句
	 * @param params 相对应的值
	 * @return Integer
	 */
	public String string(String sql,Object[] params){
		Creator creator = new Creator(sql,params);
		CallbackString callback = new CallbackString(PreparedStatementCallBack.EXECUTE_QUERY);
		return execute(creator,callback);
	}

	/**
	 * 预编译语句查询字符列表
	 * @param sql 预编译语句
	 * @param params 相对应的值
	 * @return Integer
	 */
	public List queryArray(String sql,Object[] params){
		Creator creator = new Creator(sql,params);
		CallbackArrayList callback = new CallbackArrayList();
		return execute(creator,callback);
	}
}