• Sping学习笔记_整合hibernate


    1,Spring 指定datasource

      a)     参考文档,找dbcp.BasicDataSource(3种方式)

           c3p0        dbcp ( 数据库连接池)     proxool

      b)     在DAO或者Service中注入dataSource

    @Component("u") 
    public class UserDAOImpl implements UserDAO {
        private DataSource dataSource;
        
        public DataSource getDataSource() {
            return dataSource;
        }
        @Resource
        public void setDataSource(DataSource dataSource) {
            this.dataSource = dataSource;
        }
        
        public void save(User user) {
            try {
                Connection connection=dataSource.getConnection();
                connection.createStatement().execute("insert into user values(null,'zhangsan')");
                connection.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("user saved");
        }
    
    }

      c)     在Spring中可以使用PropertyPlaceHolderConfigure来读取Properties文件的内容

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
        <context:annotation-config />
        <context:component-scan base-package="com.bjsxt" />
    
    
    
        <!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/spring" />
            <property name="username" value="root" />
            <property name="password" value="123456" />
        </bean> -->
    
        <bean
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <value>classpath:jdbc.properties</value>
            </property>
        </bean>
    
        <bean id="dataSource" destroy-method="close"
            class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${jdbc.driverClassName}" />  <!-- $占位符-->
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
    
    </beans>

    2,Spring整合Hibernate

      a)     <bean .. AnnotationSessionFactoryBean>

                    i.          <property dataSource

                   ii.          <annotatedClasses

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
        <context:annotation-config />
        <context:component-scan base-package="com.bjsxt" />
    
        <bean
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <value>classpath:jdbc.properties</value>
            </property>
        </bean>
    
        <bean id="dataSource" destroy-method="close"
            class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName"
                value="${jdbc.driverClassName}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
    
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="annotatedClasses">
                <list>
                    <value>com.bjsxt.model.User</value><!--那些类进行了注解-->
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">
                        org.hibernate.dialect.MySQLDialect
                    </prop>
                    <prop key="hibernate.show_sql">true</prop>
                </props>
            </property>
        </bean>
    
    </beans>

      b)     引入hibernate 系列jar包

      c)     User上加Annotation

    @Entity
    public class User {
        private int id;
        private String name;
        
        @Id
        @GeneratedValue
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
    }

      d)     UserDAO或者UserServie 注入SessionFactory

    @Component("u") 
    public class UserDAOImpl implements UserDAO {
    
        private SessionFactory sessionFactory;
    
        public SessionFactory getSessionFactory() {
            return sessionFactory;
        }
        
        @Resource
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
    
        public void save(User user) {
            
            System.out.println("session factory class:" + sessionFactory.getClass());
                Session s = sessionFactory.openSession();
                s.beginTransaction();
                s.save(user);
                s.getTransaction().commit();
            System.out.println("user saved!");
            
        }
    
    }

      e)     jar包问题一个一个解决

    3,声明式的事务管理

      a)     事务加在DAO层还是Service层?(service)

      b)     annotation

                    i.          加入annotation.xsd

                   ii.          加入txManager bean

                 iii.          <tx:annotation-driven

    <bean
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <value>classpath:jdbc.properties</value>
            </property>
        </bean>
    
        <bean id="dataSource" destroy-method="close"
            class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName"
                value="${jdbc.driverClassName}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
    
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="annotatedClasses">
                <list>
                    <value>com.bjsxt.model.User</value>
                    <value>com.bjsxt.model.Log</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">
                        org.hibernate.dialect.MySQLDialect
                    </prop>
                    <prop key="hibernate.show_sql">true</prop>
                </props>
            </property>
        </bean>
    
        <bean id="txManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
        
        <tx:annotation-driven transaction-manager="txManager"/>
        
    
    
    
    
    </beans>

                 iv.          在需要事务的方法上加:@Transactional

    @Component("userService")
    public class UserService {
        
        private UserDAO userDAO;  
        private LogDAO logDAO;
        public void init() {
            System.out.println("init");
        }
        @Transactional
        public void add(User user) {
            userDAO.save(user);
            Log log=new Log();
            log.setMsg("a user add");
            logDAO.save(log);
        }
        public UserDAO getUserDAO() {
            return userDAO;
        }
        
        public LogDAO getLogDAO() {
            return logDAO;
        }
        @Resource
        public void setLogDAO(LogDAO logDAO) {
            this.logDAO = logDAO;
        }
    
        @Resource(name="u")
        public void setUserDAO( UserDAO userDAO) {
            this.userDAO = userDAO;
        }
        
    
        
        public void destroy() {
            System.out.println("destroy");
        }
    }

                   v.          需要注意,Hibernate获得session时要使用SessionFactory.getCurrentSession 不能使用OpenSession

    @Component("u")
    public class UserDAOImpl implements UserDAO {
        private SessionFactory sessionFactory;
    
        public SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    
        @Resource
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
    
        public void save(User user) {
            
            Session s = sessionFactory.getCurrentSession();
            s.beginTransaction();
            s.save(user);
            
        }
    
    }

      c)     @Transactional详解

                   i.    什么时候rollback

      • 运行期异常,非运行期异常不会触发rollback
      • 必须uncheck (没有catch)
      • 不管什么异常,只要你catch了,spring就会放弃管理
      • 事务传播特性:propagation_required()如果当前环境中有事物则用原来的,否则自己创建一个。

            例如: @Transactional(propagation=Propagation.REQUIRED)等同于(@Transactional)

            作用,一个方法声明了@Transactional事务后,其内再调用的方法不需要再声明@Transactional.

           ii.read_only

    例如: @Transactional(propagation=Propagation.REQUIRED,readOnly=true)

    当方法声明readOnly=true时,该方法及其调用的方法内都不执行insert update等

      d)     xml(推荐,可以同时配置好多方法)

                    i.          <bean txmanager

                   ii.          <aop:config

              <aop:pointcut

              <aop:advisor pointcut-ref advice-ref

                 iii.          <tx:advice: id transaction-manager =

    <bean id="txManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <!-- the transactional semantics... -->
            <tx:attributes>
                <!-- all methods starting with 'get' are read-only -->
                <tx:method name="get*" read-only="true" />
                <!-- other methods use the default transaction settings (see below) -->
                <tx:method name="add*" />
            </tx:attributes>
        </tx:advice>
    
        <aop:config>
            <aop:pointcut id="businessService"
                expression="execution(public * com.bjsxt.service..*.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" />
        </aop:config>

                 iv.          <property name="packagesToScan">  可定义扫描目标包下所有实体类

    <property name="packagesToScan">            
          <value>com.bjsxt.model</value> 
     </property>

      d)     HibernateTemplate、HibernateCallback、HibernateDaoSupport(不重要)介绍

                    i.          设计模式:Template Method(模板方法)

                    ii.          Callback:回调/钩子函数

                   iii.          第一种:(建议)

            1,在spring中初始化HibernateTemplate,注入sessionFactory

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>

            2, DAO里注入HibernateTemplate

    @Component("u") 
    public class UserDAOImpl implements UserDAO {
    
        private HibernateTemplate hibernateTemplate;
    
        
    
        public HibernateTemplate getHibernateTemplate() {
            return hibernateTemplate;
        }
    
    
        @Resource
        public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
            this.hibernateTemplate = hibernateTemplate;
        }
    
    
    
        public void save(User user) {
            
                hibernateTemplate.save(user);
                
            //throw new RuntimeException("exeption!");
        }
    
    }

            3,save写getHibernateTemplate.save();

                    iv.          第二种:

    • 从HibernateDaoSupport继承(此方法不好用 可忽略)
    • 必须写在xml文件中,无法使用Annotation,因为set方法在父类中,而且是final的

    例如:

    首先,新建SuperDAOImpl类(使用Annotation注入--@Component):

    @Component
    public class SuperDAOImpl {
        private HibernateTemplate hibernateTemplate; //此处定义由spring注入管理
        public HibernateTemplate getHibernateTemplate() {
            return hibernateTemplate;
        }
        @Resource
        public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
            this.hibernateTemplate = hibernateTemplate;
        }
    }

    此时,xml中必须要有:

    <bean id="hibernateTemplate" 
    class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    或者,SuperDAOImpl类写成下面代码:

    @Component
    public class SuperDAOImpl extends HibernateDaoSupport {
        @Resource(name="sessionFactory")
        public void setSuperHibernateTemplate(SessionFactory sessionFactory) {
            super.setSessionFactory(sessionFactory);
        }
    }

    对应的xml中则可省略

    <bean id="hibernateTemplate" 
    class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    只要包含

    <bean id="sessionFactory"……..部分即可

    最后,其他类继承SuperDaoImpl类后便可直接使用HibernateTemplate

    @Component("u")
    
    public class UserDAOImpl extends SuperDAOImpl implements UserDAO {
    
          public void save(TestUser testUser) {
    
               this.getHibernateTemplate().save(testUser);
    
          }
    
    }
  • 相关阅读:
    k-近邻算法
    配置nginx 高并发 php
    ubuntu上安装mysql 编译安装
    Webpack探索【1】--- 基础知识
    【JavaScript专题】--- 立即执行函数表达式
    Eslint使用指南
    Maven学习笔记【2】 -- 安装和配置
    Maven学习笔记【1】 -- Maven简介
    Git中如何将代码恢复到之前某个节点
    密码硬编码(Password Management: Hardcoded Password)
  • 原文地址:https://www.cnblogs.com/enjoy-life-clh/p/4251553.html
Copyright © 2020-2023  润新知