s2sh框架搭建(基于spring aop)

对于spring aop 是如何管理事务的,请看一下:http://bbs.csdn.net/topics/290021423

1.applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"    
       xmlns:aop="http://www.springframework.org/schema/aop"   <!-- 新加入 -->
       xsi:schemaLocation="     
          http://www.springframework.org/schema/beans      
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
          http://www.springframework.org/schema/aop      
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd     
          http://www.springframework.org/schema/tx      
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   "> 
    <!--新加入 datasource -->
      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/hjzgg_test?characterEncoding=UTF-8" />
        <property name="username" value="root" />
        <property name="password" value="hjzgg5211314" />
      </bean>
    
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation"
            value="classpath:hibernate.cfg.xml">
        </property>
        <!--新加入 将dataSource注入到sessionFactory中 -->
        <property name="dataSource" ref="dataSource"></property>
        
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
                hibernate.hbm2ddl.auto=update
                hibernate.show_sql=true
                hibernate.format_sql=false
                hibernate.cache.use_second_level_cache=true
                hibernate.cache.use_query_cache=false
                hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
         <!--hibernate.current_session_context_class=thread 不需要了-->
</value> </property> <!--通过配置文件的方式获取数据源--> <property name="mappingResources"> <list> <!-- 以下用来列出所有的PO映射文件 --> <value>person.cfg.xml</value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="personDao" class="com.xunchang.PersonDaoImpl"> <!-- 采用依赖注入传入SessionFactory的引用 --> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="service" class="com.hjzgg.service.XunChageService"> <property name="personDao" ref="personDao"/> </bean> <bean id="login" class="com.xunchang.LoginAction"> <property name="service" ref="service"/> </bean> <!-- 配置那些类的方法进行事务管理,需要aopalliance-1.0.jar和aspectjweaver.jar,当前com.neusoft.leehom.service包中的子包, 类中所有方法需要,还需要参考tx:advice的设置 --> <!-- 需要引入tx的命名空间 --> <!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 指定哪些方法需要加入事务,这里懒惰一下全部加入,可以使用通配符来只加入需要的方法 --> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="get*" propagation="REQUIRED" read-only="true"/> <tx:method name="query*" propagation="REQUIRED" read-only="true"/> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 需要引入aop的命名空间 --> <aop:config> <!-- 切入点指明了在执行Service的所有方法时产生事务拦截操作 --> <aop:pointcut id="daoMethods" expression="execution(* com.hjzgg.service.*.*(..))" /> <!-- 定义了将采用何种拦截操作,这里引用到 txAdvice --> <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" /> </aop:config> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>

2. dao层代码

package com.xunchang;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class PersonDaoImpl implements PersonDao{

    private SessionFactory sessionFactory;  
    
    public Session getSession() {  
        return sessionFactory.getCurrentSession();  
    }  
  
    public SessionFactory getSessionFactory() {  
        return sessionFactory;  
    }  
  
    public void setSessionFactory(SessionFactory sessionFactory) {  
        this.sessionFactory = sessionFactory;  
    } 
    
    @Override
    public Person get(Integer id) {
        return (Person) this.getSession().get(Person.class, id);
    }

    @Override
    public void save(Person ps) {
        this.getSession().save(ps);
    }

    @Override
    public List<Person> findAllPerson() {
            
         /*
          *    问题: home402 is not mapped [from home402];
          *    最后发现了问题所在,hql查询时使用的from Xxx,Xxx不是实体类的名称,而是 EntityName (Hibernate注解)。
                如:
                
                @Entity
                @Table(name="QING_AO_CENTER_INFO")
                public class QingAoCenterInfo {
                         ......
                }
                此处,
                
                @Entity后并没有显示的指明EntityName,因此默认采用实体类的名称。
                
                @Entity(name="QING_AO_CENTER_INFO")
                @Table(name="QING_AO_CENTER_INFO")
                public class QingAoCenterInfo {
                             ......
                }
                可以发现,显示地指明了 EntityName,因此在使用hql查询的时候,要from   QING_AO_CENTER_INFO,而不是from  QingAoCenterInfo ;
                
                centerList = manager.find("from QING_AO_CENTER_INFO center where center.type = ? and center.centerName = ?", new Object[]{type,centerName});
         */
        Session session = this.getSession();
//      Transaction tran = session.beginTransaction();
        session.save(new Person(null, "我是hjzgg", 23));
        List<Person> list = session.createQuery("from Person").list();
//      tran.commit();
        return list;
    }

}

