結合Service層講解DAO層的異常處理操作
domain:只是定義一個javabean。
dao:對于數(shù)據(jù)庫的操作,都放到dao層,也就是dao里面通常是對數(shù)據(jù)庫的增、刪、改、查等操作。
service:完成相應的業(yè)務邏輯處理,調(diào)用dao層。
(web)servlet:完成界面請求、對界面進行跳轉(zhuǎn)等等。servlet調(diào)用service層。
例子:
在domain包中,新建Xxx.java;在dao包中,新建IXxxDAO.java;在impl包中,新建XxxDAOImpl類;在test包中,新建XxxDAOTest.java,在XxxDAOImpl.java中編寫具體方法,核心步驟為“賈璉欲執(zhí)事”。
注:
①IXxxDAO.java為接口,在其中編寫需要使用的方法,主要是增(save)刪(delete)改(update)查(get&list–查詢?nèi)?。 ②Xxx.java中的變量均為私有,并且與數(shù)據(jù)庫中的列名,屬性相同。
J2EE三層架構:


令DaoException繼承RuntimeException, 處理異常的時候可以將其拋給Service層(UserService.java),如果要處理那么就try,catch,否則就令其報錯
用AOP捕捉 Service中調(diào)用Dao的異常

PersonDao和PersonDaoImpl,并在PersonDaoImpl中制造異常
public interface PersonDao {
public void savePerson();
public void updatePerson();
}
public class PersonDaoImpl implements PersonDao {
public void savePerson() {
int a = 1/0;
}
public void updatePerson() {
Long.parseLong("aaa");
}
}
目標類和目標方法
public interface PersonService {
void savePerson();
void updatePerson();
}
public class PersonServiceImpl implements PersonService {
private PersonDao personDao;//在這里選擇set方式在spring的配置文件中進行注入
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public void savePerson() {
personDao.savePerson();
}
public void updatePerson() {
personDao.updatePerson();
}
}
切面(定義一個異常類和異常方法)
public class Exception {
/**
* 這是一個異常通知
*/
public void exceptionMethod(JoinPoint joinPoint,Throwable ex){
System.out.println(ex.getMessage());
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="personDao" class="com.mo.dao.PersonDaoImpl"></bean>
<bean id="personService" class="com.mo.service.PersonServiceImpl">
<!-- 用set方法注入 -->
<property name="personDao" ref="personDao"></property>
</bean>
<bean id="Exception" class="com.mo.exception.Exception"></bean>
<aop:config>
<!-- 切入點表達式,確定目標類 -->
<aop:pointcut
expression="execution(* com.mo.service.PersonServiceImpl.*(..))"
id="perform"/>
<!-- ref指向的對象就是切面 -->
<aop:aspect ref="Exception">
<aop:after-throwing method="exceptionMethod" pointcut-ref="perform" throwing="ex"/>
</aop:aspect>
</aop:config>
</beans>
單元測試類
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonService personService = (PersonService)context.getBean("personService");
personService.savePerson();
}
輸出
/ by zero
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
spring webflux自定義netty 參數(shù)解析
這篇文章主要介紹了spring webflux自定義netty 參數(shù)解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
springboot 配置文件配置項前綴為0的數(shù)字特殊處理方式
這篇文章主要介紹了springboot 配置文件配置項前綴為0的數(shù)字特殊處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
Spring Boot參數(shù)校驗及分組校驗的使用教程
在日常的開發(fā)中,參數(shù)校驗是非常重要的一個環(huán)節(jié),嚴格參數(shù)校驗會減少很多出bug的概率,增加接口的安全性,下面這篇文章主要給大家介紹了關于Spring Boot參數(shù)校驗及分組校驗使用的相關資料,需要的朋友可以參考下2021-08-08

