javaee基于ssm框架的项目工资
时间: 2023-12-18 07:02:33 浏览: 116
基于SSM框架的JavaEE项目工资管理系统可以实现对员工工资信息的管理,包括员工信息的录入、修改、删除和查询等功能。下面是一个简单的实现示例:
1. 首先,需要在MySQL数据库中创建一个名为salary的数据库,并创建一个名为employee的表,用于存储员工信息和工资信息。表结构如下:
```sql
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '员工编号',
`name` varchar(20) NOT NULL COMMENT '员工姓名',
`gender` varchar(2) NOT NULL COMMENT '员工性别',
`age` int(11) NOT NULL COMMENT '员工年龄',
`salary` double NOT NULL COMMENT '员工工资',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='员工信息表';
```
2. 在JavaEE项目中,需要创建一个Employee实体类,用于映射employee表中的数据。代码如下:
```java
public class Employee {
private Integer id;
private String name;
private String gender;
private Integer age;
private Double salary;
// 省略getter和setter方法
}
```
3. 创建一个EmployeeMapper接口,用于定义对employee表的CRUD操作。代码如下:
```java
public interface EmployeeMapper {
// 添加员工信息
int addEmployee(Employee employee);
// 修改员工信息
int updateEmployee(Employee employee);
// 删除员工信息
int deleteEmployee(Integer id);
// 查询所有员工信息
List<Employee> findAllEmployees();
// 根据员工编号查询员工信息
Employee findEmployeeById(Integer id);
}
```
4. 创建一个EmployeeMapper.xml文件,用于实现EmployeeMapper接口中定义的方法。代码如下:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.EmployeeMapper">
<insert id="addEmployee" parameterType="com.example.entity.Employee">
insert into employee(name, gender, age, salary)
values(#{name}, #{gender}, #{age}, #{salary})
</insert>
<update id="updateEmployee" parameterType="com.example.entity.Employee">
update employee set name=#{name}, gender=#{gender}, age=#{age}, salary=#{salary}
where id=#{id}
</update>
<delete id="deleteEmployee" parameterType="java.lang.Integer">
delete from employee where id=#{id}
</delete>
<select id="findAllEmployees" resultType="com.example.entity.Employee">
select * from employee
</select>
<select id="findEmployeeById" resultType="com.example.entity.Employee">
select * from employee where id=#{id}
</select>
</mapper>
```
5. 创建一个EmployeeService接口,用于定义对员工信息的业务逻辑操作。代码如下:
```java
public interface EmployeeService {
// 添加员工信息
boolean addEmployee(Employee employee);
// 修改员工信息
boolean updateEmployee(Employee employee);
// 删除员工信息
boolean deleteEmployee(Integer id);
// 查询所有员工信息
List<Employee> findAllEmployees();
// 根据员工编号查询员工信息
Employee findEmployeeById(Integer id);
}
```
6. 创建一个EmployeeServiceImpl类,实现EmployeeService接口中定义的方法。代码如下:
```java
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
@Override
public boolean addEmployee(Employee employee) {
return employeeMapper.addEmployee(employee) > 0;
}
@Override
public boolean updateEmployee(Employee employee) {
return employeeMapper.updateEmployee(employee) > 0;
}
@Override
public boolean deleteEmployee(Integer id) {
return employeeMapper.deleteEmployee(id) > 0;
}
@Override
public List<Employee> findAllEmployees() {
return employeeMapper.findAllEmployees();
}
@Override
public Employee findEmployeeById(Integer id) {
return employeeMapper.findEmployeeById(id);
}
}
```
7. 创建一个EmployeeController类,用于处理员工信息的请求。代码如下:
```java
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
// 添加员工信息
@PostMapping("/add")
public boolean addEmployee(@RequestBody Employee employee) {
return employeeService.addEmployee(employee);
}
// 修改员工信息
@PostMapping("/update")
public boolean updateEmployee(@RequestBody Employee employee) {
return employeeService.updateEmployee(employee);
}
// 删除员工信息
@PostMapping("/delete")
public boolean deleteEmployee(@RequestParam Integer id) {
return employeeService.deleteEmployee(id);
}
// 查询所有员工信息
@GetMapping("/findAll")
public List<Employee> findAllEmployees() {
return employeeService.findAllEmployees();
}
// 根据员工编号查询员工信息
@GetMapping("/findById")
public Employee findEmployeeById(@RequestParam Integer id) {
return employeeService.findEmployeeById(id);
}
}
```
以上就是一个简单的基于SSM框架的JavaEE项目工资管理系统的实现示例。如果需要更多的功能,可以根据需求进行扩展。
阅读全文