讲解DAO层的异常处理 结合Service层讲解DAO层的异常处理操作

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

讲解DAO层的异常处理 结合Service层讲解DAO层的异常处理操作

南下寻   2021-06-07 我要评论
想了解结合Service层讲解DAO层的异常处理操作的相关内容吗,南下寻在本文为您仔细讲解讲解DAO层的异常处理的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Service层,DAO层,异常处理,下面大家一起来学习吧。

domain:只是定义一个javabean。

dao:对于数据库的操作,都放到dao层,也就是dao里面通常是对数据库的增、删、改、查等操作。

service:完成相应的业务逻辑处理,调用dao层。

(web)servlet:完成界面请求、对界面进行跳转等等。servlet调用service层。

例子:

在domain包中,新建Xxx.java;在dao包中,新建IXxxDAO.java;在impl包中,新建XxxDAOImpl类;在test包中,新建XxxDAOTest.java,在XxxDAOImpl.java中编写具体方法,核心步骤为“贾琏欲执事”。

注:

①IXxxDAO.java为接口,在其中编写需要使用的方法,主要是增(save)删(delete)改(update)查(get&list–查询全部)。 ②Xxx.java中的变量均为私有,并且与数据库中的列名,属性相同。

J2EE三层架构:

令DaoException继承RuntimeException, 处理异常的时候可以将其抛给Service层(UserService.java),如果要处理那么就try,catch,否则就令其报错

用AOP捕捉 Service中调用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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们