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

spring jdbctemplate的增删改查



?1.首先先添加两个包:

c3p0-0.9.1.2.jar 和 mysql-connector-java-5.1.7-bin.jar

?

?

创建一个file? jdbc.properties

DriverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc\:mysql\://localhost\:3306/0902?useUnicode\=true&characterEncoding\=UTF-8
user=root
pass=root

?
2.创建一个java类
Emp.java

package cn.nm.domain;

import java.io.Serializable;
import java.util.Date;

public class Emp implements Serializable{

	
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private String sex;
	private String pass;
	private Date hiredate;
	public Emp() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Emp(Integer id, String name, String sex, String pass, Date hiredate) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.pass = pass;
		this.hiredate = hiredate;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	public Date getHiredate() {
		return hiredate;
	}
	public void setHiredate(Date hiredate) {
		this.hiredate = hiredate;
	}
	
	
}

?3.定义一个接口类

EmpDao.java

package cn.nm.dao;

import java.util.List;

import cn.nm.domain.Emp;

public interface EmpDao {
	/*插入的操作*/
	boolean insert(Emp entity);
	/*更新操作*/
	boolean update(Emp entity);
	/*删除的操作*/
	boolean delete(Emp entity);
	/*根据主键id查询*/
	Emp findById(Integer id);
	/*查询所有*/
	List<Emp> findAll();

}

?4.创建一个EmpDaoImpl.java

package cn.nm.dao;

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

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import cn.nm.domain.Emp;

public class EmpDaoImpl implements EmpDao {

	/* 封装一个JdbcTemplate的模板对象 */
	private JdbcTemplate jdbcTemplate;

	/* 通过set方法注入进来即可 */
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public boolean delete(Emp entity) {
		// 定义返回结果
		boolean flag = false;
		/* 插入实现 */
		int i = jdbcTemplate.update("delete from emp where id=? ",
				new Object[] { entity.getId() });
		if (i > 0) {
			flag = true;
		}
		return flag;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Emp> findAll() {
		// 定义返回结果
		List<Emp> entities =new ArrayList<Emp>();
		entities = (List<Emp>) jdbcTemplate.query(
				"select id,name,sex,pass,hiredate from emp", new RowMapper() {
					@Override
					public Object mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Emp emp = new Emp();
						emp.setId(rs.getInt("id"));
						emp.setName(rs.getString("name"));
						emp.setSex(rs.getString("sex"));
						emp.setPass(rs.getString("pass"));
						emp.setHiredate(rs.getDate("hiredate"));

						return emp;
					}
				});
		return entities;
	}

	@Override
	public Emp findById(Integer id) {
		// 定义返回结果
		Emp entity = nul