• spring注解 annotation


    @Resourse(name="  xxx") 意味从上下文找xxx名字一样的然后引入

    @Repository("personDao") 意味生成一个 bean 以便于让其他高业务层的去找这个  的bean

    spring.xml新加入

    xmlns:context="http://www.springframework.org/schema/context"

    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd

    spring.xml中

     1 <beans xmlns="http://www.springframework.org/schema/beans"
     2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     3     xmlns:aop="http://www.springframework.org/schema/aop"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:tx="http://www.springframework.org/schema/tx"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     7         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     8         http://www.springframework.org/schema/aop 
     9         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    10         http://www.springframework.org/schema/context
    11            http://www.springframework.org/schema/context/spring-context-2.5.xsd
    12         http://www.springframework.org/schema/tx 
    13         http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    14     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    15         <property name="configLocation">
    16             <value>classpath:hibernate/hibernate.cfg.xml</value>
    17         </property>
    18     </bean>
    19     
    20     <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    21         <property name="sessionFactory">
    22             <ref bean="sessionFactory"/>
    23         </property>
    24     </bean>
    25     
    26     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    27         <property name="sessionFactory">
    28             <ref bean="sessionFactory"/>
    29         </property>
    30     </bean>
    31     
    32     <context:component-scan base-package="cn.itcast.s2sh"></context:component-scan>
    33     
    34     <tx:annotation-driven transaction-manager="transactionManager"/>
    35     
    36 </beans>

    impl注解  (因为声明了注解,所以不能继承HibernateDaoSupport,利用组合的方式)

     1 package cn.itcast.s2sh.sh.dao.impl;
     2 
     3 import java.io.Serializable;
     4 
     5 import javax.annotation.Resource;
     6 
     7 import org.springframework.orm.hibernate3.HibernateTemplate;
     8 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
     9 import org.springframework.stereotype.Repository;
    10 
    11 import cn.itcast.s2sh.domain.sh.Person;
    12 import cn.itcast.s2sh.sh.dao.PersonDao;
    13 
    14 @Repository("personDao")                     //生成beanname
    15 public class PersonDaoImpl implements PersonDao{
    16     @Resource(name="hibernateTemplate")            //从spring.xml中引入
    17     private HibernateTemplate hibernateTemplate;   //set  get方法也就不用了,注解的形式注入了
    18     @Override
    19     public void savePerson(Person person) {
    20         // TODO Auto-generated method stub
    21         this.hibernateTemplate.save(person);
    22     }
    23 
    24     @Override
    25     public Person getPesonById(Serializable id) {
    26         // TODO Auto-generated method stub
    27         return  (Person) this.hibernateTemplate.load(Person.class, id);
    28     }
    29     
    30 }
    public class PersonDaoImpl extends HibernateSupport implements PersonDao
    {//上面代码一样 没有成员变量
    hibernateTemplate
    }
    因为
    HibernateTemple 是spring提供的类,它里面有 private HibernateTemplate hibernateTemplate;的成员变量,这个成员变量也需要 HibernateSupport提供注解,但是源代码里上面没有注解,但是又需要注入这个hibernateTemplate变量,因为继承关系继承父类的,所以 改写成组合方式,不用继承。

    serviceImpl

     1 package cn.itcast.s2sh.sh.service.impl;
     2 
     3 import java.io.Serializable;
     4 
     5 import javax.annotation.Resource;
     6 
     7 import org.springframework.stereotype.Service;
     8 import org.springframework.transaction.annotation.Transactional;
     9 
    10 import cn.itcast.s2sh.domain.sh.Person;
    11 import cn.itcast.s2sh.sh.dao.PersonDao;
    12 import cn.itcast.s2sh.sh.service.PersonService;
    13 
    14 @Service("personService")
    15 public class PersonServiceImpl implements PersonService{
    16     @Resource(name="personDao")
    17     private PersonDao personDao;
    18 
    19     @Override
    20     @Transactional(readOnly=false)              //事务
    21     public void savePerson(Person person) {
    22         // TODO Auto-generated method stub
    23         this.personDao.savePerson(person);
    24     }
    25 
    26     @Override
    27     public Person getPersonByID(Serializable id) {
    28         // TODO Auto-generated method stub
    29         Person person = this.personDao.getPesonById(id);
    30         return person;
    31     }
    32 }
  • 相关阅读:
    vue:路由实现原理
    webpack自定义loader和自定义插件
    JS常见的算法
    浅析DES、AES、RSA、MD5加密算法及其应用场景
    关于JSON.parse(JSON.stringify(obj))实现深拷贝应该注意的坑
    Canvas和SVG区别
    解决 webpack 打包文件体积过大
    箭头函数与普通函数的区别
    2.Linux基础命令
    1.Ubuntu系统与vmware虚拟机的安装与使用
  • 原文地址:https://www.cnblogs.com/friends-wf/p/3801643.html
Copyright © 2020-2023  润新知