JDBCTemplate
持久层总图.jpg需要依赖
- spring-context
- spring-jdbc
- spring-tx : spring事务支持
- mysql-connector-java:建议低版本5.1.0
JdbcTemplate基本用法
%增、删、改 使用update(),然后不同的sql语句
jdbcTemplate.update("insert into account(name,price) values(?,?)",name,price,id);
jdbcTemplate.update("delete from account where id=?",id);
jdbcTemplate.update("update account set name=?, price=? where id=?",name,price,id);
%查不太一样,使用query,返回List<T>,参数需要RowMapper<T>的实现类BeanPropertyRowMapper<T>,这里就不用自己写类了
%BeanPropertyRowMapper使用方法:BeanPropertyRowMapper<T>(T.class)
jdbcTemplate.query("select * from account where id=?",new BeanPropertyRowMapper<Account>(Account.class),id);
%使用query返回一行一列,对List进行检查
List<Account> accounts=jdbcTemplate.query("select * from account where id=?",new BeanPropertyRowMapper<Account>(Account.class),id);
return accounts.isEmpty()?null:accounts.get(0)
基于spring ioc-xml的JDBCTemplate配置
dao层使用声明:
public class AccountDao implements IAccountDao {
JdbcTemplate jdbcTemplate;
bean.xml配置bean:
<!-- 配置dao,注入jdbcTemplate -->
<bean id="accountDao" class="com.chajiu.dao.Impl.AccountDao">
<property name="jdbcTemplate" ref="jdbc"></property>
</bean>
<!-- 配置jdbcTemplate,注入dataSource -->
<bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<!-- 配置dataSource,注入连接属性 -->
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/account"></property>
<property name="username" value="root"></property>
<property name="password" value="admin"/>
</bean>
JdbcTemplate
对象此处使用spring的内置的数据源DriverManagerDataSource
,在数据源中配置连接字符串。也可以使用别的数据源,如c3p0.ComboPooledDataSource
,dbcp等。
测试
public class JDBCTemplateTest1 {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
IAccountDao dao=applicationContext.getBean("accountDao", IAccountDao.class);
Account account=new Account();
account.setName("cjc");
account.setPrice(1000f);
account.setId(2);
// dao.addAccount(account);
// Account account1=dao.findAccountById(2);
// System.out.println(account1);
// System.out.println(dao.findAccountByName("ddd"));
// dao.updateAccount(account);
dao.deleteAccount(2);
}
}
commons-dbutils包
最上面那张图可以看出,除了JDBCTemplate
,还可以用commons-dbutils
配置持久层。
先导包,下面是新增的:
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
使用QueryRunner对象进行CRUD:
-
增、删、改
-
同样使用update(sql,args...)
public void saveAccount(Account account) { try{ queryRunner.update("insert into account(name,price) values(?,?) ",account.getName(),account.getPrice()); }catch (Exception e){ throw new RuntimeException(e); } } public void updateAccount(Account account) { try{ queryRunner.update("update account set name=?,price=? where id=?",account.getName(),account.getPrice(),account.getId()); }catch (Exception e){ throw new RuntimeException(e); } } public void deleteAccount(Integer id) { try{ queryRunner.update("delete from account where id = ?",id); }catch (Exception e){ throw new RuntimeException(e); } }
-
-
查询
-
使用query,查询List用
BeanListHandler<T>
,查询一个用BeanHandler<T>
public List<Account> findAllAccounts() { try{ return queryRunner.query("select * from account",new BeanListHandler<Account>(Account.class)); }catch (Exception e){ throw new RuntimeException(e); } } public Account findAccountById(Integer id) { try{ return queryRunner.query("select * from account where id = ?",new BeanHandler<Account>(Account.class),id); }catch (Exception e){ throw new RuntimeException(e); } }
-
bean.xml配置:
- 使用了
c3p0.ComboPooledDataSource
作为数据源,有别于前面的DriverManagerDataSource
- 数据源配置过程基本一致
<!-- 配置Dao对象-->
<bean id="accountDao" class="com.chajiu.dao.AccountDAOImpl" scope="prototype">
<!-- 注入QueryRunner-->
<property name="queryRunner" ref="queryRunner"></property>
</bean>
<!-- 配置QueryRunner-->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<!-- 注入数据源-->
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<!-- 配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 连接数据库的必备信息-->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/account"></property>
<property name="user" value="root"></property>
<property name="password" value="admin"></property>
</bean>
网友评论