結(jié)合Service層講解DAO層的異常處理操作
domain:只是定義一個(gè)javabean。
dao:對于數(shù)據(jù)庫的操作,都放到dao層,也就是dao里面通常是對數(shù)據(jù)庫的增、刪、改、查等操作。
service:完成相應(yīng)的業(yè)務(wù)邏輯處理,調(diào)用dao層。
(web)servlet:完成界面請求、對界面進(jìn)行跳轉(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三層架構(gòu):


令DaoException繼承RuntimeException, 處理異常的時(shí)候可以將其拋給Service層(UserService.java),如果要處理那么就try,catch,否則就令其報(bào)錯(cuò)
用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");
}
}
目標(biāo)類和目標(biāo)方法
public interface PersonService {
void savePerson();
void updatePerson();
}
public class PersonServiceImpl implements PersonService {
private PersonDao personDao;//在這里選擇set方式在spring的配置文件中進(jìn)行注入
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public void savePerson() {
personDao.savePerson();
}
public void updatePerson() {
personDao.updatePerson();
}
}
切面(定義一個(gè)異常類和異常方法)
public class Exception {
/**
* 這是一個(gè)異常通知
*/
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>
<!-- 切入點(diǎn)表達(dá)式,確定目標(biāo)類 -->
<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
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis結(jié)果映射(ResultMap)的使用
在MyBatis中,結(jié)果映射是實(shí)現(xiàn)數(shù)據(jù)庫結(jié)果集到Java對象映射的核心,它不僅支持簡單的字段映射,還能處理字段名不一致、嵌套對象和集合映射等復(fù)雜場景,通過ResultMap,開發(fā)者可以靈活定義映射關(guān)系,以適應(yīng)各種需求,感興趣的可以了解一下2024-09-09
spring webflux自定義netty 參數(shù)解析
這篇文章主要介紹了spring webflux自定義netty 參數(shù)解析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
springboot 配置文件配置項(xiàng)前綴為0的數(shù)字特殊處理方式
這篇文章主要介紹了springboot 配置文件配置項(xiàng)前綴為0的數(shù)字特殊處理方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Spring?Retry實(shí)現(xiàn)重試機(jī)制的示例詳解
這篇文章主要為大家詳細(xì)介紹了Spring-Retry的用法以及實(shí)現(xiàn)原理是怎么樣的,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的可以了解一下2023-07-07
Spring Boot參數(shù)校驗(yàn)及分組校驗(yàn)的使用教程
在日常的開發(fā)中,參數(shù)校驗(yàn)是非常重要的一個(gè)環(huán)節(jié),嚴(yán)格參數(shù)校驗(yàn)會(huì)減少很多出bug的概率,增加接口的安全性,下面這篇文章主要給大家介紹了關(guān)于Spring Boot參數(shù)校驗(yàn)及分組校驗(yàn)使用的相關(guān)資料,需要的朋友可以參考下2021-08-08

