注解开发SPringMVC

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>

3.配置controller(控制层)的StudentController.java文件(名字随便取)
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);
	}

}

JSP页面


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值