Spring中的JdbcTemplate JdbcTemplate:他是spring框架中提供的一個對象,是對原始Jdbc API對象的簡單封裝。 JdbcTemplate的作用:用於和資料庫交互的,實現對錶的CRUD操作 JdbcTemplate的入門 導入相關的坐標 賬戶實體類的建立 操作資料庫 ...
Spring中的JdbcTemplate
JdbcTemplate:他是spring框架中提供的一個對象,是對原始Jdbc API對象的簡單封裝。
JdbcTemplate的作用:用於和資料庫交互的,實現對錶的CRUD操作
JdbcTemplate的入門
導入相關的坐標
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
</dependencies>
賬戶實體類的建立
package com.itheima.domain;
import java.io.Serializable;
//賬戶的實體類
public class Account implements Serializable {
private Integer id;
private String name;
private Float money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
操作資料庫
package com.itheima.jdbctemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
//JdbcTemplate的最基本用法
public class JdbcTemplateDemo1 {
public static void main(String[] args) {
//準備數據源:spring內置數據源
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/eesy");
ds.setUsername("root");
ds.setPassword("12345");
//創建JdbcTemplate對象
JdbcTemplate jt = new JdbcTemplate();
//給jt設置數據源
jt.setDataSource(ds);
//執行操作
jt.execute("insert into account(name,money)values('ccc',1000)");
}
}
JdbcTemplate在spring的ioc中使用
配置bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置JdbcTemplate-->
<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置數據源-->
<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/eesy"></property>
<property name="username" value="root"></property>
<property name="password" value="12345"></property>
</bean>
</beans>
測試是否可以操作資料庫
報錯:
NoSuchBeanDefinitionException: No bean named 'jdbcTemplate' is defined
原因:xxxx沒有定義,區分大小寫:
package com.itheima.jdbctemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
//JdbcTemplate的最基本用法
public class JdbcTemplateDemo1 {
public static void main(String[] args) {
//獲取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//獲取對象
JdbcTemplate jt = ac.getBean("JdbcTemplate",JdbcTemplate.class);
//執行操作
jt.execute("insert into account(name,money)values('ddd',13240)");
}
}
spring中基於xml的聲明事務控制步驟
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置業務層-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置賬戶的持久層-->
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置數據源-->
<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/eesy"></property>
<property name="username" value="root"></property>
<property name="password" value="12345"></property>
</bean>
<!--spring中基於xml的聲明事務控制步驟步驟-->
<!--1.配置事務管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--2.配置事務的通知:
需要導入事務的約束
屬性id:給事務通知一個唯一標識
屬性transaction-manager:給事務通知提供一個事務管理器引用
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--配置事務的屬性
isolation:用於指定事務的隔離級別,預設值是DEFAULT,表示使用資料庫的預設隔離級別
propagation:用於指定事務的傳播行為。預設值是REQUIRED,表示一定會有事務,增刪改的選擇。查詢方法可以選擇SUPPORTS
read-only:用於指定事務是否只讀。只有查詢方法才能設置true.預設值是false.表示只讀
timeout:用於指定事務的超時時間,預設值-1.表示永不超時。如果指定了數值,以秒為單位
rollback-for:用於指定一個異常,當產生該異常是,事務回滾,產生其他異常是,事務部回滾,沒有預設值,表示任何異常都回滾
no-rollback-for:用於指定一個異常,當產生該異常時,事務不回滾,產生其他異常時事務回滾,沒有預設值。表示任何異常都回滾
-->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
<!--3.配置AOP中的通用切入點表達式
-->
<aop:config>
<aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut>
<!--4.建立事務通知和切入點表達式的對應關係-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
</beans>