spring 整合JDBC和AOP事務(wù)的方法
spring整合JDBC
spring提供了很多模板整合Dao技術(shù)
| ORM持久化技術(shù) | 模板類(lèi) |
|---|---|
| JDBC | org.springframework.Jdbc.core.JdbcTemplate |
| Hibernate3.0 | org.springframework.orm.hiberate3.HibernateTemplate |
| IBatis(MyBatis) | org.springframework.orm.sqlMapClientTemplate |
| JPA | org.springframework.orm.jpa.JpaTemplate |
spring中提供了一個(gè)可以操作數(shù)據(jù)庫(kù)的對(duì)象.對(duì)象封裝了jdbc技術(shù).
// JDBCTemplate => JDBC模板對(duì)象
// 與DBUtils中的QueryRunner非常相似.
// 0 準(zhǔn)備連接池
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///hibernate_32");
dataSource.setUser("root");
dataSource.setPassword("1234");
// 1 創(chuàng)建JDBC模板對(duì)象
JdbcTemplate jt = new JdbcTemplate();
jt.setDataSource(dataSource);
// 2 書(shū)寫(xiě)sql,并執(zhí)行
String sql = "insert into t_user values(null,'rose') ";
jt.update(sql);
步驟
1.導(dǎo)包
- 基礎(chǔ)包4 + 2
- spring-test
- spring-aop
- junit4類(lèi)庫(kù)
- 新增c3p0連接池JDBC驅(qū)動(dòng)包
- spring-jdbc包
- spring-tx事務(wù)包
2.準(zhǔn)備數(shù)據(jù)庫(kù)
本地?cái)?shù)據(jù)庫(kù)和表
3.書(shū)寫(xiě)dao
// 使用JDBC模板實(shí)現(xiàn)增刪改查
public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
@Override
public void save(User u) {
String sql = "insert into t_user values(null,?) ";
super.getJdbcTemplate().update(sql, u.getName());
}
@Override
public void delete(Integer id) {
String sql = "delete from t_user where id = ? ";
super.getJdbcTemplate().update(sql,id);
}
@Override
public void update(User u) {
String sql = "update t_user set name = ? where id=? ";
super.getJdbcTemplate().update(sql, u.getName(),u.getId());
}
@Override
public User getById(Integer id) {
String sql = "select * from t_user where id = ? ";
return super.getJdbcTemplate().queryForObject(sql,new RowMapper<User>(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}}, id);
}
@Override
public int getTotalCount() {
String sql = "select count(*) from t_user ";
Integer count = super.getJdbcTemplate().queryForObject(sql, Integer.class);
return count;
}
@Override
public List<User> getAll() {
String sql = "select * from t_user ";
List<User> list = super.getJdbcTemplate().query(sql, new RowMapper<User>(){
@Override
public User mapRow(ResultSet rs, int arg1) throws SQLException {
User u = new User();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));
return u;
}});
return list;
}
}
4.spring配置
<!-- 指定spring讀取db.properties配置 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 1.將連接池放入spring容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
<property name="driverClass" value="${jdbc.driverClass}" ></property>
<property name="user" value="${jdbc.user}" ></property>
<property name="password" value="${jdbc.password}" ></property>
</bean>
<!-- 2.將JDBCTemplate放入spring容器 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource" ></property>
</bean>
<!-- 3.將UserDao放入spring容器 -->
<bean name="userDao" class="cn.zhli13.jdbctemplate.UserDaoImpl" >
<property name="jt" ref="jdbcTemplate" ></property>
</bean>
// 若UserDao類(lèi)繼承了JdbcDaoSupport,則無(wú)需將JDBCTemplate注入到容器中,省略第二步,第三步改為
<!-- 3.將UserDao放入spring容器 -->
<bean name="userDao" class="cn.zhli13.jdbctemplate.UserDaoImpl" >
<property name="dataSource" ref="dataSource" ></property>
</bean>
spring中aop事務(wù)
事務(wù)
事務(wù)特性:acid
事務(wù)并發(fā)問(wèn)題:臟讀、不可重復(fù)讀、幻讀
事務(wù)的隔離級(jí)別:1 讀未提交、2 讀已提交、4 可重復(fù)讀、8 串行化
spring封裝了事務(wù)管理代碼
事務(wù)操作:1、打開(kāi)事務(wù)、2、提交事務(wù)、3、回滾事務(wù)
事務(wù)操作對(duì)象
因?yàn)樵诓煌脚_(tái),操作事務(wù)的代碼各不相同.spring提供了一個(gè)接口
PlatformTransactionManager 接口
- DataSourceTransactionManager
- HibernateTransitionmanager
注意:在spring中玩事務(wù)管理.最為核心的對(duì)象就是TransactionManager對(duì)象
spring管理事務(wù)的屬性介紹
事務(wù)的隔離級(jí)別:1 讀未提交、2 讀已提交、4 可重復(fù)讀、8 串行化
是否只讀: true只讀 false可操作
事務(wù)的傳播行為:決定業(yè)務(wù)方法之間調(diào)用,事務(wù)應(yīng)該如何處理
* 保證同一個(gè)事務(wù)中
PROPAGATION_REQUIRED 支持當(dāng)前事務(wù),如果不存在 就新建一個(gè)(默認(rèn)、推薦99.99%用這種)
PROPAGATION_SUPPORTS 支持當(dāng)前事務(wù),如果不存在,就不使用事務(wù)
PROPAGATION_MANDATORY 支持當(dāng)前事務(wù),如果不存在,拋出異常
* 保證沒(méi)有在同一個(gè)事務(wù)中
PROPAGATION_REQUIRES_NEW 如果有事務(wù)存在,掛起當(dāng)前事務(wù),創(chuàng)建一個(gè)新的事務(wù)
PROPAGATION_NOT_SUPPORTED 以非事務(wù)方式運(yùn)行,如果有事務(wù)存在,掛起當(dāng)前事務(wù)
PROPAGATION_NEVER 以非事務(wù)方式運(yùn)行,如果有事務(wù)存在,拋出異常
PROPAGATION_NESTED 如果當(dāng)前事務(wù)存在,則嵌套事務(wù)執(zhí)行
spring管理事務(wù)方式
// 編碼式 // 1.將核心事務(wù)管理器配置到spring容器 <!-- 事務(wù)核心管理器,封裝了所有事務(wù)操作. 依賴(lài)于連接池 --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name="dataSource" ref="dataSource" ></property> </bean> // 2.配置TransactionTemplate模板 <!-- 事務(wù)模板對(duì)象 --> <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" > <property name="transactionManager" ref="transactionManager" > </property> </bean> // 3.將事務(wù)模板注入Service <!-- 3.Service--> <bean name="accountService" class="cn.itcast.service.AccountServiceImpl" > <property name="ad" ref="accountDao" ></property> <property name="tt" ref="transactionTemplate" ></property> </bean>
xml配置(aop)
// 1.導(dǎo)包
// aop、aspect、aopliance、aspect.weaver
// 2.導(dǎo)入新的約束(tx)
// beans: 最基本、context:讀取properties配置、aop:配置aop、tx:配置事務(wù)通知
// 3.配置通知
<!-- 配置事務(wù)通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<!-- 以方法為單位,指定方法應(yīng)用什么事務(wù)屬性
isolation:隔離級(jí)別
propagation:傳播行為
read-only:是否只讀
-->
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice>
// 4.配置將通知織入目標(biāo)
<!-- 配置織入 -->
<aop:config >
<!-- 配置切點(diǎn)表達(dá)式 -->
<aop:pointcut expression="execution(* cn.zhli13.service.*ServiceImpl.*(..))" id="txPc"/>
<!-- 配置切面 : 通知+切點(diǎn)
advice-ref:通知的名稱(chēng)
pointcut-ref:切點(diǎn)的名稱(chēng)
-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
</aop:config>
注解配置(aop)
// 1.導(dǎo)包(如xml配置一樣)
// 2.導(dǎo)入約束(同上)
// 3.開(kāi)啟注解管理事務(wù)
<!-- 開(kāi)啟使用注解管理aop事務(wù) -->
<tx:annotation-driven/>
// 4.使用注解
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
public class AccountServiceImpl implements AccountService {
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public void transfer(final Integer from,final Integer to,final Double money) {
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Springboot通過(guò)aop實(shí)現(xiàn)事務(wù)控制過(guò)程解析
- Spring如何基于aop實(shí)現(xiàn)事務(wù)控制
- 線(xiàn)程池中使用spring aop事務(wù)增強(qiáng)
- 解決springboot的aop切面不起作用問(wèn)題(失效的排查)
- Spring AOP執(zhí)行先后順序?qū)嵗斀?/a>
- springboot配置aop切面日志打印過(guò)程解析
- Spring Boot 通過(guò)AOP和自定義注解實(shí)現(xiàn)權(quán)限控制的方法
- SpringAOP事務(wù)配置語(yǔ)法及實(shí)現(xiàn)過(guò)程詳解
相關(guān)文章
Java 8 開(kāi)發(fā)的 Mybatis 注解代碼生成工具
MybatisAnnotationTools 是基于 Java8 開(kāi)發(fā)的一款可以用于自動(dòng)化生成 MyBatis 注解類(lèi)的工具,支持配置數(shù)據(jù)源、類(lèi)路徑,表名去前綴、指定類(lèi)名前后綴等功能.這篇文章主要介紹了Java 8 開(kāi)發(fā)的 Mybatis 注解代碼生成工具 ,需要的朋友可以參考下2019-07-07
hibernate-validator改進(jìn)校驗(yàn)框架validator?v0.4使用
這篇文章主要為大家介紹了改進(jìn)?hibernate-validator,新一代校驗(yàn)框架?validator?使用介紹?v0.4,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-03-03
完美解決request請(qǐng)求流只能讀取一次的問(wèn)題
這篇文章主要介紹了完美解決request請(qǐng)求流只能讀取一次的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Spring Boot集成spring-boot-devtools開(kāi)發(fā)時(shí)實(shí)現(xiàn)熱部署的方式
這篇文章主要介紹了Spring Boot集成spring-boot-devtools開(kāi)發(fā)時(shí)實(shí)現(xiàn)熱部署的方式,文中還給大家提到了spring boot 實(shí)現(xiàn)熱部署的方式及集成注意事項(xiàng),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05
利用Java實(shí)現(xiàn)圖片轉(zhuǎn)化為ASCII圖的示例代碼
本文將詳細(xì)講解如何利用 Java 實(shí)現(xiàn)圖片轉(zhuǎn)化為 ASCII 圖,從項(xiàng)目背景與意義、相關(guān)技術(shù)知識(shí),到系統(tǒng)需求與架構(gòu)設(shè)計(jì),再到詳細(xì)實(shí)現(xiàn)思路、完整代碼和代碼解讀,最后對(duì)項(xiàng)目進(jìn)行總結(jié)與展望,需要的朋友可以參考下2025-03-03
idea2020導(dǎo)入spring5.1的源碼詳細(xì)教程
這篇文章主要介紹了idea2020導(dǎo)入spring5.1的源碼的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
基于MockMvc進(jìn)行springboot調(diào)試(SpringbootTest)
這篇文章主要介紹了基于MockMvc進(jìn)行springboot調(diào)試(SpringbootTest),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
java去除重復(fù)對(duì)象的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇java去除重復(fù)對(duì)象的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
SpringMVC對(duì)自定義controller入?yún)㈩A(yù)處理方式
這篇文章主要介紹了SpringMVC對(duì)自定義controller入?yún)㈩A(yù)處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
使用Spring boot標(biāo)記一個(gè)方法過(guò)時(shí)
這篇文章主要介紹了使用Spring boot標(biāo)記一個(gè)方法過(guò)時(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12

