Personnel management system --第五天
员工信息的增删改查
-
实体类
public class Staff { private Integer id; private String account; private String password; private String status; private Integer did; private String name; private String sex; private String idNumber; private Date workTime; private Date leaveTime; private Date bornTime; private String info; private Department department; setter/getter()方法 }
- dao层与实现
@Repository("staffDao")
public interface StaffDao {
public void insert(Staff staff);
public void delete(Integer id);
public void update(Staff staff);
public Staff selectById(Integer id);
public List<Staff> selectAll();
}
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="per.lc.sms.dao.StaffDao"> <resultMap id="resultMap" type="Staff"> <id property="id" column="id" javaType="Integer"/> <result property="account" column="account" javaType="String"/> <result property="password" column="password" javaType="String"/> <result property="status" column="status" javaType="String"/> <result property="did" column="did" javaType="Integer"/> <result property="name" column="name" javaType="String"/> <result property="sex" column="sex" javaType="String"/> <result property="idNumber" column="id_number" javaType="String"/> <result property="workTime" column="work_time" javaType="Date"/> <result property="leaveTime" column="leave_time" javaType="Date"/> <result property="bornTime" column="born_date" javaType="Date"/> <result property="info" column="info" javaType="String"/> <association property="department" column="did" javaType="Department" select="per.lc.sms.dao.DepartmentDao.selectById"/> //***在员工的显示列表上只能看到一个代表部门的编号,通过<association>将编号与部门对象连接起来 </resultMap> <insert id="insert" parameterType="Staff" useGeneratedKeys="true"> insert into staff (account,password,status,did,name,sex,id_number,work_time,leave_time,born_date,info) values (#{account},#{password},#{status},#{did},#{name},#{sex},#{idNumber},#{workTime},#{leaveTime},#{bornTime},#{info}) </insert> <delete id="delete" parameterType="Integer"> delete from staff where id=#{id} </delete> <update id="update" parameterType="Staff"> update staff set account=#{account},password=#{password},status=#{status},did=#{did},name=#{name},sex=#{sex},id_number=#{idNumber},//注意逗号!!!!,我的妈呀!! work_time=#{workTime},leave_time=#{leaveTime},born_date=#{bornTime},info=#{info} where id=#{id} </update> <select id="selectById" parameterType="Integer" resultMap="resultMap"> select * from staff where id=#{id} </select> <select id="selectAll" resultMap="resultMap"> select * from staff </select> </mapper>
- 业务层接口与实现
public interface StaffService { public void add(Staff staff); public void remove(Integer id); public void edit(Staff staff); public Staff get(Integer id); public List<Staff> getAll(); }
@Service("staffService")
public class StaffServiceImp implements StaffService {
@Autowired
private StaffDao staffDao;
public void add(Staff staff) {
staff.setPassword("123456"); //这几个信息不需要在页面显示,直接设置好。相当与默认的信息
staff.setWorkTime(new Date());
staff.setStatus("正常");
staffDao.insert(staff);
}
public void remove(Integer id) {
staffDao.delete(id);
}
public void edit(Staff staff) {
staffDao.update(staff);
}
public Staff get(Integer id) {
return staffDao.selectById(id);
}
public List<Staff> getAll() {
return staffDao.selectAll();
}
}
控制层:仅显示所有员工的信息
@Controller("staffController")
public class StaffController {
@Autowired
private StaffService staffService;
public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Staff> staffList=staffService.getAll();
request.setAttribute("staffList",staffList);
request.getRequestDispatcher("../staff_list.jsp").forward(request,response);
}

浙公网安备 33010602011771号