spring框架總結(04)----介紹的是Spring中的JDBC模板
1.1 Jdbc模板概述
它是spring框架中提供的一個對象,是對原始Jdbc API對象的簡單封裝。spring框架為我們提供了很多的操作模板類,入下圖所示:
我們今天的主角在spring-jdbc-4.24.RELEASE.jar中,我們在導包的時候,除了要導入這個jar包外,還需要導入一個spring-tx-4.2.4.RELEASE.jar(它是和事務相關的)。
1、Spring中的jdbc模板入門
1.1.1. 創建工程、引入jar包
1.1.2. 創建測試表
CREATE TABLE account(
id BIGINT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(40),
money DOUBLE
)CHARACTER SET utf8 COLLATE utf8_general_ci;
1.1.3. 創建測試類
註意:需要導入c3p0的jar包
public class TestJdbcTemplate {
@Test
public void test1(){
//創建jdbc模板對象
JdbcTemplate jdbcTemplate = new JdbcTemplate();
//創建c3p0數據源
ComboPooledDataSource dataSource = new
try {
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/spring_itheima10");
dataSource.setUser("root");
dataSource.setPassword("123456");
} catch (PropertyVetoException e) {
e.printStackTrace();
}
//設置數據源
jdbcTemplate.setDataSource(dataSource);
//插入操作
jdbcTemplate.update("insert into account(name,money) values(?,?)","張三",1000.0);
}
1.1.4. 將JdbcTemplate交給Spring管理(講某一對象或者圍著交給spring進行管理,需要的時候直接從註解中進行管理是實現,降低耦合性)
<!-- 配置JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</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/spring_itheima10"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
1.1.5. 在DAO中使用JdbcTemplate
n 創建AccountDao接口
public interface AccountDao {
public void save(Account account);
}
n 創建AccountDaoImpl實現類
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate jdbcTemplate;
}
@Override
public void save(Account account) {
this.jdbcTemplate.update("insert into account(name,money) values(?,?)",account.getName(),account.getMoney());
}
}
1.1.6. 把JdbcTemplate註入給DAO(然後再通過spring把持久層中需要的東西添加給這個表上的是直接從spring容器中獲取,不用從業務層中進行獲取)
<bean id="accountDao" class="cn.itcast.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</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/spring_itheima10"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
1.1.7. 編寫測試類
/**
* 測試保存
*/
@Test
public void test2(){
Account account = new Account();
account.setName("JAY");
account.setMoney(1000.0);
accountDao.save(account);
}
1.2. 配置DBCP連接池
1.2.1. 導入jar包
1.2.2. 配置DBCP連接池
<!-- 配置dbcp數據庫源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
1.3. 配置Spring自帶的數據庫連接池
<!-- 配置spring自帶的數據源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring_itheima10"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
總結:
三種方式:一種是c3p0數據源進行配置,一種是dbcp數據源進行配置,第三種Spring自帶的數據源進行配置實現這個過程中的數據。
1.4. 將數據庫連接信息保存到屬性文件中
1.4.1. 新建jdbc.properties屬性文件
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/hibernate_itcast55
jdbc.username=root
jdbc.password=123456
1.4.2. 在applicationContext.xml中引入jdbc.properties文件
n 配置bean引入
<!-- 配置bean引入jdbc.properties -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean>
<!-- 配置數據源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClass}"></property>
<property name="url" value="${jdbcUrl}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
n 通過context標簽引入
<!-- 通過context標簽引入jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
提示:此處不加classpath也行,因為jdbc.properties就放在類路徑下,就放在類一般加上。
總結:引入jdbc.properties配置文件的兩種方式:1、使用配置bean進行引入(第一種方式)
2、通過context方式進行引入
1.5. 使用Jdbc模板完成CRUD
1.5.1. 新增數據
/**
* JdbcTemplate之新增
*/
@Test
public void test1(){
jdbcTemplate.update("insert into account(name,money) values(?,?)","李四",1000);
}
1.5.2. 修改數據
/**
* JdbcTemplate之修改
*/
@Test
public void test2(){
jdbcTemplate.update("update account set money = ? where id = ?",1100,1);
}
1.5.3. 刪除數據
/**
* JdbcTemplate之刪除
*/
@Test
public void test3(){
jdbcTemplate.update("delete from account where id = ?",2);
}
查詢數據比較簡單此處省略
1.1.1.1. 查詢某列的值
/**
* JdbcTemplate之查詢某列的值
*/
@Test
public void test5(){
double money = jdbcTemplate.queryForObject("select money from account where id = ?", Double.class, 1);
System.out.println(money);
}
1.1.1.1. 查詢一個對象
n 創建實體類
public class Account implements Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private Double money;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account [id=" + id + ", name=" + name + ", money=" + money + "]";
}
}
n 創建RowMapper
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.getDouble("money"));
return account;
}
}
n 查詢得到一個對象
/**
* JdbcTemplate之查詢一個對象
*/
@Test
public void test4(){
AccountRowMapper mapper = new AccountRowMapper();
Account account = jdbcTemplate.queryForObject("select * from account where id = ?", mapper, 1);
System.out.println(account);
}
1.1.1.2. 查詢一個集合
/**
* JdbcTemplate之查詢一個集合
*/
@Test
public void test6(){
AccountRowMapper rowMapper = new AccountRowMapper();
List<Account> accounts = jdbcTemplate.query("select * from account", rowMapper);
for(int i = 0;i < accounts.size();i++){
System.out.println(accounts.get(i));
}
}
1.6. 在DAO中使用JdbcTemplate的兩種方式
1.6.1. 方式一:在DAO中直接註入JdbcTemplate
編寫DAO,註入JdbcTemplate
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void save(Account account) {
jdbcTemplate.update("insert into account(name,money) values(?,?)", account.getName(),account.getMoney());
}
}
把DAO配置到Spring中
<bean id="accountDao" class="cn.itcast.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
1.6.2. 方式二:在DAO中使用JdbcDaoSupport
讓DAO繼承JdbcDaoSupport
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public void save(Account account) {
this.getJdbcTemplate().update("insert into account(name,money) values(?,?)", account.getName(),account.getMoney());
}
}
給DAO註入DataSource
<bean id="accountDao" class="cn.itcast.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
比較:兩版Dao有什麽區別呢?
第一種在Dao類中定義JdbcTemplate的方式,適用於所有配置方式(xml和註解都可以)。
第二種讓Dao繼承JdbcDaoSupport的方式,只能用於基於XML的方式,註解用不了。
兩種dao類中定義jdbcTemplate的方式,適用於所有的配置方式,(xml和註解都是可以使用的一種方式)
2、第二大塊內容介紹----------------------------------------------Spring中的事務控制
事務的回顧:
l 事務的概念
n 事務是邏輯上一組操作,組成這組操作各個邏輯單元,要麽一起成功,要麽一起失敗。
l 事務的特性
n 原子性
n 一致性
n 隔離性
n 持久性
l 如果不考慮隔離性,引發安全問題
n 讀問題
u 臟讀
u 不可重復讀
u 虛讀
n 寫問題
u 丟失更新
l 解決讀問題
n 設置事務隔離級別
u read uncommitted
u read committed
u repeatable read
u Serializable
spring框架總結(04)----介紹的是Spring中的JDBC模板