准备工作:创建工程,添加jar包,配置核心文件,创建数据库,建表
第一步、导入所需要的配置文件,建一个source Folder ,放入所需要的配置文件
第二步、分包分模块,将springMvc 帮我们生成好的代码粘贴到我们的项目中。
第三步、创建DpetService 以及他的实现类 DeptServiceImpl
DpetService .java
public interface DeptService {
List<Dept> findList();
Dept findById(Integer did);
void update(Dept Dept);
void add(Dept Dept);
void delete(Integer did);
}
DeptServiceImpl.java
@Service
public class DeptServiceImpl implements DeptService{
@Autowired //注入
private DeptMapper deptMapper;
public List<Dept> findList() {
DeptExample example = new DeptExample();
List<Dept> depts = deptMapper.selectByExample(example);
return depts;
}
@Override
public Dept findById(Integer did) {
// TODO Auto-generated method stub
Dept dept = deptMapper.selectByPrimaryKey(did);
return dept;
}
@Override
public void update(Dept Dept) {
// TODO Auto-generated method stub
deptMapper.updateByPrimaryKey(Dept);
}
@Override
public void add(Dept Dept) {
// TODO Auto-generated method stub
deptMapper.insert(Dept);
}
public void delete(Integer did) {
deptMapper.deleteByPrimaryKey(did);
}
}
第四步、创建DeptController类
@Controller
@RequestMapping(value= {"/dept"})
public class DeptController {
@Autowired
private DeptService deptService;
@RequestMapping(value= {"/findList"})
public String findList(Model model) {
List<Dept> depts = deptService.findList();
model.addAttribute("depts", depts);
return "deptList";
}
//删除数据
@RequestMapping(value= {"/delete"})
public String delete(Integer did) {
deptService.delete(did);
return "redirect:/dept/findList.do";
}
//添加数据
@RequestMapping(value= {"/add"})
public String add(Dept dept) {
deptService.add(dept);
return "redirect:/dept/findList.do";
}
//去add.jsp
@RequestMapping(value="/toAdd")
public String toAdd() {
return "addList";
}
//去update.jsp
@RequestMapping(value="/toUpdate")
public String findById(Model model, Integer did ) {
Dept dept = deptService.findById(did);
model.addAttribute("dept", dept);
return "update";
}
//添加数据
@RequestMapping(value="/update")
public String update(Dept dept) {
deptService.update(dept);
return "redirect:/dept/findList.do";
}
}
Ok~将项目添加到Tomcat下,就可以运行啦
付一下jsp页面 ,jsp页面要放在WEB-INF目录下面