Aop與JDBC
阿新 • • 發佈:2018-12-14
Aop1
一、Aop底層實現
(一)設定異常增強的步驟
- spring 設定增強bean
- 設定異常增強
- 首先設定切面(類似蛋糕的截面)
- 設定切點
- 切面與增強bean進行關聯(引入增強bean)
<aop:aspect ref="aopError"></aop:aspect>
- 設定異常增強
<aop:after-throwing method="affterError" pointcut-ref="pointcut" throwing="e"></aop:after-throwing>
(二)詳解
(1)所建包
(2)所需jar包
(3)完整程式碼
com.offcn.aop
package com.offcn.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import java.util.Arrays; public class AopError { /*JoinPoint 這個類所有動態代理類的資訊*/ public void affterError(JoinPoint joinPoint, RuntimeException e){ /*joinPoint.getSignature() 這個方法是拿到你的動態代理的簽名檔案*/ System.out.println(joinPoint.getSignature().getName()+"這個方法異常資訊"+e.getMessage()); } public void after(JoinPoint joinPoint){ System.out.println(joinPoint.getSignature().getName()); } /*環繞增強 傳遞的是其JoinPoint子類*/ public void around(ProceedingJoinPoint proceedingJoinPoint){ System.out.println(proceedingJoinPoint.getTarget()+ Arrays.toString(proceedingJoinPoint.getArgs())); /*呼叫此方法執行目標物件相應的方法*//*這個方法又相當於一個分割線,也就是前置與後置的分割線*/ try { proceedingJoinPoint.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } System.out.println(proceedingJoinPoint.getSignature().getName()); } }
dao及其實現類 com.offcn.dao和impl
package com.offcn.dao;
public interface UserDao {
public void add();
}
package com.offcn.dao.impl; import com.offcn.dao.UserDao; public class UserDaoImpl implements UserDao { @Override public void add() { System.out.println("增加成功"); // throw new RuntimeException("出錯了!"); } }
service和impl
package com.offcn.service;
public interface UserService {
public void add();
}
package com.offcn.service;
import com.offcn.dao.UserDao;
import com.offcn.service.UserService;
public class UserServiceDaoImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void add() {
userDao.add();
}
}
test
package com.offcn.test;
import com.offcn.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void test(){
//載入xml檔案
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userServiceDao = (UserService) app.getBean("userService");
userServiceDao.add();
}
}
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!--設定dao層-->
<bean id="userDao" class="com.offcn.dao.impl.UserDaoImpl"></bean>
<!--設定Service層-->
<bean id="userService" class="com.offcn.service.UserServiceDaoImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!--設定增強aop-->
<bean id="aopError" class="com.offcn.aop.AopError"></bean>
<aop:config>
<!--對那一方法或者哪一個類進行植入--><!--expression 每個引數對哪一個類進行注入,這句話execution代表所有方法,*代表全部方法-->
<aop:pointcut id="pointcut" expression="execution(* com.offcn.service.UserService.*(..))"></aop:pointcut>
<!--引入方法增強bean-->
<aop:aspect ref="aopError">
<!--在這個裡面可以配置--><!--這個方法必須對應引入增強bean的方法,在這裡面要設定切點-->
<!--<aop:after-throwing method="affterError" pointcut-ref="pointcut" throwing="e"></aop:after-throwing>-->
<!--後置增強-->
<!--<aop:after method="after" pointcut-ref="pointcut"></aop:after>-->
<!--前置增強-->
<!--<aop:before method="."-->
<!--環繞增強,在前後都做相關操作,相當於一個前置和一個後者增強-->
<aop:around method="around" pointcut-ref="pointcut"></aop:around>
</aop:aspect>
</aop:config>
</beans>
(4)程式碼執行流程
- 先執行Test
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
- 執行applicationContext.xml
<aop:config>
<!--對那一方法或者哪一個類進行植入--><!--expression 每個引數對哪一個類進行注入,這句話execution代表所有方法,*代表全部方法-->
<aop:pointcut id="pointcut" expression="execution(* com.offcn.service.UserService.*(..))"></aop:pointcut>
<!--引入方法增強bean-->
<aop:aspect ref="aopError">
<!--在這個裡面可以配置--><!--這個方法必須對應引入增強bean的方法,在這裡面要設定切點-->
<!--<aop:after-throwing method="affterError" pointcut-ref="pointcut" throwing="e"></aop:after-throwing>-->
<!--後置增強-->
<!--<aop:after method="after" pointcut-ref="pointcut"></aop:after>-->
<!--前置增強-->
<!--<aop:before method="."-->
<!--環繞增強,在前後都做相關操作,相當於一個前置和一個後者增強-->
<aop:around method="around" pointcut-ref="pointcut"></aop:around>
</aop:aspect>
</aop:config>
- 執行AopError
package com.offcn.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import java.util.Arrays;
public class AopError {
/*JoinPoint 這個類所有動態代理類的資訊*/
public void affterError(JoinPoint joinPoint, RuntimeException e){
/*joinPoint.getSignature() 這個方法是拿到你的動態代理的簽名檔案*/
System.out.println(joinPoint.getSignature().getName()+"這個方法異常資訊"+e.getMessage());
}
public void after(JoinPoint joinPoint){
System.out.println(joinPoint.getSignature().getName());
}
/*環繞增強 傳遞的是其JoinPoint子類*/
public void around(ProceedingJoinPoint proceedingJoinPoint){
System.out.println(proceedingJoinPoint.getTarget()+ Arrays.toString(proceedingJoinPoint.getArgs()));
/*呼叫此方法執行目標物件相應的方法*//*這個方法又相當於一個分割線,也就是前置與後置的分割線*/
try {
proceedingJoinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println(proceedingJoinPoint.getSignature().getName());
}
}
- 執行Test
UserService userServiceDao = (UserService) app.getBean("userService");
userServiceDao.add();
二、JDBC
(一)概述
Spring JDBC 框架負責所有的低層細節,從開始開啟連線,準備和執行 SQL 語句,處理異常,處理事務,到最後關閉連線。
所以當從資料庫中獲取資料時,你所做的是定義連線引數,指定要執行的 SQL 語句,每次迭代完成所需的工作。
(二)步驟詳解
- 所建包
- 導包(jar包)
- 獲取資料來源
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!--拿到資料庫的資訊-->
<property name="url" value="jdbc:mysql://localhost:3306/account?useUnicode=true&characterEncoding=utf-8"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
- 建立實體類pojo或者entity
package com.offcn.pojo;
import java.io.Serializable;
public class Account implements Serializable {
private int aid;
private double abalance;
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public double getAbalance() {
return abalance;
}
public void setAbalance(double abalance) {
this.abalance = abalance;
}
}
- 建立dao、service類及其實現類impl
package com.offcn.dao;
import com.offcn.pojo.Account;
import java.util.List;
public interface AccountDao {
//根據ID查詢
public Account selectById(int aid);
//查詢所有
public List<Account> selectAll();
//根據ID修改,影響的行數
public int update(Account account);
//新增
public int insert(Account account);
//根據ID刪除
int deleteById(int aid);
}
package com.offcn.dao.impl;
import com.offcn.dao.AccountDao;
import com.offcn.pojo.Account;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class AccountDaoImpl implements AccountDao {
//spring提供操作資料庫的類
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public Account selectById(int aid) {
String sql = "select * from account where aid = ?";
Object[] objects = {aid};
/*返回發每一行資料都在這個型別,第一個引數為:返回的結果集,第二個引數為:當前索引*/
RowMapper<Account> rowMapper = new RowMapper<Account>() {
@Override
public Account mapRow(ResultSet resultSet, int i) throws SQLException {
System.out.println(i+"dhhddhdhd");
Account account = new Account();
account.setAid(resultSet.getInt("aid"));
account.setAbalance(resultSet.getDouble("abalance"));
return account;
}
};
Account account1 = jdbcTemplate.queryForObject(sql,objects,rowMapper);
return account1;
}
@Override
public List<Account> selectAll() {
String sql = "select * from account";
RowMapper<Account> accountrowMapper = new RowMapper<Account>() {
@Override
public Account mapRow(ResultSet resultSet, int i) throws SQLException {
Account account = new Account();
account.setAid(resultSet.getInt("aid"));
account.setAbalance(resultSet.getDouble("abalance"));
return account;
}
};
List<Account> list = jdbcTemplate.query(sql,accountrowMapper);
return list;
}
@Override
public int update(Account account) {
String sql = "update account set abalance = ? where aid = ?";
Object[] objects ={account.getAbalance(),account.getAid()};
int num = jdbcTemplate.update(sql,objects);
return num;
}
@Override
public int insert(Account account) {
String sql = "insert into account(abalance) values(?)";
Object[] objects = {account.getAbalance()};
int num = jdbcTemplate.update(sql,objects);
return num;
}
@Override
public int deleteById(int aid) {
String sql = "delete from account where aid = ?";
Object[] objects = {aid};
int num = jdbcTemplate.update(sql,objects);
return num;
}
}
package com.offcn.service;
import com.offcn.pojo.Account;
import java.util.List;
public interface AccountService {
//根據ID查詢
public Account selectById(int aid);
//查詢所有
public List<Account> selectAll();
//根據ID修改,影響的常熟
public int update(Account account);
//新增
public int insert(Account account);
//根據ID刪除
int deleteById(int aid);
}
package com.offcn.service.impl;
import com.offcn.dao.AccountDao;
import com.offcn.pojo.Account;
import com.offcn.service.AccountService;
import java.util.List;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public Account selectById(int aid) {
return accountDao.selectById(aid);
}
@Override
public List<Account> selectAll() {
return accountDao.selectAll();
}
@Override
public int update(Account account) {
return accountDao.update(account);
}
@Override
public int insert(Account account) {
return accountDao.insert(account);
}
@Override
public int deleteById(int aid) {
return accountDao.deleteById(aid);
}
}
- 配置xml檔案
<!--spring 給我們提供操作資料庫的類-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!--設定dao層-->
<bean id="accountDao" class="com.offcn.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--設定service層-->
<bean id="accountService" class="com.offcn.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
- 建立測試類text
package com.offcn.text;
import com.offcn.pojo.Account;
import com.offcn.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Text {
@Test
public void text(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) app.getBean("accountService");
Account account = accountService.selectById(1);
System.out.println(account.getAid()+"\t"+account.getAbalance());
}
@Test
public void text1(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) app.getBean("accountService");
List<Account> list = accountService.selectAll();
for (Account a:list) {
System.out.println(a.getAid()+"\t"+a.getAbalance());
}
}
@Test
public void text2(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) app.getBean("accountService");
Account account = new Account();
account.setAid(1);
account.setAbalance(10000);
int num = accountService.update(account);
System.out.println(num);
}
@Test
public void text3(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) app.getBean("accountService");
Account account = new Account();
account.setAbalance(50000);
int num = accountService.insert(account);
System.out.println(num);
}
@Test
public void text4(){
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) app.getBean("accountService");
int num = accountService.deleteById(6);
System.out.println(num);
}
}