## 配置
~~~
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--掃描包-->
<context:component-scan base-package="com.like"/>
<!--配置JDBCTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置數據源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.10.10:3306/jdbc"/>
<property name="username" value="homestead"/>
<property name="password" value="secret"/>
</bean>
<!-- spring中基于XML的聲明式事務控制配置步驟
1.配置事務管理器
2.配置事務的通知
此時我們需要導入事務的約束,tx名稱空間和約束,同時也需要AOP的
使用tx:advice標簽配置事務通知
屬性:
id:給事務起唯一標識
transaction-manager:給事務通知聽一個事務管理器飲引用
3.配置AOP中的通用切入點表達式
4.建立事務通知和切入點表達式的對應關系
5.配置事務的屬性
在事務tx:advice標簽的內部
-->
<!-- 1.配置事務管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事務的通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--transfer表示業務實現層的一個方,它需要有一些屬性
isolation:用于指定事務的隔離級別.默認值是default,表示使用數據庫的默認隔離級別
propagation:用于指定事務的傳播行為,默認值是REQUIRED,表示一定會有事務,增刪改的選擇.查詢方法可以選擇SUPPORTS
read-only:用于指定事務是否可讀.只有查詢方法才能設置為true.默認值是false,表示讀寫.
timeout:用于指定事務的超時時間,默認值是-1,表示永不超時.如果指定了數值,以秒為單位.
rollback-for:用于指定一個異常,當產生該異常時,事務回滾,產生其他異常時,事務不回滾,沒有默認值,表示任何異常都回滾.
no-rollback-for:用于指定一個異常,當產生該異常時,事務不回滾,產生其他異常時,事務回滾.沒有默認值,表示任何異常都回滾.
-->
<tx:method name="transfer" propagation="REQUIRED" read-only="false"/>
<!--針對以find開頭的方法使用以下配置-->
<!-- <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>-->
</tx:attributes>
</tx:advice>
<!--配置AOP-->
<aop:config>
<aop:pointcut id="pt1" expression="execution(* com.like.service.impl.*ServiceImpl.*(..))"/>
<!--建立事務通知和切入點表達式的對應關系-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config>
</beans>
~~~
## 代碼
~~~
package com.like.domain;
import java.io.Serializable;
public class Account implements Serializable
{
private Integer id;
private String name;
private Float money;
@Override
public String toString()
{
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + 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;
}
}
~~~
~~~
package com.like.dao;
import com.like.domain.Account;
public interface IAccountDao
{
Account findAccountByName(String name);
void updateAccount(Account account);
}
~~~
~~~
package com.like.dao.impl;
import com.like.dao.IAccountDao;
import com.like.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class AccountDaoImpl implements IAccountDao
{
@Autowired
private JdbcTemplate jt;
public Account findAccountByName(String name)
{
List<Account> accounts = jt.query("select * from account where name = ?", new BeanPropertyRowMapper<Account>(Account.class), name);
if (accounts.isEmpty()) {
return null;
}
if (accounts.size() > 1) {
throw new RuntimeException("結果集不唯一");
}
return accounts.get(0);
}
public void updateAccount(Account account)
{
jt.update("update account set money = ? where name = ?", account.getMoney(), account.getName());
}
}
~~~
~~~
package com.like.service;
public interface IAccountService
{
void transfer(String sourceName,String targetName,Float money);
}
~~~
~~~
package com.like.service.impl;
import com.like.dao.IAccountDao;
import com.like.domain.Account;
import com.like.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AccountServiceImpl implements IAccountService
{
@Autowired
private IAccountDao accountDao;
public void transfer(String sourceName, String targetName, Float money)
{
Account source = accountDao.findAccountByName(sourceName);
Account target = accountDao.findAccountByName(targetName);
source.setMoney(source.getMoney() - money);
target.setMoney(target.getMoney() + money);
accountDao.updateAccount(source);
int a = 1/0;
accountDao.updateAccount(target);
}
}
~~~
## 測試
~~~
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IAccountService accountServiceImpl = (IAccountService)context.getBean("accountServiceImpl");
accountServiceImpl.transfer("milan","jack",500f);
~~~
- Maven
- 概述
- 常用命令
- 生命周期
- scope詳解
- maven概念模型圖
- IDEA創建maven工程
- 創建maven web項目
- 沖突解決
- pom文件標簽詳解
- maven工程拆分與聚合的思想
- 父子工程的創建
- 工程和模塊的關系以及集成和依賴的概念
- 父子工程添加依賴
- 父子工程的三種啟動方式
- Struts2
- 執行流程
- 配置
- action的使用
- 獲取Servlet的API
- result標簽的視圖配置
- struts2屬性封裝
- struts2模型封裝
- OGNL
- 值棧(ValueStack)
- 值棧context區存數據
- 值棧root區
- struts2對el的增強
- #,%,$符號使用
- 值棧擴展
- 攔截器
- 自定義攔截器
- 方法攔截器
- 注解方式
- Hibernate
- 配置
- 簡單crud
- 持久化類編寫規范
- OID
- 持久化類三種狀態
- 一級緩存
- 查詢
- session與當前線程綁定
- hibernate一對多配置
- 冗余SQL語句的原因
- hibernate多對多配置
- 級聯操作
- 對象導航查詢
- JPA
- JPA單表CRUD
- JPA多種查詢
- JPA一對多關系映射
- JPA一對多操作
- JPA多對多關系映射
- JPA多對多操作
- QBC查詢
- 離線條件查詢(DetachedCriteria)
- SpringMVC
- 環境搭建
- 常用注解
- 請求參數綁定
- 綁定基本類型和字符串
- 綁定實體類型
- 解決中文亂碼
- 綁定集合類型
- 自定義類型轉換器
- 獲取Servlet的API
- 響應字符串
- 響應void
- 響應ModelAndView
- 響應forward和redirect
- 響應過濾靜態資源
- 響應json
- 文件上傳基礎
- SpringMVC上傳文件
- 跨服務器文件上傳
- 攔截器
- SpringMVC異常
- ControllerAdvice
- SpringMVC默認處理方式
- 概述
- @ExceptionHandler
- 消息轉換器
- SpringMVC跨域
- Spring
- 概述
- IoC快速入門
- ApplicationContext三個常用實現類
- beanFactory和ApplicationContext區別
- bean創建三種方式
- POJO和Javabean的區別
- bean作用范圍
- bean對象生命周期
- spring的依賴注入
- DI的屬性注入方式
- 常用注解
- Spring新注解
- Spring整合連接池
- Spring的IOC注解配置
- Spring完全使用注解
- Spring整合junit
- AOP
- AOP配置
- JDBCTemplate
- JDBCTemplate在IOC中使用
- JDBCTemplate的CRUD
- JDBCTemplate在dao中使用
- 聲明式事務
- spring事務API
- 事務XML配置
- 事務注解配置
- 全注解事務
- Spring編程式事務
- 整合SSH(XML版本)
- 整合SSH(半XML半注解)
- Spring5新特性
- MyBatis
- 概述
- 用maven創建MyBatis
- 將數據庫配置單獨文件
- typeAliases標簽
- MyBatis的CRUD
- MyBatis實現Dao層開發
- 使用dao和代理類的區別
- MyBatis連接池
- MyBatis事務
- MyBatis動態SQL
- 多表操作
- 多表一對一
- 多表一對多
- 多表多對多
- JNDI
- 延遲加載和立即加載
- 延遲加載
- 一級緩存
- 二級緩存
- MyBatis注解
- MyBatis注解CRUD
- 注解實體類屬性和字段對應關系
- MyBatis注解一對一和一對多
- MyBatis注解二級緩存
- SSM整合
- 搭建環境
- SpringBoot
- SpringBoot核心功能
- 快速入門
- SpringBoot配置文件
- yml配置文件語法
- 配置文件與配置類的屬性映射方式
- 端口和映射路徑
- 日志級別
- 訪問靜態資源
- SpringBoot注入方式一
- SpringBoot注入方式二
- 攔截器
- HikariCP連接池
- SpringBoot集成MyBatis
- 通用mapper
- SpringBoot事務
- SpringBoot集成Junit
- SpringBoot集成DataJPA
- SpringBoot集成Redis
- 使用SpringBoot提供的測試啟動類
- 使用MockMvc
- SpringCloud
- RestTemplate
- Eureka概述
- Eureka快速入門
- Eureka集群
- Eureka客戶端
- Eureka失效剔除和自我保護
- 負載均衡Ribbon
- Hystrix
- Hystrix服務降級
- Hystrix服務熔斷
- Feign
- Feign的熔斷機制
- Feign的請求壓縮和日志級別
- Zuul網關
- Zuul快速入門
- Zuul路由規則
- Zuul過濾器
- Zuul自定義過濾器
- Zuul負載均衡和熔斷
- Zuul高可用
- Zuul網關緩存
- SpringSecurity
- 快速入門
- SpringSecurity使用數據庫認證