Spring与JDBC模板 为了避免直接使用JDBC而带来的复杂且冗长的代码,Spring提供了一个强有力的模板类---JdbcTemplate来简化JDBC操作。
Spring对于JDBC(JdbcTemplate)模板的使用,是IoC的应用,是将JDBC模板对象注入给了Dao层的实现类。
public class StarDaoImpl extends JdbcDaoSupport implements StarDao {
@Override
public int insertStar(Star star) {
String sql = "insert into star(name,age,score) values(?,?,?)";
return this.getJdbcTemplate().update(sql, star.getName(),star.getAge(),star.getScore());
}
@Override
public int deleteById(int id) {
String sql = "delete from star where id=?";
return this.getJdbcTemplate().update(sql, id);
}
@Override
public int updateStar(Star star) {
String sql = "update star set name=?,age=?,score=? where id=?";
return this.getJdbcTemplate().update(sql,star.getName(),star.getAge(),star.getScore(),star.getId());
}
@Override
public List<Star> selectAll() {
String sql = "select id,name,age,score from star";
return this.getJdbcTemplate().query(sql, new RowMapperUtil());
}
@Override
public String selectNameById(int id) {
String sql = "select name from star where id=?";
return this.getJdbcTemplate().queryForObject(sql, String.class,id);
}
@Override
public List<String> selectAllName() {
String sql = "select name from star";
return this.getJdbcTemplate().queryForList(sql, String.class);
}
}
注意需要写RowMapper接口的实现类,建立实体类和数据库表的映射关系
public class RowMapperUtil implements RowMapper<Star>{
@Override
public Star mapRow(ResultSet rs, int rowNum) throws SQLException {
Star star = new Star();
star.setId(rs.getInt("id"));
star.setName(rs.getString("name"));
star.setAge(rs.getInt("age"));
star.setScore(rs.getDouble("score"));
return star;
}
}
Spring配置文件
<?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:context="http://www.springframework.org/schema/context"
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.springframework.org/schema/context/spring-context.xsd">
<!-- 注册starDaoImpl实体类 -->
<bean id="starDaoImpl" class="com.bjsxt.dao.impl.StarDaoImpl">
<!-- 方式一 设置注入(直接配置数据源)dataSource -->
<property name="dataSource" ref="dataSource"></property>
<!-- 方式二 设值注入(JDBC模板)jdbcTemplate-->
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!-- 注册JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 加载jdbc数据库配置文件 -->
<context:property-placeholder location="config.properties"></context:property-placeholder>
<!-- 注册数据源(DBCP) -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="username" value="${jdbc.username}"></property>
</bean>
</beans>
Tips:数据源的配置(Spring默认数据源DriverManagerDataSource 、DBCP数据源BasicDataSource(dbcp和pooljar包) 、C3P0数据源ComboPooledDataSource(c3p0包)) ,C3P0数据源要注意属性名为:driverClass、jdbcUrl、user、password
网友评论