zoukankan      html  css  js  c++  java
  • ssh_maven之service开发

      我们已经完成了dao的开发,我们现在开始开发我们的service层,首先,我们的maven项目我们service要依赖我们的到层的依赖,我们可以在我们ssh_service的pom.xml中引入我们dao的一个依赖,下面是ssh_service/pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>com.itheima</groupId>
        <artifactId>ssh_parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
      <artifactId>ssh_service</artifactId>
      <dependencies>
          <dependency>
              <groupId>com.itheima</groupId>
              <artifactId>ssh_dao</artifactId>
              <version>0.0.1-SNAPSHOT</version>
          </dependency>
      </dependencies>
    </project>

      首先我们先看我们的目录结构

      

      我们的CustomerService

    package com.itheima.service;
    
    import java.util.List;
    
    import com.itheima.entity.Customer;
    
    public interface CustomerService {
        /**
         * 添加一个客户
         */
        public void saveCustomer(Customer customer);
        /**
         * 插叙所有的客户信息
         */
        public List<Customer> findAllCustomer();
    }

      我们的CustomerServiceImpl

    package com.itheima.service.impl;
    
    import java.util.List;
    
    import com.itheima.dao.CustomerDao;
    import com.itheima.entity.Customer;
    import com.itheima.service.CustomerService;
    
    public class CustomerServiceImpl implements CustomerService {
        
        private CustomerDao customerDao;
        
        public void saveCustomer(Customer customer) {
            customerDao.saveCustomer(customer);
        }
    
        public List<Customer> findAllCustomer() {
            return customerDao.findAllCustomer();
        }
    
        public void setCustomerDao(CustomerDao customerDao) {
            this.customerDao = customerDao;
        }
    }

      我们的applicationContext-service.xml

      注意:我们需要引入我们的applicationContext-dao.xml文件,因为我们的customerDao对象是在改文件中进行配置的

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
         <import resource="classpath:applicationContext-dao.xml"/>
        <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
            <property name="customerDao" ref="customerDao"></property>
        </bean>
    </beans>

      我们可以写个测试类进行测试

    package com.itheima.test;
    
    import java.util.List;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.itheima.entity.Customer;
    import com.itheima.service.CustomerService;
    
    public class CustomerServiceTest {
        @Test
        public void findAllCustomerTest(){
            ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext-service.xml");
            CustomerService customerService = (CustomerService) ac.getBean("customerService");
            List<Customer> customers = customerService.findAllCustomer();
            for (Customer customer : customers) {
                System.out.println(customer);
            }
        }
        
        @Test
        public void saveCustomerTest(){
            ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext-service.xml");
            CustomerService customerService = (CustomerService) ac.getBean("customerService");
            Customer customer = new Customer();
            customer.setCustName("黄忠");
            customerService.saveCustomer(customer);
        }
    }

      这里,我们测试的话,第一个方法findAllCustomer没有问题,但是第二个会出现问题

      

      下面我们看看这个错误

    javax.validation.ValidationException: HV000183: Unable to load 'javax.el.ExpressionFactory'. Check that you have the EL dependencies on the classpath, or use ParameterMessageInterpolator instead
        at org.hibernate.validator.internal.engine.ValidatorFactoryImpl.createValidator(ValidatorFactoryImpl.java:339)
        at org.hibernate.validator.internal.engine.ValidatorContextImpl.getValidator(ValidatorContextImpl.java:122)
        at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:111)
        at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:78)
        at org.hibernate.action.internal.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:197)
        at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:75)
        at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:597)
        at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:232)
        at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:213)
        at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:256)
        at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:317)
        at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:272)
        at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178)
        at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:109)
        at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
        at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
        at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
        at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
        at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
        at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679)
        at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
        at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
        at org.springframework.orm.hibernate5.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:620)
        at org.springframework.orm.hibernate5.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:616)
        at org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:341)
        at org.springframework.orm.hibernate5.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:309)
        at org.springframework.orm.hibernate5.HibernateTemplate.save(HibernateTemplate.java:616)
        at com.itheima.dao.impl.CustomerDaoImpl.saveCustomer(CustomerDaoImpl.java:13)
        at com.itheima.service.impl.CustomerServiceImpl.saveCustomer(CustomerServiceImpl.java:14)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
        at com.sun.proxy.$Proxy29.saveCustomer(Unknown Source)
        at com.itheima.test.CustomerServiceTest.saveCustomerTest(CustomerServiceTest.java:29)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

      异常解决:

      在父工程的pom.xml中导入,就行了

    <dependency>  
        <groupId>javax.el</groupId>  
        <artifactId>javax.el-api</artifactId>  
        <version>2.2.4</version>  
    </dependency>  
    <dependency>  
        <groupId>org.glassfish.web</groupId>  
        <artifactId>javax.el</artifactId>  
        <version>2.2.4</version>  
    </dependency>

      这样的话,我们的service模块也开发好了,只剩下我们的controller层了!

      

      

  • 相关阅读:
    将博客搬至CSDN
    第一章 spring起步
    动态规划-最长非降子序列
    硬币问题-动态规划详解
    哲学家就餐
    java并发编程(十九)障碍器CyclicBarrier
    java并发编程(十八)阻塞队列和阻塞栈
    java并发编程(十七)Executor框架和线程池
    java并发编程(十七)内存操作总结
    java并发编程(十六)happen-before规则
  • 原文地址:https://www.cnblogs.com/wh-share/p/ssh_maven_service.html
Copyright © 2011-2022 走看看