Spring MVC

第一步:所需jar包

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">
  <display-name>A</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- Spring MVC配置 -->
<!-- ====================================== -->
<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

配置spring-mvc.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<!-- 默认的注解映射的支持 -->
	<mvc:annotation-driven />

	<!-- 自动扫描 --><!-- 扫描注解 -->
	<context:component-scan base-package="com.cs.*" />

	<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

</beans>

配置jdbc连接的c3p0.properties

#数据库驱动类
c3p0.driverClass =com.mysql.jdbc.Driver
#数据库地址 
c3p0.jdbcUrl =jdbc:mysql://localhost:3306/usermainshiti?characterEncoding=utf-8
#数据库用户名
c3p0.user =root
#数据库密码
c3p0.password=0811

testController.java

package com.cs.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.cs.model.employee;
import com.cs.model.user;
import com.cs.service.UserService;

@Controller
public class testController extends AbstractController{
	
	@Autowired
	private UserService userservice;
	
	@RequestMapping("/login")
	protected ModelAndView login(@RequestParam String loginid,@RequestParam String pws) throws Exception{
		
		System.out.println(loginid+"^");
//		ModelAndView m = new ModelAndView();
//		m.setViewName("/ShouYe");
		user user = new user(loginid,pws);
		boolean semployee = userservice.checkUser(user);
		if(semployee){
			System.out.println("成功");
		}else{
			System.out.println("失败");
		}
		System.out.println(semployee);
		
		return ShouYe();
	}
	
	@RequestMapping("/aaa")
	public ModelAndView aaa() throws Exception{
		ModelAndView m = new ModelAndView();
		m.setViewName("index");
		employee semployee = userservice.find();
		System.out.println(semployee);
		return m;
	}
	@RequestMapping("/ShouYe")
	public ModelAndView ShouYe() throws Exception{
		ModelAndView m = new ModelAndView();
		m.setViewName("ShouYe");
		List<user> semployee = userservice.showAll();
		for(user emp:semployee){
			System.out.println(emp);
		}
		ModelAndView mv = new ModelAndView("ShouYe");
		mv.addObject("semployee", semployee);
//		System.out.println(semployee);
		return mv;
	}
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		return new ModelAndView("index");
	}
	
}

UserDao.java

package com.cs.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.cs.model.employee;
import com.cs.model.user;

@Repository
public class UserDao extends BaseDao{


