A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

1:测试Spring环境
    1.1:加载相应的jar包和初始配置文件
        建立User Library:Spring3-Core  -->>  asm-3.2.0.RELEASE.jar
                                              com.springsource.org.apache.commons.logging-1.1.1.jar
                                              spring-beans-3.2.0.RELEASE.jar
                                              spring-context-3.2.0.RELEASE.jar
                                              spring-core-3.2.0.RELEASE.jar
                                              spring-expression-3.2.0.RELEASE.jar
                                              spring-test-3.2.0.RELEASE.jar
        加载初始配置文件:applicationContext.xml

    1.2:采用测试用例:
        private ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
                @Test
                public void test01(){
                    System.out.println(context.getBean("date"));
                }

2:测试Hibernate环境
    2.1:通过MyEclipse加载相应的jar包:Hibernate3.6  -->>  
    2.2:自动生成HibernateSessionFactory,并加载配置文件
    2.3:创建表、通过hibernate逆向工程生成POJO和ORM映射文件,删除lib中的数据库驱动包【删除之前要先remove包】,自己添加。(mysql-connector-java-5.0.4)
                【如果使用逆向工程则Hibernate必须要通过MyEclipse加载】
        drop database if exists gogoshop;
        create database gogoshop default character set utf8;
        use gogoshop;
        drop table if exists category;
        create table category(
        id int not null auto_increment,        /*类别编号,自动增长*/
        type varchar(20),                /*类别名称*/
        hot bool default false,                /*类别是否为热点类别,热点类别才有可能显示在首页*/
        primary key(id)                        /*设置类别编号为主键*/
        );
    2.4:创建Service包(业务逻辑与数据访问层没有分开,可以减少前期代码量)
         定义一个CategoryServiceImpl实现类与其接口
    2.5:进行环境测试,如果能插入数据则OK
        在实现类中注入SessionFactory,并提供set()方法,进行如下操作:
            2.5.1:定义方法  2.5.2:获取session  2.5.3:开启事务  2.5.4:业务处理  2.5.5:提交事务  2.5.6:关闭session
                private SessionFactory sessionFactory;
                public void setSessionFactory(SessionFactory sessionFactory){
                    this.sessionFactory = sessionFactory;
                }
                public void save(Category category){
                    Session session = HibernateSessionFactory.getSession();
                    session.getTransaction().begin();
                    session.save(category);
                    session.getTransaction().commit();
                    HibernateSessionFactory.closeSession();
                }
    2.6:注意要配置:<property name="javax.persistence.validation.mode">none</property>
                     <property name="show_sql">true</property>
3:测试Hibernate+Spring整合的环境
    3.1:加载整合环境的jar包:Spring3-Persistence-Core  -->>
                      事务包:Spring3-AOP  -->>
    3.2:引入c3p0的jar包,让Spring类管理连接数据库的信息:c3p0  -->>  c3p0.jar
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"/>
            <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/gogoshop"/>
            <property name="user" value="root"/>
            <property name="password" value="root"/>
        </bean>
    3.3:通过【LocalSessionFactoryBean】加载Hibernate的配置文件,取代HibernateFactory
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        </bean>
    3.4:配置事务管理器,关联sessionFactory
                【之所以要关联sessionFactory是因为所有的session都是从sessionFactory中创建的】
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    3.5:配置tx(事务模板)   ---->>        配置了哪些方法需要切入什么类型的事务
        <tx:advice id="advice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="save*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="delete*" propagation="REQUIRED"/>
                <tx:method name="*" propagetion="NEVER" read-only="true"/>
            </tx:attributes>
        </tx:advice>
    3.6:配置切入表达式   ---->>    配置哪些包的类需要切入事务
        <aop:config>
            <aop:pointcut expression="execution(* cn.chuang.shop.service.impl.*.*(..))" id="pointcut" />
            <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
        </aop:config>
    3.7:配置CategoryService,通过Spring来实现声明事务
        <bean id="categoryService" class="cn.chuang.shop.service.impl.CategoryServiceImpl">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    3.8:进行SSHTest测试
        private ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        ...

4:测试Struts的环境
        1.1:加载相应的jar包和配置
        1.2:创建Action继承ActionSupport
        1.3:在struts.xml中配置Action的逻辑名和相应的Action信息
        1.4:在web.xml中配置前端过滤器,过滤*.action的请求
               
5:测试Struts和Spring整合的环境(让Spring来管理Action,只要Action是从Spring创建的,则相应的依赖可以注入,实现了层之间的解耦)
        5.1:添加Spring-WEB包,里面必须要有:struts2-spring-plugin-2.1.6.jar
                 加载包之后要刷新项目,则Tomcat的lib才会同步
        5.2:把Action交给Spring管理,Action的class属性与Spring中的Action的ID属性要相对应
        5.3:在web.xml中配置监听器,启动的时候加载Spring的配置文件(加载完毕后存储到了Application内置对象中)
                            


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马