pom.xml文件
4.0.0
com.zzxt
spring.mvc
war
0.0.1-SNAPSHOT
spring.mvc Maven Webapp
http://maven.apache.org
UTF-8
UTF-8
1.8
5.0.7.RELEASE
javax.servlet
javax.servlet-api
3.1.0
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
javax.servlet
jstl
1.2
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
mysql
mysql-connector-java
5.1.46
com.alibaba
druid
1.1.10
junit
junit
4.12
test
spring.mvc
org.springframework.boot
spring-boot-maven-plugin
public
aliyun nexus
http://maven.aliyun.com/nexus/content/groups/public/
true
public
aliyun nexus
http://maven.aliyun.com/nexus/content/groups/public/
true
false
applacationContext-mvc.xml文件
applicationContext.xml文件
com.zzxt.spring.mvc.restful.dao文件夹下
stuInfoDao.java
package com.zzxt.spring.mvc.restful.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.zzxt.spring.mvc.restful.entity.Student;
@Repository
public interface StuInfoDao {
public void insertStuInfo(Student ci);
public List getStuInfo();
public Student searchStuInfoById(String stuNo);
public void delStuInfo(String stuNo);
public void updateInfo(Student stu,String stuNo);
}
stuInfoDaoImpl
package com.zzxt.spring.mvc.restful.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import com.zzxt.spring.mvc.restful.entity.Student;
@Repository
public class StuInfoDaoImpl implements StuInfoDao{
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
/*
*
* 增加学生信息
*
* */
public void insertStuInfo(Student ci){
System.out.println("=======>开始注入信息");
String sql = "insert into stu_info values (:stuNo,:stuName,:gender,:age,:classNo)";
Map map = new HashMap();
map.put("stuNo", ci.getStuNo());
map.put("stuName", ci.getStuName());
map.put("gender", ci.getGender());
map.put("age",ci.getAge());
map.put("classNo", ci.getClassNo());
System.out.println("======>" + ci.getStuNo());
namedParameterJdbcTemplate.update(sql,map);
}
/*
* 查询所有学生信息
*
* */
public List getStuInfo() {
String sql = "select * from stu_info";
return jdbcTemplate.query(sql, new RowMapper() {
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
//创建一个学生信息结构对象,并利用set方法进行初始化
Student st = new Student();
st.setStuNo(rs.getString("stuNo"));
st.setStuName(rs.getString("stuName"));
st.setGender(rs.getString("gender"));
st.setAge(rs.getInt("age"));
st.setClassNo(rs.getString("classNo"));
return st;
}
});
}
/*
*
* 查询特定学生信息
*
* */
public Student searchStuInfoById(String stuNo){
String sql = "select * from stu_info where stuNo=?";
return jdbcTemplate.queryForObject(sql, new Object[] {stuNo}, new RowMapper() {
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
Student st = new Student();
st.setStuNo(rs.getString("stuNo"));
st.setStuName(rs.getString("stuName"));
st.setGender(rs.getString("gender"));
st.setAge(rs.getInt("age"));
st.setClassNo(rs.getString("classNo"));
return st;
}
});
}
/*
*
*
* 修改学生信息
*
*
* */
public void updateInfo(Student stu,String stuNo) {
String sql = "update stu_info set stuName=?, gender=?, age=?,classNo=? where stuNo=?";
jdbcTemplate.update(sql, new Object[] {stu.getStuName(),stu.getGender(),stu.getAge(),stu.getClassNo(),stuNo});
}
/*
*
*
* 删除学生信息
*
*
*
* */
public void delStuInfo(String stuNo) {
// TODO Auto-generated method stub
String sql = "delete from stu_info where stuNo = ?";
jdbcTemplate.update(sql, new Object[] {stuNo});
}
}
com.zzxt.spring.mvc.restful.service包下的
StuService.java接口
package com.zzxt.spring.mvc.restful.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.zzxt.spring.mvc.restful.entity.Student;
@Service
public interface StuService {
public void addStuInfo(Student ci);
public List getStuInfo();
public Student updateStuInfo(String stuNo);
public void delStuInfo(String stuNo);
public void doUpdateStuInfo(Student stu,String stuNo);
}
StuServiceImpl.java上面接口的实现
package com.zzxt.spring.mvc.restful.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzxt.spring.mvc.restful.dao.StuInfoDao;
import com.zzxt.spring.mvc.restful.entity.Student;
@Service
public class StuServiceImpl implements StuService{
/*
*
* servic层我认为起到了信息转换的作用
*
* */
@Autowired
private StuInfoDao stuInfoDao;
//添加学生信息
public void addStuInfo(Student ci) {
// TODO Auto-generated method stub
stuInfoDao.insertStuInfo(ci);
}
//查询所有学生信息
public List getStuInfo() {
return stuInfoDao.getStuInfo();
}
//修改学生信息初始化页面
public Student updateStuInfo(String stuNo) {
// TODO Auto-generated method stu
return stuInfoDao.searchStuInfoById(stuNo);
}
//删除学生信息
public void delStuInfo(String stuNo) {
// TODO Auto-generated method stub
stuInfoDao.delStuInfo(stuNo);
}
//修改学生信息
public void doUpdateStuInfo(Student stu,String stuNo) {
// TODO Auto-generated method stub
stuInfoDao.updateInfo(stu,stuNo);
}
}
com.zzxt.spring.mvc.restful.entity包下
Student.java
package com.zzxt.spring.mvc.restful.entity;
import org.springframework.stereotype.Component;
@Component
public class Student {
private String stuNo;
private String stuName;
private String gender;
private String classNo;
private int age;
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getGender() {
return gender;
}
public void setGender(String string) {
this.gender = string;
}
public String getClassNo() {
return classNo;
}
public void setClassNo(String classNo) {
this.classNo = classNo;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
com.zzxt.spring.mvc.restful.action包下
stuController.java
package com.zzxt.spring.mvc.restful.action;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.zzxt.spring.mvc.restful.entity.Student;
import com.zzxt.spring.mvc.restful.service.StuService;
@Controller
@RequestMapping("/student")
public class stuController {
@Autowired
private StuService stuServices;
//显示学生信息
@RequestMapping("/list")
public String stuList(Map map) {
map.put("student", stuServices.getStuInfo());
return "/views/stu/showView.jsp";
}
//增加学生信息
@RequestMapping(value="/manager",method=RequestMethod.POST)
public String addStuInfo(Student ci) {
//System.out.println("我运行到了页面转接处");
stuServices.addStuInfo(ci);
return "redirect:list";
}
//更新页面初始化; 这段代码要熟记。
@RequestMapping(value="/updateInit/{stuNo}",method=RequestMethod.GET)
public String updateInit(@PathVariable("stuNo") String stuNo,Map map) {
map.put("student", stuServices.updateStuInfo(stuNo));
return "/views/stu/insertInfo.jsp";
}
// 更新后跳转
@RequestMapping(value="/manager",method=RequestMethod.PUT)
public String update(Student ci) {
stuServices.doUpdateStuInfo(ci,ci.getStuNo());
return "redirect:list";
}
//删除学生信息
@RequestMapping(value="/delete/{stuNo}",method=RequestMethod.GET)
public String deleteInit(@PathVariable("stuNo") String stuNo,Map map) {
stuServices.delStuInfo(stuNo);
map.put("student", stuServices.getStuInfo());
return "/views/stu/showView.jsp";
}
}
jsp页面代码
insertInfo.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
学生信息网页录入
showView.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
学生信息列表
以下是我eclipse下的文件分布, 哈哈,
web.xml文件
Archetype Created Web Application
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext-mvc.xml
1
DispatcherServlet
/
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
DispatcherServlet
有些地方不是很完善,只提供大致思路。