結(jié)合Service層講解DAO層的異常處理操作
domain
:只是定義一個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, 處理異常的時候可以將其拋給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"); } }
目標(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(); } }
切面(定義一個異常類和異常方法)
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> <!-- 切入點(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
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot JPA實(shí)現(xiàn)增刪改查、分頁、排序、事務(wù)操作等功能示例
本篇文章主要介紹了SpringBoot JPA實(shí)現(xiàn)增刪改查、分頁、排序、事務(wù)操作等功能示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03Spring?Cloud灰度部署實(shí)現(xiàn)過程詳解
這篇文章主要為大家介紹了Spring?Cloud灰度部署實(shí)現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Java編程實(shí)現(xiàn)游戲中的簡單碰撞檢測功能示例
這篇文章主要介紹了Java編程中的簡單碰撞檢測功能,涉及java針對坐標(biāo)點(diǎn)的相關(guān)數(shù)學(xué)運(yùn)算操作技巧,需要的朋友可以參考下2017-10-10gateway網(wǎng)關(guān)與前端請求跨域問題的解決方案
這篇文章主要介紹了gateway網(wǎng)關(guān)與前端請求跨域問題的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07