温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Service层讲解DAO层中异常处理操作的示例分析

发布时间:2021-06-07 14:41:17 来源:亿速云 阅读:154 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关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三层架构:

Service层讲解DAO层中异常处理操作的示例分析

Service层讲解DAO层中异常处理操作的示例分析

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

用AOP捕捉 Service中调用Dao的异常

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

关于“Service层讲解DAO层中异常处理操作的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI