Spring-JdbcTemplate
Spring-JdbcTemplate
JDBCTemplate:
Spring对JDBC薄薄的一层封装
使用:
-
配置数据源
- C3P0数据源
- DBCP数据源
- Spring内置数据源
-
引入外部文件,将连接信息存储到配置文件
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"/> </bean> <!--配置方式2--> <context:property-placeholder location="classpath:jdbc.properties"/> -
配置JDBCTamplate
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> -
增删改查
获取JdbcTemplate对象
//1.获取 Spring 容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2.根据 id 获取 bean 对象 JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate");增
jt.update("insert into account(name,money)values(?,?)","fff",5000);删
jt.update("delete from account where id = ?",6);改
jt.update("update account set money = money-? where id = ?",300,6);查询所有
List<Account> accounts = jt.query("select * from account where money > ? ", new AccountRowMapper(), 500); for(Account o : accounts){ System.out.println(o); }public class AccountRowMapper implements RowMapper<Account>{ @Override public Account mapRow(ResultSet rs, int rowNum) throws SQLException { Account account = new Account(); account.setId(rs.getInt("id")); account.setName(rs.getString("name")); account.setMoney(rs.getFloat("money")); return account; } }查询一个
//查询一个 ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); IEmpDao empdao = ac.getBean("empdao", IEmpDao.class); emp byId = empdao.findById(7034); System.out.println(byId);DAO层接口的实现类
public emp findByName(String name) { List<emp> query = jt.query("select * from emp where ename = ? ", new BeanPropertyRowMapper<emp>(emp.class), name); if (query.isEmpty()){ return null; } if (query.size()>1){ throw new RuntimeException("结果不唯一"); } return query.get(0); }查询一行一列
Integer total = jt.queryForObject ("select count(*) from account where money > ?",Integer.class,500); System.out.println(total);
在DAO中使用JdbcTemplate
方式一:
在类中定义属性,在配置文件中配置对象
private JdbcTemplate jt;
缺点:
多个类中会造成重复代码,即定义JdbcTemplate属性的代码和注入用的set方法
方式二:
JdbcDaoSupport 是 spring 框架为我们提供的一个类,该类中定义了一个 JdbcTemplate 对象,我们可以
直接获取使用,但是要想创建该对象,需要为其提供一个数据源:具体源码如下:
让dao继承JdbcDaoSupport;
public abstract class JdbcDaoSupport extends DaoSupport {
//定义对象
private JdbcTemplate jdbcTemplate;
//set 方法注入数据源,判断是否注入了,注入了就创建 JdbcTemplate
public final void setDataSource(DataSource dataSource) {
if (this.jdbcTemplate == null || dataSource != this.jdbcTemplate.getDataSource())
{
//如果提供了数据源就创建 JdbcTemplate
this.jdbcTemplate = createJdbcTemplate(dataSource);
initTemplateConfig();
}
}
//使用数据源创建 JdcbTemplate
protected JdbcTemplate createJdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
//当然,我们也可以通过注入 JdbcTemplate 对象
public final void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
initTemplateConfig();
}
//使用 getJdbcTmeplate 方法获取操作模板对象
public final JdbcTemplate getJdbcTemplate() {
return this.jdbcTemplate;
}
}

浙公网安备 33010602011771号