3.service层部分代码:

package com.hjzgg.service;

import java.util.List;

import com.xunchang.Person;
import com.xunchang.PersonDao;

public class XunChageService {
    private PersonDao personDao;

    
    public Person get(Integer id){
        return personDao.get(id);
    }
    
    public void save(Person ps){
        personDao.save(ps);
    }
    public List<Person> findAllPerson(){
        return personDao.findAllPerson();
    }
    public PersonDao getPersonDao() {
        return personDao;
    }
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
}

4.action部分代码:

package com.xunchang;

import java.util.List;

import com.hjzgg.service.XunChageService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
    private XunChageService service;
    
    @Override
    public String execute() throws Exception{
        List<Person> list_person =  service.findAllPerson();
        ActionContext.getContext().getSession().put("person", list_person);
        return "success";
    }

    public XunChageService getService() {
        return service;
    }

    public void setService(XunChageService service) {
        this.service = service;
    }
    
}

5.和数据库映射的pojo:

package com.xunchang;

import java.io.Serializable;
/*
 *待解决问题,为什么一定要实现 Serializeable
*/
public class Person implements Serializable{
    private Integer personId;
    private String person_name;
    private Integer person_age;
    
    public Person(){
    }

    public Person(Integer personId, String person_name, Integer person_age) {
        super();
        this.personId = personId;
        this.person_name = person_name;
        this.person_age = person_age;
    }

    public Integer getPersonId() {
        return personId;
    }
    public void setPersonId(Integer personId) {
        this.personId = personId;
    }
    public String getPerson_name() {
        return person_name;
    }
    public void setPerson_name(String person_name) {
        this.person_name = person_name;
    }
    public Integer getPerson_age() {
        return person_age;
    }
    public void setPerson_age(Integer person_age) {
        this.person_age = person_age;
    }
}

6.hibernate映射文件xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping 
                PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.xunchang">
        <class name="Person" table="home402">
            <!-- 定义持久化类的表示属性 -->
            <id name="personId" column="personId" type="java.lang.Integer">
                <!-- 定义主键生成策略 -->
                <generator class="identity"/>
            </id>
            <property name="person_name" column="person_name" type="java.lang.String"/>
            <property name="person_age" column="person_age" type="java.lang.Integer"/>
        </class>
</hibernate-mapping>

7.struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package namespace="/" name="struts2" extends="struts-default">  
    
        <!-- package中的标签必须按照如下顺序配置
        result-types,interceptors,default-interceptor-ref,default-action-ref,default-class-ref,global-results,global-exception-mappings,action*(就是所有的action放到最后)
        -->
        <!-- 自定义拦截器 ,如果有拦截器,必须放在package标签内的第一位-->
        <interceptors>
          <!-- 在这里可以添加自己的拦截器
          <interceptor name="myInterceptor" class="自定义pojo类"></interceptor>
          -->
          <interceptor-stack name="myInterceptorStack">
            <!--  
            <interceptor-ref name="myInterceptor"></interceptor-ref>
            -->
            <interceptor-ref name="defaultStack"></interceptor-ref>
          </interceptor-stack>
        </interceptors>  
         
        <global-results>
          <result></result>
        </global-results>
        
        <action name="login" class="login">
            <result name="success">/servlet/MyServlet</result>
        </action>
        
  </package>
</struts>    

 

posted @ 2015-03-27 19:43  hjzqyx  阅读(424)  评论(0编辑  收藏  举报