Dorado 5 : 9.Dorado5快速入门(二)附录 (T1B)

在使用CustomDataset维护部门表的时候,曾经使用到了DeptDao对象,该对象是本例中我们对Dao使用方式的一种简化包装。
本文中CustomDataset开发时涉及到一些其他的Java对象,列表如下:

相关的类列表

hr.Constants

hr.manage.dao.RowConverter

hr.manage.dao.SqlExecutor

hr.manage.dao.BaseDao

hr.manage.dao.DeptDao

hr.manage.dao.BranchDao

详细代码如下:

Constants.java

package hr;

/**
 * 常量
 */
public class Constants {
	// 数据源
	public static final String DATA_SOURCE = "doradosample";
}

RowConverter.java

package hr.manage.dao;

import java.sql.SQLException;

import com.bstek.dorado.utils.variant.VariantSet;

/**
 * 用来将VariantSet转换成domain的接口
 */
public interface RowConverter {
	public Object convert(VariantSet vs) throws SQLException;
}

SqlExecutor.java

package hr.manage.dao;

import hr.util.DoradoUtils;

import com.bstek.dorado.data.ParameterSet;
import com.bstek.dorado.data.db.DBStatement;

/**
 * 封装创建DBStatement,给statement的参数赋值和关闭DBStatement操作 具体是查询还是更新操作由子类实线
 * 
 * @param <K>
 */
public abstract class SqlExecutor {

	/**
	 * 公共操作(获取Statement和关闭之)
	 * 
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public Object execute(String sql) throws Exception {
		DBStatement stmt = DoradoUtils.getStatement(sql);
		try {
			ParameterSet p = stmt.parameters();
			return extract(stmt, p);
		} finally {
			stmt.close();
		}
	}

	/**
	 * 子类需要实现的CURD操作
	 * 
	 * @param stmt
	 * @param p
	 * @return
	 * @throws Exception
	 */
	public abstract Object extract(DBStatement stmt, ParameterSet p)
			throws Exception;
}

BaseDao.java

package hr.manage.dao;

import hr.Constants;

import java.util.ArrayList;
import java.util.List;

import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.utils.variant.VariantSet;

/**
 * Dao基类 定义一些基本的操作,如取得Dorado的DBStatement以及CRUD操作
 */
public abstract class BaseDao implements RowConverter {
	protected DBStatement getStatement() {
		DBStatement stmt = new DBStatement();
		stmt.setDataSource(Constants.DATA_SOURCE);
		return stmt;
	}

	protected DBStatement getStatement(String sql) {
		DBStatement stmt = getStatement();
		stmt.setSql(sql);
		return stmt;
	}

	protected DBStatement getStatement(String type, String tableName) {
		DBStatement stmt = getStatement();
		stmt.setSql(type, tableName);
		return stmt;
	}

	protected DBStatement getStatement(String type, String tableName,
			String keyFields) {
		DBStatement stmt = getStatement();
		stmt.setSql(type, tableName, keyFields);
		return stmt;
	}

	protected Object query(DBStatement stmt) throws Exception {
		VariantSet vs = stmt.query();
		return vs == null ? null : this.convert(vs);
	}

	protected List queryForList(DBStatement stmt) throws Exception {
		List result = new ArrayList();
		List list = stmt.queryForList();
		for (int i = 0; i < list.size(); i++) {
			VariantSet entity = (VariantSet) list.get(info);
			result.add(this.convert(entity));
		}
		return result;
	}

	protected List queryForIntList(DBStatement stmt, String fieldName)
			throws Exception {
		List result = new ArrayList();
		List list = stmt.queryForList();
		for (int i = 0; i < list.size(); i++) {
			VariantSet entity = (VariantSet) list.get(info);
			result.add(new Integer(entity.getInt(fieldName)));
		}
		return result;
	}
}

DeptDao.java

package hr.manage.dao;

import hr.manage.domain.Dept;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bstek.dorado.data.ParameterSet;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.utils.StringHelper;
import com.bstek.dorado.utils.variant.VariantSet;

/**
 * 部门CURD操作
 */
public class DeptDao extends BaseDao {

	public Object convert(VariantSet vs) throws SQLException {
		Dept dept = new Dept();
		dept.setDeptId(vs.getString("dept_id"));
		dept.setBranchId(vs.getString("branch_id"));
		dept.setDeptName(vs.getString("dept_name"));

		return dept;
	}