	public boolean checkUser(user user) throws Exception {
		// TODO Auto-generated method stub
		Connection con = getConnection();
		String sql = "select * from user where zhanghao="+user.getZhanghao()+" and psw="+user.getPsw();
		PreparedStatement pStatement = con.prepareStatement(sql);
		ResultSet rs = pStatement.executeQuery();
//		,user.getZhanghao(),user.getPsw()
		boolean flag = false;
//		ResultSet rs=JDBCUtil.generalsqlexcute(sql,user.getZhanghao(),user.getPsw());
		try {
			while(rs.next()){
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
	
	public List<user> showAll() throws Exception {
		Connection con = getConnection();
		String sql = "select * from user";
		PreparedStatement pStatement = con.prepareStatement(sql);
		ResultSet rs = pStatement.executeQuery();
		user user2 = null;
		List<user> pts = new ArrayList<user>();
		while (rs.next()) {
			user2 = new user(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),
					rs.getInt(5),rs.getInt(6),rs.getString(7),rs.getInt(8),
					rs.getString(9),rs.getString(10),rs.getString(11),rs.getString(12));
			pts.add(user2);
		}
		rs.close();
		close(pStatement, con);
		return pts;
	}
	
	
	public employee find() throws Exception {
		Connection con = getConnection();
		String sql = "select * from tbl_employee where id=4";
		PreparedStatement pStatement = con.prepareStatement(sql);
		ResultSet rSet = pStatement.executeQuery();
		employee user2 = null;
		while (rSet.next()) {
			System.out.println("====");
			int id = rSet.getInt(1);
			String LastName=rSet.getString(2);
			String Email=rSet.getString(3);
			user2 = new employee();
			user2.setId(id);
			user2.setLastName(LastName);
			user2.setEmail(Email);
		}
		rSet.close();
		close(pStatement, con);
		return user2;
	}
	
	
	

}

BaseDao.java

package com.cs.dao;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class BaseDao {
	
	private ComboPooledDataSource ds ;
	

	public BaseDao() {
		ds = new ComboPooledDataSource();
	}
	
	public Connection getConnection() throws Exception{
		Connection con = ds.getConnection();
		return con;
	}
	
	public void close(PreparedStatement pStatement,Connection con) throws SQLException {
		if (pStatement!=null) {
			pStatement.close();
		}
		if (con!=null) {
			con.close();
		}
	}
	
	public  int executeUpdate(String sql,Object[] parms) throws Exception {
		Connection con = getConnection();
		PreparedStatement pStatement = con.prepareStatement(sql);
		if (parms!=null) {
			for(int i=0;i<parms.length;i++){
				pStatement.setObject(i+1, parms[i]);
			}
		}
		int a =	pStatement.executeUpdate();
		close(pStatement, con);
		
		return a;
	}
	

	
	
	
	

}

user.java

package com.cs.model;

public class user {
	private int id;
	
	public user(int id) {
		super();
		this.id = id;
	}
	private String zhanghao;
	private String psw;
	private String name;
	private int age;
	private int sex;
	private String shenfen;
	private int yn;
	private String creattime;
	private String creatman;
	private String updatetime;
	private String updateman;
	public user(){}
	
	public user(Integer id, String zhanghao, String psw, String name, int age,
			int sex, String shenfen, int yn, String creattime, String creatman,
			String updatetime, String updateman) {
		super();
		this.id = id;
		this.zhanghao = zhanghao;
		this.psw = psw;
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.shenfen = shenfen;
		this.yn = yn;
		this.creattime = creattime;
		this.creatman = creatman;
		this.updatetime = updatetime;
		this.updateman = updateman;
	}
	@Override
	public String toString() {
		return "user [id=" + id + ", zhanghao=" + zhanghao + ", psw=" + psw
				+ ", name=" + name + ", age=" + age + ", sex=" + sex
				+ ", shenfen=" + shenfen + ", yn=" + yn + ", creattime="
				+ creattime + ", creatman=" + creatman + ", updatetime="
				+ updatetime + ", updateman=" + updateman + "]";
	}
	public user( String zhanghao, String psw) {
		super();
		this.zhanghao = zhanghao;
		this.psw = psw;
	}
	
	public user(int id, String zhanghao, String psw, String name,int age, int sex,
			String shenfen, int yn, String creattime, String creatman,
			String updatetime, String updateman) {
		super();
		this.id = id;
		this.zhanghao = zhanghao;
		this.psw = psw;
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.shenfen = shenfen;
		this.yn = yn;
		this.creattime = creattime;
		this.creatman = creatman;
		this.updatetime = updatetime;
		this.updateman = updateman;
	}
	public user(String zhanghao, String psw, String name,int age, int sex,
			String shenfen, int yn, String creattime, String creatman,
			String updatetime, String updateman) {
		super();
		this.zhanghao = zhanghao;
		this.psw = psw;
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.shenfen = shenfen;
		this.yn = yn;
		this.creattime = creattime;
		this.creatman = creatman;
		this.updatetime = updatetime;
		this.updateman = updateman;
	}
	public user(int id,String zhanghao, String psw, String name,int age, int sex,
			String shenfen, int yn, 
			String updatetime, String updateman) {
		super();
		this.id = id;
		this.zhanghao = zhanghao;
		this.psw = psw;
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.shenfen = shenfen;
		this.yn = yn;
		this.updatetime = updatetime;
		this.updateman = updateman;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getZhanghao() {
		return zhanghao;
	}
	public void setZhanghao(String zhanghao) {
		this.zhanghao = zhanghao;
	}
	public String getPsw() {
		return psw;
	}
	public void setPsw(String psw) {
		this.psw = psw;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	public String getShenfen() {
		return shenfen;
	}
	public void setShenfen(String shenfen) {
		this.shenfen = shenfen;
	}
	public int getYn() {
		return yn;
	}
	public void setYn(int yn) {
		this.yn = yn;
	}
	public String getCreattime() {
		return creattime;
	}
	public void setCreattime(String creattime) {
		this.creattime = creattime;
	}
	public String getCreatman() {
		return creatman;
	}
	public void setCreatman(String creatman) {
		this.creatman = creatman;
	}
	public String getUpdatetime() {
		return updatetime;
	}
	public void setUpdatetime(String updatetime) {
		this.updatetime = updatetime;
	}
	public String getUpdateman() {
		return updateman;
	}
	public void setUpdateman(String updateman) {
		this.updateman = updateman;
	}
	
	
}

employee.java

package com.cs.model;

public class employee {

	private Integer id;
	private String last_name;
	private String gender;
	private String email;
	
	public employee(){
		super();
	}
	public employee(Integer invalid,String last_name,String gender,String email){
		super();
		this.id = invalid;
		this.last_name = last_name;
		this.gender = gender;
		this.email = email;
	}
	
	
	public Integer getId() {
		return id;
	}
	@Override
	public String toString() {
		return "Employee [id=" + id + ", last_name=" + last_name + ", gender="
				+ gender + ", email=" + email + "]";
	}
	public void setId(Integer id) {
		this.id = id;
	} 
	public String getLast_name() {
		return last_name;
	}
	public void setLastName(String lastName) {
		this.last_name = lastName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	
	
	
}

UserService.java

package com.cs.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cs.dao.UserDao;
import com.cs.model.employee;
import com.cs.model.user;

@Service
public class UserService {

	@Autowired
	private UserDao userDao;
	
	public employee find() throws Exception {
		return userDao.find();
	}
	public List<user> showAll() throws Exception{
		return userDao.showAll();
	}
	public boolean checkUser(user user) throws Exception{
		return userDao.checkUser(user);
	}
	
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萤火的微亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值