`

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

 
阅读更多
   工作半年了,接触的第一个项目就是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<Employee> employeeList = null;
public List<Employee> getEmployeeList() {
return employeeList;
}
public void setEmployeeList(List<Employee> 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<Employee> queryEmployeeListByCondition(EmployeeQueryCondition employeeQueryCondition, Pagination pagination);
   
}
(2).接口的实现类
public class EmployeeDAOImpl extends JdbcBasedDao<Employee> 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<Employee> {
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<String> 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<Employee> 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<Employee> employeeList = null;
public EmployeeQueryCondition getEmployeeQueryCondition() {
return employeeQueryCondition;
}
public void setEmployeeQueryCondition(EmployeeQueryCondition employeeQueryCondition) {
this.employeeQueryCondition = employeeQueryCondition;
}
public List<Employee> getEmployeeList() {
return employeeList;
}
public void setEmployeeList(List<Employee> 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配置,见附件
分享到:
评论

相关推荐

    ss2h框架系统搭建

    SS2H框架是一个基于Java Web技术的开发框架,主要用于简化页面开发流程。在这个"ss2h框架系统搭建"中,我们将探讨如何集成和使用这个框架,以便于构建高效、可维护的Web应用。SS2H可能指的是Spring+Struts2+...

    PHP实例开发源码——sspanel前端框架 php版.zip

    PHP实例开发源码——sspanel前端框架 php版.zip

    SS2H框架纯净版

    SS2H框架是Java开发中的一个经典组合,由Spring、Struts2和Hibernate三大开源框架集成,常用于构建企业级的Web应用。本压缩包"SS2H框架纯净版"包含的是Spring 4.0、Hibernate 4.2和Struts2的一个精简配置,便于...

    PHP实例开发源码—sspanel前端框架 php版.zip

    PHP实例开发源码—sspanel前端框架 php版.zip

    J2EE开发ss2h框架JAR包整合

    在Java企业级应用开发中,J2EE标准提供了一个强大的平台来构建可扩展且高度模块化的应用程序。...通过理解并熟练掌握这三个框架的集成,开发者可以大大提高开发效率,同时保证代码的可读性和可维护性。

    SS系列常用三极管Multisim仿真实例

    在Multisim提供的SS系列三极管仿真实例中,你可能遇到诸如音频放大器、比较器、开关电源等应用场景。通过这些实例,你可以深入理解三极管在实际电路中的作用,提升电路设计技能。 总结,掌握SS系列三极管在Multisim...

    ss52ss ss52ss ss52ss ss52ss ss52ss

    如果您能提供关于这个主题的更多上下文,如"ss52ss"代表的是"SQL Server 52nd Security Update"或"SSH Secure Shell"等,那么我可以生成一篇关于这些话题的详细文章。 如果您想要了解某个特定的IT领域,比如云计算...

    SS2H框架模板

    在实际开发中,SS2H框架模板能够帮助开发者快速搭建项目结构,减少重复代码,更专注于业务逻辑的实现。同时,由于它包含了数据库操作、文件上传、分页显示和移动通信等常见功能的示例,对于学习和理解SSH框架及其...

    SSSP整合及分页开发

    SSSP(Spring、Spring MVC、Spring Data和Spring JPA)整合是Java开发中常见的技术栈,主要用于构建高效、模块化的Web应用程序。这个整合方案利用了Spring框架的强大功能,结合Spring MVC处理HTTP请求,Spring Data...

    三大框架ss2h整合(一)

    在IT行业中,SSH(Struts2、Spring、Hibernate)被称为三大框架,是Java Web开发中的核心组件,常用于构建高效、可维护的企业级应用程序。本教程将深入探讨如何通过纯XML配置来整合这三个框架,这对于初学者理解MVC...

    js时间控件实例代码

    2. Bootstrap Datetimepicker:Bootstrap是一个流行的前端开发框架,提供了一套响应式设计和移动设备优先的Web组件。其datetimepicker插件是专门用于日期和时间选择的组件,它可以集成到Bootstrap的输入框中,提供...

    基于SS2H的注册的简单应用实例

    在Web开发中,SS2H框架是一个常见的选择,它由三个主要部分组成:Struts2、Spring和Hibernate。Struts2是MVC(Model-View-Controller)设计模式的一个实现,Spring提供了依赖注入和事务管理等功能,而Hibernate则是...

    一个课堂签到微信小程序源码,后台使用ssm+mysql开发.zip

    这是一个课堂签到微信小程序,后台使用ssm框架+mysql开发,可以进行 限时签到,密码签到,手势签到,位置签到。 这是一个课堂签到微信小程序,后台使用ssm框架+mysql开发,可以进行 限时签到,密码签到,手势签到...

    sssp框架的增删改查

    SSSP框架,全称为Spring-SpringMVC-SpringData-JPA的组合,是Java开发中常见的一种Web应用程序架构。这个框架集合了Spring的核心特性、SpringMVC的控制器机制、SpringData的数据访问抽象以及JPA(Java Persistence ...

    SS7.rar_protocols ss7_ss7_ss7 协议_七号信令

    在本压缩包文件中,"SS7.rar_protocols ss7_ss7_ss7 协议_七号信令"显然包含了关于SS7协议的详细资料,对于那些正在开发或研究SS7技术的人来说,是非常宝贵的学习资源。 SS7协议的核心是消息传递部分(MTP),它...

    java ss2h框架增删改查

    Java SS2H 框架,全称是 Spring Security、Spring MVC 和 Hibernate 的组合,是一种常用的Web开发架构,尤其在企业级应用中广泛使用。它整合了Spring的强大功能,包括依赖注入、AOP(面向切面编程)、MVC模式以及...

Global site tag (gtag.js) - Google Analytics