1.導入jar包和配置web.xml文件(配置文件詳情內容見SPringMVC的web.xml配置)
2.配置在classpath目錄下的SPringMVC.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 掃描註解 -->
<context:component-scan base-package="com.mingde"></context:component-scan>
<!-- 配置映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 配置適配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- 配置響應跳轉 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/student/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
package com.mingde.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.mingde.dao.StudentDao;
import com.mingde.po.Student;
@Controller
@RequestMapping("stu") //在驅動服務器後,在地址欄加上/stu,然後根據下方要調用那個方法就再加上方法上面的註解裏的路徑+action,如stu/list.action,即可調用該方法
public class StudentController {
@Resource(name="sd")
private StudentDao sd;
@RequestMapping("/list")
public ModelAndView seeAll()throws Exception{
List seeAll = sd.SeeAll();
ModelAndView mv=new ModelAndView("list", "students", seeAll); //參數一:要跳轉的頁面,參數二和三是要傳到頁面上的數據,參數二爲數據名,參數三爲數據
return mv;
}
@RequestMapping("/toAdd")
public String toAdd(Model model) throws Exception{
List seeClasses = sd.SeeClasses();
model.addAttribute("cls", seeClasses);
return "toAdd"; //要跳轉的頁面
}
@RequestMapping("/add")
public String add(Student st)throws Exception{
sd.add(st);
return "redirect:list.action"; //重定向
}
@RequestMapping("toUpdate")
public ModelAndView update(int sid)throws Exception{
Student find = sd.find(sid);
List seeClasses = sd.SeeClasses();
ModelAndView mv = new ModelAndView("update"); //要跳轉的頁面
mv.addObject("cls",seeClasses);
mv.addObject("st", find);
return mv;
}
@RequestMapping("update")
public String update(Student st)throws Exception{
sd.update(st);
return "redirect:list.action"; //重定向
}
@RequestMapping("delete")
public String delete(int sid) throws Exception{
sd.delete(sid);
return "redirect:list.action"; //重定向
}
}
在驅動服務器後,在地址欄加上/stu,然後根據下方要調用那個方法就再加上方法上面的註解裏的路徑+action,如stu/list.action,即可調用該方法
Dao層
package com.mingde.dao.impl;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Repository;
import com.mingde.dao.StudentDao;
import com.mingde.po.Classes;
import com.mingde.po.Student;
import com.mingde.utils.JdbcUtils;
@Repository("sd")
public class StudentDaoImpl implements StudentDao {
QueryRunner qr;
public StudentDaoImpl() {
qr=new QueryRunner(JdbcUtils.getDataSource());
}
@Override
public List SeeAll() throws Exception {
return qr.query("select * from student", new BeanListHandler<>(Student.class));
}
@Override
public List SeeClasses() throws Exception {
return qr.query("select * from classes", new BeanListHandler<>(Classes.class));
}
@Override
public void add(Student st) throws Exception {
qr.update("insert into student values(seqstudent.nextval,?,?,?,?,to_date(?,'yyyy-MM-dd'),?)",st.getSname(),
st.getSex(),st.getAge(),st.getAddr(),st.getBirth(),st.getCid());
}
@Override
public Student find(int sid) throws Exception {
return qr.query("select * from student where sid=?", new BeanHandler<>(Student.class),sid);
}
@Override
public void update(Student st) throws Exception {
qr.update("update student set sname=?,sex=?,age=?,addr=?,birth=to_date(?,'yyyy-MM-dd'),cid=? where sid=?",
st.getSname(),st.getSex(),st.getAge(),st.getAddr(),st.getBirth(),st.getCid(),st.getSid());
}
@Override
public void delete(int sid) throws Exception {
qr.update("delete from student where sid=?",sid);
}
}