package com.ssm.controllers;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.ssm.entities.Employee;
import com.ssm.services.DepartmentService;
import com.ssm.services.EmployeeService;
@Controller
public class MainController {
@Autowired
private DepartmentService departmentService;
@Autowired
private EmployeeService employeeService;
// 得到所有的employees
@RequestMapping("/emps")
public String getAllEmployee(Map<String, Object> map) {
map.put("allEmployees", employeeService.getAll());
return "lists";
}
@ModelAttribute
public void getEmployee(
@RequestParam(required = false, value = "id") Integer id,
Map<String, Object> map) {
if (id != null) {
map.put("employee", employeeService.get(id));
}
}
// 修改页面前得到employee
@RequestMapping(value = "/emp/{id}", method = RequestMethod.GET)
public String input(@PathVariable("id") Integer id, Map<String, Object> map) {
map.put("employee", employeeService.get(id));
System.out.println("=============="+employeeService.get(id).toString());
map.put("allDepartments", departmentService.getDepartments());
return "input";
}
// 增加页面前新建employee
@RequestMapping(value = "/emp", method = RequestMethod.GET)
public String input(Map<String, Object> map) {
map.put("allDepartments", departmentService.getDepartments());
map.put("employee", new Employee());
return "input";
}
// 增加页面保存employee
@RequestMapping(value = "/emp", method = RequestMethod.POST)
public String save(@Valid Employee employee, BindingResult bindingResult,
Map<String, Object> map) {
if (bindingResult.hasErrors()) {
for (ObjectError error : bindingResult.getAllErrors()) {
System.out.println(error.getObjectName() + " : "
+ error.getDefaultMessage());
}
map.put("allDepartments", departmentService.getDepartments());
return "input";
}
System.out.println(employee.toString());
employeeService.save(employee);
return "redirect:/emps";
}
// 修改页面保存employee
@RequestMapping(value = "/emp", method = RequestMethod.PUT)
public String update(Employee employee) {
employeeService.save(employee);
return "redirect:/emps";
}
// 删除employee
@RequestMapping(value = "/emp/{id}", method = RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id) {
employeeService.delete(id);
return "redirect:/emps";
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
SpringMVC+Spring+MyBatis_Demo
共91个文件
jar:44个
xml:16个
java:7个
3星 · 超过75%的资源 需积分: 3 53 下载量 101 浏览量
2015-04-28
22:02:41
上传
评论
收藏 11.66MB RAR 举报
温馨提示
用SpringMVC+Spring+MyBatis框架搭建的小Demo,实现了CRUD,可供新手对SSM的学习和参考
资源推荐
资源详情
资源评论
收起资源包目录
SSM_02.rar (91个子文件)
SSM_02
.project 1KB
.springBeans 455B
WebContent
WEB-INF
props
log4j.properties 1024B
lib
spring-aop-4.1.4.RELEASE.jar 351KB
commons-fileupload-1.3.1.jar 67KB
log4j-1.2.16.jar 470KB
spring-context-support-4.1.4.RELEASE.jar 174KB
spring-instrument-4.1.4.RELEASE.jar 7KB
spring-context-4.1.4.RELEASE.jar 1002KB
spring-webmvc-4.1.4.RELEASE.jar 763KB
mybatis-spring-1.2.1.jar 48KB
cglib-nodep-2.1_3.jar 317KB
jstl.jar 20KB
hibernate-validator-annotation-processor-5.1.3.Final.jar 63KB
spring-beans-4.1.4.RELEASE.jar 692KB
spring-web-4.1.4.RELEASE.jar 698KB
validation-api-1.1.0.Final.jar 62KB
commons-io-2.2.jar 170KB
hibernate-validator-5.1.3.Final.jar 619KB
commons-logging-1.1.1.jar 59KB
jackson-annotations-2.2.3.jar 33KB
jboss-logging-3.1.3.GA.jar 56KB
spring-jms-4.1.4.RELEASE.jar 263KB
spring-core-4.1.4.RELEASE.jar 983KB
commons-beanutils-1.9.2.jar 228KB
spring-orm-4.1.4.RELEASE.jar 367KB
jackson-core-2.2.3.jar 188KB
spring-jdbc-4.1.4.RELEASE.jar 417KB
spring-aspects-4.1.4.RELEASE.jar 56KB
spring-test-4.1.4.RELEASE.jar 491KB
spring-tx-4.1.4.RELEASE.jar 247KB
spring-instrument-tomcat-4.1.4.RELEASE.jar 10KB
aopalliance-1.0.jar 4KB
spring-websocket-4.1.4.RELEASE.jar 379KB
classmate-1.0.0.jar 59KB
standard.jar 384KB
spring-messaging-4.1.4.RELEASE.jar 279KB
commons-pool-1.6.jar 109KB
commons-logging-1.2.jar 60KB
hibernate-validator-cdi-5.1.3.Final.jar 34KB
jackson-databind-2.2.3.jar 846KB
commons-collections-3.2.1.jar 562KB
mysql-connector-java-5.1.7-bin.jar 693KB
spring-webmvc-portlet-4.1.4.RELEASE.jar 173KB
spring-expression-4.1.4.RELEASE.jar 253KB
mybatis-3.1.1.jar 636KB
spring-oxm-4.1.4.RELEASE.jar 80KB
web.xml 2KB
lists.jsp 2KB
index.jsp 399B
scripts
jquery-1.9.1.min.js 90KB
input.jsp 1KB
META-INF
MANIFEST.MF 39B
src
com
ssm
services
DepartmentService.java 739B
EmployeeService.java 1KB
entities
Department.java 680B
Employee.java 1KB
controllers
MainController.java 3KB
mappings
EmployeeMapper.xml 3KB
DepartmentMapper.xml 2KB
daos
DepartmentDao.java 460B
EmployeeDao.java 444B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 547B
.jsdtscope 503B
config
log4j.xml 639B
spring-mybatis.xml 2KB
spring-mvc.xml 1KB
db.properties 170B
spring.xml 702B
build
classes
log4j.xml 639B
generatorConfig.xml 2KB
spring-mybatis.xml 2KB
com
ssm
services
EmployeeService.class 2KB
DepartmentService.class 1KB
entities
Employee.class 2KB
Department.class 1KB
controllers
MainController.class 5KB
mappings
EmployeeMapper.xml 3KB
DepartmentMapper.xml 2KB
daos
EmployeeDao.class 504B
DepartmentDao.class 514B
spring-mvc.xml 1KB
mybatis-config.xml 399B
db.properties 170B
spring.xml 702B
ssm_02.sql 2KB
.classpath 879B
共 91 条
- 1
资源评论
- wangtianqun2015-11-11太简单!而且代码可读性比较差
yyyuqirong
- 粉丝: 0
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功