ITeye博客

关于SS框架后台代码开发规范实例

小色帝 · 2013-02-01 17:20 · jquery · Web前端 · Markdown

工作半年了,接触的第一个项目就是SS为后台的框架的开发。当然项目组基本的与数据库JDBC链接的方法都封装好了,在这里 就总结下SS为后台代码的编写样例 1.domain: public class Employee extends BaseDomain{//这里的BaseDomain 是基类,里面只有一个ID,封装好的 private String name = null; public String getName() { return name; } public void setName(String name) { this.name = name; } }

2.DTO:数据传输,一般的与分页对象绑定在一起的 public class EmployeeListInfoDTO extends PaginationSupportDTO {//这里继承的是分页对象,封装好的 private List employeeList = null; public List getEmployeeList() { return employeeList; } public void setEmployeeList(List employeeList) { this.employeeList = employeeList; }

}

3.Condition:查询条件的类。按条件查询时带上这个对象参数,有什么条件,就往这个对象里面加就可以了,灰常的方便 public class EmployeeQueryCondition extends QueryCondition { private String employeeNames = null; private Date begin_birthDay = null; private Date end_birthDay = null; private String address = null; public String getEmployeeNames() { return employeeNames; } public void setEmployeeNames(String employeeNames) { this.employeeNames = employeeNames; } public Date getBegin_birthDay() { return begin_birthDay; } public void setBegin_birthDay(Date beginBirthDay) { begin_birthDay = beginBirthDay; } public Date getEnd_birthDay() { return end_birthDay; } public void setEnd_birthDay(Date endBirthDay) { end_birthDay = endBirthDay; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }

}

4.dao:基本的对数据库的增删改查操作 (1).接口 public interface EmployeeDAO { public Employee create(Employee employee); public void update(Employee employee); public void delete(String employeeId); public Employee query(String employeeId); public int queryEmployeeCountByCodition(EmployeeQueryCondition employeeQueryCondition); public List queryEmployeeListByCondition(EmployeeQueryCondition employeeQueryCondition, Pagination pagination);

} (2).接口的实现类 public class EmployeeDAOImpl extends JdbcBasedDao implements EmployeeDAO { public static final String CREATE_EMPLOYEE = "INSERT INTO WP_EMPLOYEE(ID,NAME,ADDRESS,EMAIL,BIRTHDAY,DESCRIPTION,CREATEDATE) VALUES(?,?,?,?,?,?,?)"; public static final String DELETE_EMPLOYEE = "DELETE FROM WP_EMPLOYEE WHERE ID =?"; public static final String QUERY_EMPLOYEE = "SELECT * FROM WP_EMPLOYEE E WHERE E.ID=?"; public static final String UPDATE_EMPLOYEE = "UPDATE WP_EMPLOYEE SET NAME=?,ADDRESS=?,EMAIL=?,BIRTHDAY=?,DESCRIPTION=?,CREATEDATE=? WHERE ID=?";

public Employee create(Employee employee) {
	Object params = new Object { employee.getId(), employee.getName(), employee.getAddress(),employee.getEmail(), employee.getBirthDay(), employee.getDescription(), employee.getCreateDate() };
	this.executeUpdate(CREATE_EMPLOYEE, params);
	return employee;
}

public void delete(String employeeId) {
	this.executeUpdate(DELETE_EMPLOYEE, new Object { employeeId });
}

public int queryEmployeeCountByCodition(EmployeeQueryCondition employeeQueryCondition) {
	StringBuffer query_employee_count = new StringBuffer("SELECT COUNT(1) FROM WP_EMPLOYEE E");
	ParameterList pmList = new ParameterList();
	// 构造查询条件
	this.buildEmployeeQueryCondition(employeeQueryCondition, query_employee_count, pmList);
	// 构造参数对象数组
	Object param = pmList.toArray();
	return this.queryDomainCount(query_employee_count.toString(), param);
}

public List<Employee> queryEmployeeListByCondition(EmployeeQueryCondition employeeQueryCondition,
		Pagination pagination) {
	StringBuffer query_employee_list = new StringBuffer(
			"SELECT ID,NAME,ADDRESS,EMAIL,BIRTHDAY,DESCRIPTION,CREATEDATE FROM WP_EMPLOYEE E ");
	ParameterList pmList = new ParameterList();
	// 构造查询条件
	this.buildEmployeeQueryCondition(employeeQueryCondition, query_employee_list, pmList);
	// 构造参数对象数组
	Object params = pmList.toArray();
	return this.queryDomainList(query_employee_list.toString(), params, pagination, new EmployeeRowMapper());
}

public void update(Employee employee) {
	Object params = new Object { employee.getName(), employee.getAddress(), employee.getEmail(),employee.getBirthDay(), employee.getDescription(), employee.getCreateDate(), employee.getId() };
	this.executeUpdate(UPDATE_EMPLOYEE, params);
}

public Employee query(String employeeId) {
	return this.queryDomain(QUERY_EMPLOYEE, new Object { employeeId }, new EmployeeRowMapper());
}

private void buildQuerySql(WikiTitleCondition condition, StringBuffer sql, ParameterList paramsList) { sql.append(" WHERE 1 = 1 "); if (!"".equals(StringUtils.defaultString(condition.getPartner_id()))) { sql.append(" AND( t.creator = ? OR p.PARTNER_ID=?)"); paramsList.add(SqlUtils.is(condition.getPartner_id())); paramsList.add(SqlUtils.is(condition.getPartner_id())); } if (!"".equals(StringUtils.defaultString(condition.getTitle()))) { sql.append(" AND t.TITLE LIKE ?"); paramsList.add(SqlUtils.like(condition.getTitle())); } if (!"".equals(StringUtils.defaultString(condition.getCreateor()))) { sql.append(" AND u.real_name LIKE ?"); paramsList.add(SqlUtils.like(condition.getCreateor())); } if (!"".equals(StringUtils.defaultString(condition.getCreateDateGT()))) { sql.append(" AND t.create_date >= ? "); paramsList.add(SqlUtils.is(condition.getCreateDateGT())); } if (!"".equals(StringUtils.defaultString(condition.getCreateDateLT()))) { sql.append(" AND Convert(varchar(10),t.create_date,111) <= Convert(varchar(10),?,111)"); paramsList.add(SqlUtils.is(condition.getCreateDateLT().replace("-", "/"))); } }

public class EmployeeRowMapper implements RowMapper { public Employee mapRow(ResultSet rs, int rowNum) throws SQLException { Employee employee = new Employee(); employee.setId(rs.getString("id")); employee.setName(rs.getString("name")); employee.setAddress(rs.getString("address")); employee.setEmail(rs.getString("email")); employee.setDescription(rs.getString("description")); employee.setBirthDay(rs.getDate("birthday")); employee.setCreateDate(rs.getDate("createdate")); return employee; }

}

}

5.BS层 (1).接口 public interface EmployeeBS { public EmployeeListInfoDTO getEmployeeList(EmployeeQueryCondition employeeQueryCondition, Pagination pagination) throws BizException; public Employee getEmployeeDetail(String employeeId) throws BizException;

public void modifyEmployee(Employee employee) throws BizException;

public void deleteEmployee(List<String> employeeIds) throws BizException;

public void createEmployee(Employee employee) throws BizException;

}

(2).实现类 public class EmployeeBSImpl extends BaseBizService implements EmployeeBS { private EmployeeDAO employeeDAO = null; public void setEmployeeDAO(EmployeeDAO employeeDAO) { this.employeeDAO = employeeDAO; } public void deleteEmployee(List employeeIds) throws BizException { for (String employeeId : employeeIds) { employeeDAO.delete(employeeId); }

}

public Employee getEmployeeDetail(String employeeId) throws BizException { return employeeDAO.query(employeeId); }

public EmployeeListInfoDTO getEmployeeList(EmployeeQueryCondition employeeQueryCondition, Pagination pagination) throws BizException { Validate.notNull(pagination, "Parameter 'pagination' can not be null"); int total = employeeDAO.queryEmployeeCountByCodition(employeeQueryCondition); pagination.setTotal(total); pagination.calculate(); List employeeList = employeeDAO.queryEmployeeListByCondition(employeeQueryCondition, pagination); EmployeeListInfoDTO dto = new EmployeeListInfoDTO(); dto.setEmployeeList(employeeList); dto.setPagination(pagination); return dto; }

public void modifyEmployee(Employee employee) throws BizException { Employee dbEmployee = employeeDAO.query(employee.getId()); if (dbEmployee == null) throw new EntityNotFoundException("员工信息不存在或者已经被删除"); BeanUtils.copyProperties(dbEmployee, employee, "name","email","description","birthDay"); employeeDAO.update(dbEmployee); }

public void createEmployee(Employee employee) throws BizException { Validate.notNull(employee, "Parameter 'employee' can not be null"); employee.setId(new UUIDGenerator().generate().toString()); employee.setCreateDate(new Date()); employeeDAO.create(employee); }

}

6.AC层 (1)基类:注入需要的BS层 public abstract class EmployeeBaseAction extends BaseAction { private static final long serialVersionUID = -8157097700651767069L; protected EmployeeBS employeeBS = null; public void setEmployeeBS(EmployeeBS employeeBS) { this.employeeBS = employeeBS; } } (2)查列表 public class ShowEmployeeListAC extends EmployeeBaseAction { private FrontPagination pagination = null; private EmployeeQueryCondition employeeQueryCondition = null; private List employeeList = null; public EmployeeQueryCondition getEmployeeQueryCondition() { return employeeQueryCondition; } public void setEmployeeQueryCondition(EmployeeQueryCondition employeeQueryCondition) { this.employeeQueryCondition = employeeQueryCondition; } public List getEmployeeList() { return employeeList; } public void setEmployeeList(List employeeList) { this.employeeList = employeeList; } public FrontPagination getPagination() { return pagination; } public void setPagination(FrontPagination pagination) { this.pagination = pagination; }

public String execute() throws Exception { Validate.notNull(pagination, "Parameter 'pagination' can not be null"); EmployeeListInfoDTO dto = employeeBS.getEmployeeList(employeeQueryCondition, pagination); employeeList = dto.getEmployeeList(); pagination = (FrontPagination) dto.getPagination();

	return SUCCESS;
}

}

(3)、新增、删除 (4)、前往修改的页面 public class ShowEmployeeDetailAC extends EmployeeBaseAction { private String employeeId = null; private Employee employee = null; private boolean success = false; public boolean getSuccess() { return success; } public String getEmployeeId() { return employeeId; } public void setEmployeeId(String employeeId) { this.employeeId = employeeId; } public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee = employee; } public void setSuccess(boolean success) { this.success = success; } public String execute() throws Exception { Validate.notNull(employeeId, "Parameter 'employeeId' can not be null"); employee = employeeBS.getEmployeeDetail(employeeId); success = true; return SUCCESS; } }

修改保存/新增: public class SaveEmployeeAC extends EmployeeBaseAction {

private static final long serialVersionUID = -7739724383305484103L;

private Employee employee = null;

private boolean success = false;

private MaintainType type = null;

public MaintainType getType() {
	return type;
}

public void setType(MaintainType type) {
	this.type = type;
}

public Employee getEmployee() {
	return employee;
}

public void setEmployee(Employee employee) {
	this.employee = employee;
}

public boolean getSuccess() {
	return success;
}

public void setSuccess(boolean success) {
	this.success = success;
}

public String execute() throws Exception {
	if(type.equals(MaintainType.CREATE)){
		employeeBS.createEmployee(employee);
	}else{
		employeeBS.modifyEmployee(employee);
	}
	success = true;
	return SUCCESS;
}

}

7.Spring配置 ,见附件

8.Strutsp配置,见附件

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。