Spring的JDBC的有什么用?
(打X表示要做的事)
使用了Spring的JDBC,我们只需要负责:配置数据库连接参数,定义SQL(包含设置参数),处理结果集。
Spring的JDBC大大简化了开发人员对数据库的操作,使得开发人员可以从繁琐的数据库操作中解脱出来,
从而将更多的精力投入到编写业务逻辑中。
Spring对ORM框架的支持
Spring的JDBC的使用
· 依赖的jar包
spring-jdbc,spring-tx,数据库驱动包,连接池相关jar包(下文使用的是Driud连接池)。
· 配置数据库连接参数
1、让Spring管理DataSource对象
阅读DruidDataSource类源代码:
package com.alibaba.druid.pool; public abstract class DruidAbstractDataSource extends WrapperAdapter implements DruidAbstractDataSourceMBean, DataSource, DataSourceProxy, Serializable { public void setUsername(String username) { /* 具体代码略 */ } public void setPassword(String password) { /* 具体代码略 */ } public void init() throws SQLException { /* 具体代码略 */ } public void close() { /* 具体代码略 */ } // 其余代码略 }
可发现DruidDataSource提供了数据库相关配置的属性的setter方法,和init、close方法。
其中
则可以在Spring的xml配置文件中对属性进行注入。
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql:///spring_demo" /> <property name="username" value="root" /> <property name="password" value="你的密码" /> </bean>
2、属性占位符的使用
上文,数据库连接相关配置在Spring的配置文件中。而一般都会将数据库的配置放置在properties文件中。
使用<context:property-placeholder>标签可以方便我们,使用Spring读取properties文件中的数据库配置参数。
如下,在Spring的xml配置文件中配置:
<context:property-placeholder location="classpath:db.properties" />
其中location属性为数据库配置文件的地址值(配合classpath前缀)
而后,DataSource的property中value属性值就可以使用占位符了。
配置效果如下
<context:property-placeholder location="classpath:db.properties" /> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>
数据库配置文件: db.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///spring_study jdbc.username=root jdbc.password=你的密码
上文为演示参数占位符,所以在db.properties中的key加上了jdbc前缀。
若不加jdbc前缀,要配置system-properties-mode属性值为NEVER。否则Spring回去找当前系统的property。
· JdbcTemplate的使用
1、创建JdbcTemplate实例对象
阅读源代码,可发现创建JdbcTemplate对象需要一个DataSource对象。
public class JdbcTemplate extends JdbcAccessor implements JdbcOperations { public JdbcTemplate(DataSource dataSource) { this.setDataSource(dataSource); this.afterPropertiesSet(); } }
在DAO实现类中,通过让Spring注入DataSource属性来创建JdbcTemplate对象。
@Repository public class EmployeeDAOImpl implements IEmployeeDAO { private JdbcTemplate jdbcTemplate; @Autowired public void setDataSource(DataSource ds){ this.jdbcTemplate = new JdbcTemplate(ds); }
2、JdbcTemplate中常用的实例方法
update方法
用于增、删、改操作。
query方法
其RowMapper参数用于处理结果集。
实现其mapRow方法,操作每一行的结果集。如下例:
@Override public List<Employee> list() { List<Employee> list = this.jdbcTemplate.query("SELECT * FROM employee", new RowMapper<Employee>() { @Override public Employee mapRow(ResultSet resultSet, int i) throws SQLException { Employee e = new Employee(); e.setId(resultSet.getLong("id")); e.setName(resultSet.getString("name")); System.out.println(i); return e; } }); return list; }
该方法也可以设置参数,如下例 :
@Override public Employee get(Long id) { List<Employee> list = this.jdbcTemplate .query("SELECT * FROM employee WHERE id = ?", new RowMapper<Employee>() { @Override public Employee mapRow(ResultSet resultSet, int i) throws SQLException { Employee e = new Employee(); e.setId(resultSet.getLong("id")); e.setName(resultSet.getString("name")); System.out.println(i); return e; } },id ); return list.size() == 1 ? list.get(0) : null; }
queryForObject方法
可以设置返回的类型。如下例:
@Override public Long getTotalCount() { Long totalCount = this.jdbcTemplate .queryForObject("SELECT COUNT(*) FROM employee", Long.class); return totalCount; }
也可以处理结果集。返回的是单个对象。如下例:
@Override public Employee get(Long id) { Employee e = this.jdbcTemplate.queryForObject("SELECT * FROM employee WHERE id = ?", new RowMapper<Employee>() { @Override public Employee mapRow(ResultSet resultSet, int i) throws SQLException { Employee e = new Employee(); e.setId(resultSet.getLong("id")); e.setName(resultSet.getString("name")); System.out.println(i); return e; } }, id); return e; }
· JdbcDaoSupport
该类封装了JdbcTemplate对象和DataSource的setter方法。
public abstract class JdbcDaoSupport extends DaoSupport { private JdbcTemplate jdbcTemplate; public JdbcDaoSupport() { } public final void setDataSource(DataSource dataSource) { if (this.jdbcTemplate == null || dataSource != this.jdbcTemplate.getDataSource()) { this.jdbcTemplate = this.createJdbcTemplate(dataSource); this.initTemplateConfig(); } } // 其余代码略 }
我们自定义的DAO实现类只要继承JdbcDAOSupport类,
通过Spring的XML配置文件注入dataSource属性,就可以通过super获取到JdbcTemplate对象,
使用JdbcTemplate中的方法。
(若想使用注解方式,需将JdbcDaoSupport中的如上文所示的部分代码,复制到自定义的DAO实现类中)