日期:2014-05-16  浏览次数:20467 次

spring mvc和jdbcTemplate例子
 

用到的包大家可以百度上找,稍后给出所有源代码

jar包下载

1. 建立mysql数据库 名称 test

CREATE TABLE `person` (
`id`  int(11) NOT NULL ,
`name`  varchar(100) ,
`password`  varchar(100),
PRIMARY KEY (`id`)
)

2.在 net.spring.bean下面建立一个实体Person

package net.spring.bean;

public class Person {
	int id;
	String name;
	String password;
	public Person(int id,String name,String password){
		this.id=id;
		this.name=name;
		this.password=password;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

 

3.建立一个dao以及imp

package net.spring.dao;

import net.spring.bean.Person;

public interface PersonDAO {
	void save(Person person);
	void del(Person person);
	void update(Person person);
	void searchAll();
}

 

package net.spring.dao.imp;

import org.springframework.jdbc.core.JdbcTemplate;
import net.spring.bean.Person;
import net.spring.dao.PersonDAO;

public class PersonDAOIMP implements PersonDAO{

	JdbcTemplate jdbcTemplate;
	public void del(Person person) {
		// TODO Auto-generated method stub
			}

	public void save(Person person) {
		// TODO Auto-generated method stub
		jdbcTemplate.update("insert into person values(?,?,?)",
				new Object[]{person.getId(),person.getName(),person.getPassword()});
	}

	public void searchAll() {
		// TODO Auto-generated method stub
		
	}

	public void update(Person person) {
		// TODO Auto-generated method stub
		
	}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}

4.修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<display-name>Spring3MVC</display-name>
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:spring-servlet.xml</param-value>
		</init-param>

		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>


 

5.建立一个配置文件spring-servlet.xml 路径在web.xml已经定义。在classpath也就是src根目录

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context http://www.springframew