基于Java+springmvc+mysql+jquery实现企业员工管理系统
以下是一个简化的员工管理系统的核心功能实现,包括员工列表展示和添加员工的基本过程。
数据库设计(MySQL):
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
实体类(Java):
public class Employee {
private int id;
private String name;
private String email;
// 省略getter和setter方法
}
Mapper接口(Java):
public interface EmployeeMapper {
void insert(Employee employee);
List<Employee> findAll();
}
Service层(Java):
@Service
public class EmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
public void addEmployee(Employee employee) {
employeeMapper.insert(employee);
}
public List<Employee> getAllEmployees() {
return employeeMapper.findAll();
}
}
控制器(Java):
@Controller
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public ModelAndView list() {
ModelAndView mav = new ModelAndView("employeeList");
mav.addObject("employees", employeeService.getAllEmployees());
return mav;
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addEmployee(@ModelAttribute Employee employee) {
employeeService.addEmployee(employee);
return "redirect:/employee/list";
}
}
JSP页面(employeeList.jsp):
<html>
<head>
<title>员工列表</title>
</head>
<body>
<h1>员工列表</h1>
<table>
<tr>
<th>ID</th>
<th>姓名</th>
<th>邮箱</th>
</tr>
<c:forEach var="employee" items="${employees}">
<tr>
<td>${employee.id}</td>
<td>${employee.name}</td>
<td>${employee.email}</td>
</tr>
</c:forEach>
</table>
<form action="add" method="post">
姓名: <input type="text" name="name"/>
邮箱: <input type="text" name="email"/>
<input type="submit" value="添加员工"/>
</form>
</body>
</html>
Spring配置(XML):
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/sc
评论已关闭