	/**
	 * 取得所有的dept列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public List getAll() throws Exception {

		String sql = "select * from dept";
		return (List) new SqlExecutor() {

			public Object extract(DBStatement stmt, ParameterSet p)
					throws Exception {
				return queryForList(stmt);
			}

		}.execute(sql);
	}

	/**
	 * 取得所有的dept列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public List getAll(ParameterSet parameters) throws Exception {
		String deptId = parameters.getString("deptId");
		String branchId = parameters.getString("branchId");
		String deptName = parameters.getString("deptName");

		DBStatement stmt = this.getStatement(DBStatement.SELECT, "dept");
		ParameterSet p = stmt.parameters();
		if (StringHelper.isNotEmpty(deptId)) {
			p.setString("dept_id", deptId);
		}
		if (StringHelper.isNotEmpty(branchId)) {
			p.setString("branch_id", branchId);
		}
		if (StringHelper.isNotEmpty(deptName)) {
			p.setString("dept_name", deptName);
		}

		List result = new ArrayList();
		List list = stmt.queryForList();
		for (int i = 0; i < list.size(); i++) {
			VariantSet entity = (VariantSet) list.get(info);
			result.add(this.convert(entity));
		}
		return result;

	}

	/**
	 * 修改部门信息
	 * 
	 * @param dept
	 */
	public void update(Dept dept) {
		// @todo
	}

	/**
	 * 删除部门信息
	 * 
	 * @param dept
	 */
	public void remove(Dept dept) {
		// @todo
	}

	/**
	 * 新增部门信息
	 * 
	 * @param dept
	 */
	public void save(Dept dept) {
		// @todo
	}
}

BranchDao.java

package hr.manage.dao;

import hr.manage.domain.Branch;

import java.sql.SQLException;
import java.util.List;

import com.bstek.dorado.data.ParameterSet;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.utils.variant.VariantSet;

/**
 * 部门CURD操作
 */
public class BranchDao extends BaseDao {

	public Object convert(VariantSet vs) throws SQLException {
		Branch branch = new Branch();
		branch.setBranchId(vs.getString("branch_id"));
		branch.setBranchName(vs.getString("branch_name"));

		return branch;
	}

	/**
	 * 取得所有的user列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public List getAll() throws Exception {
		String sql = "select * from branch";
		return (List) new SqlExecutor() {

			public Object extract(DBStatement stmt, ParameterSet p)
					throws Exception {

				return queryForList(stmt);
			}

		}.execute(sql);
	}

}

EmployeeDao.java

package hr.manage.dao;

import hr.manage.domain.Dept;
import hr.manage.domain.Employee;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bstek.dorado.data.ParameterSet;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.utils.StringHelper;
import com.bstek.dorado.utils.variant.VariantSet;

/**
 * 员工CURD操作
 */
public class EmployeeDao extends BaseDao {

	public Object convert(VariantSet vs) throws SQLException {
		Employee employee = new Employee();
		employee.setEmployeeId(vs.getString("employee_id"));
		employee.setEmployeeName(vs.getString("employee_id"));
		employee.setSex(vs.getBoolean("sex"));
		employee.setBirthday(vs.getDate("birthday"));
		employee.setMarried(vs.getBoolean("married"));
		employee.setSalary(vs.getFloat("salary"));
		employee.setDegree(vs.getString("degree"));
		employee.setEmail(vs.getString("email"));
		employee.setWeb(vs.getString("web"));
		employee.setCmnt(vs.getString("cmnt"));
		return employee;
	}

	/**
	 * 取得所有的employee列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public List getAll() throws Exception {

		String sql = "select * from employee";
		return (List) new SqlExecutor() {

			public Object extract(DBStatement stmt, ParameterSet p)
					throws Exception {
				return queryForList(stmt);
			}

		}.execute(sql);
	}

	/**
	 * 取得所有的employee列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public List getAll(ParameterSet parameters) throws Exception {
		String deptId = parameters.getString("deptId");

		List result = new ArrayList();
		DBStatement stmt = this.getStatement(DBStatement.SELECT, "employee");
		try {
			ParameterSet p = stmt.parameters();
			if (StringHelper.isNotEmpty(deptId)) {
				p.setString("dept_id", deptId);
			}

			List list = stmt.queryForList();

			for (int i = 0; i < list.size(); i++) {
				VariantSet entity = (VariantSet) list.get(info);
				result.add(this.convert(entity));
			}
		} finally {
			stmt.close();
		}
		return result;

	}

	/**
	 * 修改员工信息
	 * 
	 * @param dept
	 */
	public void update(Employee employee) {
		// @todo
	}

	/**
	 * 删除员工信息
	 * 
	 * @param dept
	 */
	public void remove(Employee employee) {
		// @todo
	}

	/**
	 * 新增员工信息
	 * 
	 * @param dept
	 */
	public void save(Employee employee) {
		// @todo
	}
}


(完)