ITKeyword,专注技术干货聚合推荐

注册 | 登录

Spring JDBC DAO的两个example

zwhfyy 分享于

2020腾讯云10周年活动,优惠非常大!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1040

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
地址https://www.aliyun.com/minisite/goods

推荐:Spring DAO之JDBC

  Spring DAO之JDBC     Spring提供的DAO(数据访问对象)支持主要的目的是便于以标准的方式使用不同的数据访问技术, 如JDBC,Hibernate或者JDO等。它不仅可以让

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.RowMapperResultReader;

public class SpringJDBCDAO {

private class PersonRowMapper implements RowMapper {
public Object mapRow(ResultSet rs, int index) throws SQLException {
Person person = new Person();
person.setId(new Integer(rs.getInt("id")));
person.setFirstName(rs.getString("first_name"));
person.setLastName(rs.getString("last_name"));
return person;
}
}

private JdbcTemplate jdbcTemplate;

public void processStoredProcedure() {
CallableStatementCallback cb = new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs)
throws SQLException {
cs.execute();
return null;
}
};
jdbcTemplate.execute("{ ARCHIVE_STUDENTS }", cb);
}

public List getAllPersons() {
String sql = "select id, first_name, last_name from person";
return jdbcTemplate.query(sql, new RowMapperResultReader(
new PersonRowMapper()));
}

public String getLastNameForId(Integer id) {
String sql = "select last_name from person where id = ?";
return (String) jdbcTemplate.queryForObject(sql, new Object[] { id },
String.class);
}

public int getNumberOfPersons() {
return jdbcTemplate.queryForInt("select count(*) from person");
}

public Person getPerson1(final Integer id) {
String sql = "select id, first_name, last_name from person where id = ?";
final Person person = new Person();
final Object[] params = new Object[] { id };
jdbcTemplate.query(sql, params, new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
person.setId(new Integer(rs.getInt("id")));
person.setFirstName(rs.getString("first_name"));
person.setFirstName(rs.getString("last_name"));
}
});
return person;
}

public Person getPerson2(final Integer id) {
String sql = "select id, first_name, last_name from person where id = ?";
final Person person = new Person();
final Object[] params = new Object[] { id };
List list = jdbcTemplate.query(sql, params, new RowMapperResultReader(
new PersonRowMapper()));
return (Person) list.get(0);
}

public int insertPerson(Person person) {
String sql = "insert into person (id, firstName, lastName) values (?, ?, ?)";
Object[] params = new Object[] { person.getId(), person.getFirstName(),
person.getFirstName() };
int[] types = new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR };
JdbcTemplate jdbcTemplate = null;
return jdbcTemplate.update(sql, params, types);
}

public int[] updatePersons(final List persons) {
String sql = "insert into person (id, firstName, lastName) values (?, ?, ?)";
BatchPreparedStatementSetter setter = null;
setter = new BatchPreparedStatementSetter() {
public int getBatchSize() {
return persons.size();
}

推荐:springframework(十三)spring的Dao和JDBC

DAO(Data Access Object),我们开发的时候通常采用一接口一实现的方式。Dao所抛出的异常在spring中都是DataAccessException的子类,并且DataAccessException是

public void setValues(PreparedStatement ps, int index)
throws SQLException {
Person person = (Person) persons.get(index);
ps.setInt(0, person.getId().intValue());
ps.setString(1, person.getFirstName());
ps.setString(2, person.getLastName());
}
};
return jdbcTemplate.batchUpdate(sql, setter);
}
}
============

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

public class SpringJDBCDAOWithObject {
class InsertPerson extends SqlUpdate {
public InsertPerson(DataSource ds) {
setDataSource(ds);
setSql("insert into person (id, firstName, lastName) values (?, ?, ?)");
declareParameter(new SqlParameter(Types.NUMERIC));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}

public int insert(Person person) {
Object[] params = new Object[] { person.getId(),
person.getFirstName(), person.getLastName() };
return update(params);
}
}

private class PersonByIdQuery extends MappingSqlQuery {
public PersonByIdQuery(DataSource ds) {
super(ds, "select id, first_name, last_name from person "
+ "where id = ?");
declareParameter(new SqlParameter("id", Types.INTEGER));
compile();
}

public Object mapRow(ResultSet rs, int rowNumber) throws SQLException {
Person person = new Person();
person.setId((Integer) rs.getObject("id"));
person.setFirstName(rs.getString("first_name"));
person.setLastName(rs.getString("last_name"));
return person;
}
}

private InsertPerson insertPerson;

private PersonByIdQuery personByIdQuery;

public Person getPerson(Integer id) {
Object[] params = new Object[] { id };
return (Person) personByIdQuery.execute(params).get(0);
}

public int insertPerson(Person person) {
return insertPerson.insert(person);
}
}

 

推荐:Spring DAO with Jdbc and Hibernate

up vote 2 down vote favorite I was reading a paper which looked at investigating trends in monthly wind speed data for the past 20 years or so. The pa

import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import java.util.List; import

相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。