ssm注解式开发

3 篇文章 0 订阅
  • 将spring-mvc改为注解
    1. 修改spring-mvc.xml,将处理器的注册方式由原来的<bean/>注册方式改为组件扫描器
      <beans xmlns="http://www.springframework.org/schema/beans"
      	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
      	xmlns:context="http://www.springframework.org/schema/context"
      	xmlns:mvc="http://www.springframework.org/schema/mvc"
      	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
      
      	<!--
      	<bean id="/test/register.do" class="com.enpong.web.StudentController">
      		<property name="studentService" ref="studentService"></property>
      	</bean>
      	-->
      	
      	<context:component-scan base-package="com.enpong.web"></context:component-scan>
      	
      </beans>

       

    2. 修改处理器类,修改配置文件后,无法再通过配置文件完成service的注入,需要在处理器中以注解方式完成Service的注入
      package com.enpong.web;
      
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Qualifier;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.servlet.ModelAndView;
      
      import com.enpong.beans.Student;
      import com.enpong.service.IStudentService;
      
      @Controller
      @RequestMapping("/test")
      public class StudentController{
      
      	@Autowired
      	@Qualifier("studentService")  //ByName方式注入
      	private IStudentService studentService;
      	
      	@RequestMapping("/regiter.do")
      	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
      		//接收请求参数
      		String name = request.getParameter("name");
      		String ageStr = request.getParameter("age");
      		Integer age = Integer.valueOf(ageStr);
      		Student student = new Student(name, age);
      		studentService.addStudent(student);
      		return new ModelAndView("/WEB-INF/jsp/welcome.jsp");
      	}
      
      }
      

       

  • 将spring改为注解
    1. 将service改为注解,完成Dao的注入
      • 修改spring-service.xml
        <beans xmlns="http://www.springframework.org/schema/beans"
        	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        	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-4.0.xsd
        	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
        
        	<!--
        	<bean id="studentService" class="com.enpong.service.impl.StudentServiceImpl">
        		<property name="studentDao" ref="IStudentDao"></property>
        	</bean>
        	-->
        	
        	<context:component-scan base-package="com.enpong.service.impl"></context:component-scan>
        
        </beans>

         

      • 修改Service实现类,对dao注入时可以用ByType方式或者ByName方式注入
        package com.enpong.service.impl;
        
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;
        import org.springframework.transaction.annotation.Transactional;
        
        import com.enpong.beans.Student;
        import com.enpong.dao.IStudentDao;
        import com.enpong.service.IStudentService;
        
        @Service
        public class StudentServiceImpl implements IStudentService {
        
        	@Autowired   //ByType方式注入
        	private IStudentDao studentDao;
        
        
        	@Override
        	@Transactional
        	public void addStudent(Student student) {
        		studentDao.insertStudent(student);
        	}
        
        }
        

         

    2. 将事务以注解方式织入到Service层
      • 修改spring-tx.xml
        <beans xmlns="http://www.springframework.org/schema/beans"
        	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        	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-4.0.xsd
        	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
        
        	<!-- 配置事务管理器 -->
        	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        		<property name="dataSource" ref="dataSource"></property>
        	</bean>
        	
        	<!-- 注册事务通知 -->
        	<tx:annotation-driven transaction-manager="transactionManager"/>
        
        </beans>

         

      • 在service实现类中加入@Transactional
  • 一般情况下,考虑性能问题,对于mybatis不建议使用注解式开发
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值