0914)spring中的JdbcTemplate
jdbcTemplate的作用:他就是用於和數據庫交互的,實現對錶的CRUD
1)基於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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
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/spring"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- spring中基於XML的聲明式事務控制配置步驟
1、配置事務管理器
2、配置事務的通知
此時我們需要導入事務的約束 tx名稱空間和約束,同時也需要aop的
使用tx:advice標籤配置事務通知
屬性:
id:給事務通知起一個唯一標識
transaction-manager:給事務通知提供一個事務管理器引用
3、配置AOP中的通用切入點表達式
4、建立事務通知和切入點表達式的對應關係
5、配置事務的屬性
是在事務的通知tx:advice標籤的內部
-->
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事務的通知-->
<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>
<!-- 配置aop-->
<aop:config>
<!-- 配置切入點表達式-->
<aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut>
<!--建立切入點表達式和事務通知的對應關係 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
</beans>
2)基於註解的事務配置
就是將xml中的bean使用註解來表示
3)認識Junit5
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
這看上去比Junit4 複雜,實際上在導入包時也會複雜一些。
JUnit Platform是在JVM上啓動測試框架的基礎。
JUnit Jupiter是JUnit5擴展的新的編程模型和擴展模型,用來編寫測試用例。Jupiter子項目爲在平臺上運行Jupiter的測試提供了一個TestEngine (測試引擎)。
JUnit Vintage提供了一個在平臺上運行JUnit 3和JUnit 4的TestEngine 。
推薦閱讀:https://zhuanlan.zhihu.com/p/55648107
4)spring配置數據源
1)C3P0
<bean id=“dataSource” class=“com.mchange.v2.c3p0.ComboPooledDataSource”>
<property name=“driverClass” value=“com.mysql.jdbc.Driver”>
<property name=“jdbcUrl” value=“jdbc:mysql:///spring_day02”>
<property name=“user” value=“root”>
<property name=“password” value=“1234”>
2)DBCP
<bean id=“dataSource” class=“org.apache.commons.dbcp.BasicDataSource”>
<property name=“driverClassName” value=“com.mysql.jdbc.Driver”>
<property name=“url” value=“jdbc:mysql:// /spring_day02”>
<property name=“username” value=“root”>
<property name=“password” value=“1234”>
3)spring內置數據源
<bean id=“dataSource”
class=“org.springframework.jdbc.datasource.DriverManagerDataSource”>
<property name=“driverClassName” value=“com.mysql.jdbc.Driver”>
<property name=“url” value=“jdbc:mysql:///spring_day02”>
<property name=“username” value=“root”>
<property name=“password” value=“1234”>
5)spring配置中如何引入外部文件
一種方式
<bean
class=“org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
<property name=“location” value=“classpath:jdbc.properties”/>
另一種方式
<context:property-placeholder location=“classpath:jdbc.properties”/>
6)在配置文件中配置JdbcTemplate
<bean id=“jdbcTemplate” class=“org.springframework.jdbc.core.JdbcTemplate”>
<property name=“dataSource” ref=“dataSource”>
<bean id=“dataSource”
class=“org.springframework.jdbc.datasource.DriverManagerDataSource”>
<property name=“driverClassName” value=“com.mysql.jdbc.Driver”>
<property name=“url” value=“jdbc:mysql:///spring_day02”>
<property name=“username” value=“root”>
<property name=“password” value=“1234”>
7)spring的jdbcTemplate的使用
public static void main(String[] args) {
//1.獲取 Spring 容器
ApplicationContext ac = new ClassPathXmlApplicationContext(“bean.xml”);
//2.根據 id 獲取 bean 對象
JdbcTemplate jt = (JdbcTemplate) ac.getBean(“jdbcTemplate”);
//3.執行操作
jt.execute(“insert into account(name,money)values(‘eee’,500)”);
}
8)在 dao 中使用 JdbcTemplate
方式一 在dao中定義JdbcTemplate
<?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">
<bean id=“accountDao” class=“com.itheima.dao.impl.AccountDaoImpl”>
<property name=“jdbcTemplate” ref=“jdbcTemplate”>
<bean id=“jdbcTemplate” class=“org.springframework.jdbc.core.JdbcTemplate”>
<property name=“dataSource” ref=“dataSource”>
傳智播客——專注於 Java、.Net 和 Php、網頁平面設計工程師的培訓
北京市昌平區建材城西路金燕龍辦公樓一層 電話:400-618-9090
<bean id=“dataSource”
class=“org.springframework.jdbc.datasource.DriverManagerDataSource”>
<property name=“driverClassName”
value=“com.mysql.jdbc.Driver”>
<property name=“url” value=“jdbc:mysql:///spring_day04”>
<property name=“username” value=“root”>
<property name=“password” value=“1234”>
方式二 讓dao繼承JdbcDaoSupport
[外鏈圖片轉存失敗(img-xSusdiOq-1569134218326)(C:\Users\15566\AppData\Roaming\Typora\typora-user-images\1569129310897.png)]