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

Spring-JDBC,带一小例子

一、包结构(需要导的库)

?

二、entity

package org.e276.entity;

import java.sql.Date;
/**
 * 员工实体类
 * @author miao
 *
 */
public class Employee {

	private int id;
	private String name;
	private boolean sex;
	private Date birthday;
	private double salary;
	private int departId;

	public Employee() {
		super();
	}

	public Employee(int id, String name, boolean sex, Date birthday, double salary, int departId) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.birthday = birthday;
		this.salary = salary;
		this.departId = departId;
	}

	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 boolean isSex() {
		return sex;
	}

	public void setSex(boolean sex) {
		this.sex = sex;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public int getDepartId() {
		return departId;
	}

	public void setDepartId(int departId) {
		this.departId = departId;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday
				+ ", salary=" + salary + ", departId=" + departId + "]";
	}

}

?

三、dao接口

package org.e276.dao;

import java.util.List;
import java.util.Map;
import org.e276.entity.Employee;

/**
 * 实现所有的接口
 */
public interface EmployeeDao {

	// 查询所有的用户,使用RowMapper可映射多行数据
	public List<Employee> getAllEmployees();

	// 查询薪水在3000-5000之间的有条件查询,用RowCallbackHandler,也可以用RowMapper
	public List<Employee> getEmployeesBetweenSalary(double low, double up);

	// 女或男职员有多少个,单值数据查询
	public int getEmployeeCountBySex(boolean sex);

	// 一次插入多名职员
	public int[] saveEmployees(final Employee[] employees);

	// 删除员工
	public int deleteEmployee(int id);

	// 修改 e必须是final类型
	public int updateEmployee(final Employee e);

	// 根据ID得到职员,查询单值对象
	public Employee getEmployeeById(int id);

	// 查询工资小于money的员工
	public List<Employee> getEmployeeLessSalary(double money);

	// 多表链接查询,封装成Map
	public List<Map<String, Object>> findEmployeeInfo();

}

?

四、daoImpl实现类

package org.e276.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.e276.dao.EmployeeDao;
import org.e276.entity.Employee;
import org.e276.mapper.EmployeeMapper;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
/**
 * 实现类
 * @author miao
 *
 */
public class EmployeeDaoImpl extends NamedParameterJdbcDaoSupport implements EmployeeDao {

	/**
	 * 查询所有的用户,使用RowMapper可映射多行数据
	 */
	public List<Employee> getAllEmployees() {
		return super.getJdbcTemplate().query(
				"select id, name, sex, salary, birthday, depart_id from employee",
				new EmployeeMapper());
	}

	/**
	 * 查询薪水在3000-5000之间的有条件查询,用RowCallbackHandler,也可以用RowMapper
	 */
	public List<Employee> getEmployeesBetweenSalary(double low, double up) {
		return super
				.getJdbcTemplate()
				.query("select id, name, sex, salary, birthday, depart_id from employee  where salary between ? and ?",
						new EmployeeMapper(), low, up);
	}

	/**
	 * 女或男职员有多少个
	 */
	public int getEmployeeCountBySex(boolean sex) {
		return super.getJdbcTemplate()
				.queryForInt("select count(*) from employee where sex=?", sex);
	}

	/**
	 * 一次插入多名职员
	 */
	public int[] saveEmployees(final Employee