SSM框架实现审批功能代码
时间: 2024-05-01 10:23:30 浏览: 100
基于SSM的OA办公系统的设计与实现+毕业论文
5星 · 资源好评率100%
以下是SSM框架实现审批功能的代码示例:
1. 审批表(approval)的建表语句
```sql
CREATE TABLE `approval` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '审批ID',
`apply_user_id` int(11) NOT NULL COMMENT '申请人ID',
`apply_time` datetime NOT NULL COMMENT '申请时间',
`approval_user_id` int(11) NOT NULL COMMENT '审批人ID',
`approval_time` datetime NOT NULL COMMENT '审批时间',
`status` int(11) NOT NULL COMMENT '审批状态:1-待审批,2-已通过,3-已拒绝',
`remark` varchar(255) DEFAULT NULL COMMENT '审批备注',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='审批表';
```
2. 审批实体类(Approval)
```java
public class Approval {
private Integer id;
private Integer applyUserId;
private Date applyTime;
private Integer approvalUserId;
private Date approvalTime;
private Integer status;
private String remark;
// 省略getter和setter方法
}
```
3. 审批DAO层接口(ApprovalDao)
```java
public interface ApprovalDao {
/**
* 添加审批记录
* @param approval
*/
void addApproval(Approval approval);
/**
* 更新审批记录
* @param approval
*/
void updateApproval(Approval approval);
/**
* 根据ID查询审批记录
* @param id
* @return
*/
Approval getApprovalById(Integer id);
/**
* 查询待审批的记录
* @param approvalUserId
* @return
*/
List<Approval> listApprovalsToBeApproved(Integer approvalUserId);
}
```
4. 审批DAO层实现类(ApprovalDaoImpl)
```java
@Repository
public class ApprovalDaoImpl implements ApprovalDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void addApproval(Approval approval) {
String sql = "INSERT INTO approval(apply_user_id, apply_time, approval_user_id, status, remark) " +
"VALUES(?, ?, ?, ?, ?)";
jdbcTemplate.update(sql, approval.getApplyUserId(), approval.getApplyTime(),
approval.getApprovalUserId(), approval.getStatus(), approval.getRemark());
}
@Override
public void updateApproval(Approval approval) {
String sql = "UPDATE approval SET approval_time=?, status=?, remark=? WHERE id=?";
jdbcTemplate.update(sql, approval.getApprovalTime(), approval.getStatus(),
approval.getRemark(), approval.getId());
}
@Override
public Approval getApprovalById(Integer id) {
String sql = "SELECT * FROM approval WHERE id=?";
return jdbcTemplate.queryForObject(sql, new Object[] {id}, new ApprovalMapper());
}
@Override
public List<Approval> listApprovalsToBeApproved(Integer approvalUserId) {
String sql = "SELECT * FROM approval WHERE approval_user_id=? AND status=1";
return jdbcTemplate.query(sql, new Object[] {approvalUserId}, new ApprovalMapper());
}
private static final class ApprovalMapper implements RowMapper<Approval> {
@Override
public Approval mapRow(ResultSet rs, int rowNum) throws SQLException {
Approval approval = new Approval();
approval.setId(rs.getInt("id"));
approval.setApplyUserId(rs.getInt("apply_user_id"));
approval.setApplyTime(rs.getTimestamp("apply_time"));
approval.setApprovalUserId(rs.getInt("approval_user_id"));
approval.setApprovalTime(rs.getTimestamp("approval_time"));
approval.setStatus(rs.getInt("status"));
approval.setRemark(rs.getString("remark"));
return approval;
}
}
}
```
5. 审批服务层接口(ApprovalService)
```java
public interface ApprovalService {
/**
* 添加审批记录
* @param approval
*/
void addApproval(Approval approval);
/**
* 审批通过
* @param id
*/
void approve(Integer id);
/**
* 审批拒绝
* @param id
* @param remark
*/
void reject(Integer id, String remark);
/**
* 根据ID查询审批记录
* @param id
* @return
*/
Approval getApprovalById(Integer id);
/**
* 查询待审批的记录
* @param approvalUserId
* @return
*/
List<Approval> listApprovalsToBeApproved(Integer approvalUserId);
}
```
6. 审批服务层实现类(ApprovalServiceImpl)
```java
@Service
public class ApprovalServiceImpl implements ApprovalService {
@Autowired
private ApprovalDao approvalDao;
@Override
public void addApproval(Approval approval) {
// 设置申请时间和待审批状态
approval.setApplyTime(new Date());
approval.setStatus(1);
approvalDao.addApproval(approval);
}
@Override
public void approve(Integer id) {
Approval approval = getApprovalById(id);
// 设置审批时间和已通过状态
approval.setApprovalTime(new Date());
approval.setStatus(2);
approvalDao.updateApproval(approval);
}
@Override
public void reject(Integer id, String remark) {
Approval approval = getApprovalById(id);
// 设置审批时间、已拒绝状态和审批备注
approval.setApprovalTime(new Date());
approval.setStatus(3);
approval.setRemark(remark);
approvalDao.updateApproval(approval);
}
@Override
public Approval getApprovalById(Integer id) {
return approvalDao.getApprovalById(id);
}
@Override
public List<Approval> listApprovalsToBeApproved(Integer approvalUserId) {
return approvalDao.listApprovalsToBeApproved(approvalUserId);
}
}
```
这样,我们就完成了一个简单的审批功能的实现。在实际应用中,还需要根据具体业务需求进行进一步的开发和优化。
阅读全文