1.实体类
在entity下创建数据库表对应的类
package com.imooc.oa.entity;
public class Department {
private String sn;
private String name;
private String address;
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.dao接口和sql映射文件
dao:
package com.imooc.oa.dao;
import com.imooc.oa.entity.Department;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("departmentDao")
public interface DepartmentDao {
void insert(Department department);
void update(Department department);
void delete(String sn);
Department select(String sn);
List<Department> selectAll();
}
xml:
<?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="com.imooc.oa.dao.DepartmentDao">
<resultMap id="department" type="Department">
<id property="sn" column="sn" javaType="String"/>
<result property="name" column="name" javaType="String"/>
<result property="address" column="address" javaType="String"/>
</resultMap>
<insert id="insert" parameterType="Department">
insert into department values(#{sn},#{name},#{address})
</insert>
<update id="update" parameterType="Department">
update department set name=#{name},address=#{address} where sn=#{sn}
</update>
<delete id="delete" parameterType="String">
delete from department where sn=#{sn}
</delete>
<select id="select" parameterType="String" resultMap="department">
select * from department where sn=#{sn}
</select>
<select id="selectAll" resultMap="department">
select * from department
</select>
</mapper>
3.biz(service)接口和实现类
(1)interfece
package com.imooc.oa.biz;
import com.imooc.oa.entity.Department;
import java.util.List;
public interface DepartmentBiz {
void add(Department department);
void edit(Department department);
void remove(String sn);
Department get(String sn);
List<Department> getAll();
}
(2)impl
package com.imooc.oa.biz.impl;
import com.imooc.oa.biz.DepartmentBiz;
import com.imooc.oa.dao.DepartmentDao;
import com.imooc.oa.entity.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("departmentBiz")
public class DepartmentBizImpl implements DepartmentBiz {
@Autowired
private DepartmentDao departmentDao;
public void add(Department department) {
departmentDao.insert(department);
}
public void edit(Department department) {
departmentDao.update(department);
}
public void remove(String sn) {
departmentDao.delete(sn);
}
public Department get(String sn) {
return departmentDao.select(sn);
}
public List<Department> getAll() {
return departmentDao.selectAll();
}
}
4.控制器
oa_web controller下 DepartmentController
package com.imooc.oa.controller;
import com.imooc.oa.biz.DepartmentBiz;
import com.imooc.oa.entity.Department;
import com.imooc.oa.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller("departmentController")
@RequestMapping("/department")
public class DepartmentController {
@Autowired
private DepartmentBiz departmentBiz;
@RequestMapping("/list")
public String list(Map<String,Object> map){
map.put("list",departmentBiz.getAll());
return "department_list";
}
@RequestMapping("/to_add")
public String toAdd(Map<String,Object> map){
map.put("department",new Department());
return "department_add";
}
@RequestMapping("/add")
public String add(Department department){
departmentBiz.add(department);
return "redirect:list";
}
@RequestMapping(value = "/to_update",params = "sn")
public String toUpdate(String sn,Map<String,Object> map){
map.put("department",departmentBiz.get(sn));
return "department_update";
}
@RequestMapping("/update")
public String update(Department department){
departmentBiz.edit(department);
return "redirect:list";
}
@RequestMapping(value = "/remove",params = "sn")
public String remove(String sn){
departmentBiz.remove(sn);
return "redirect:list";
}
}
5.JSP界面 实现增删改查
注意点:
1.tomcat的项目路径要/ 不然界面显示不全