【JavaEE】SSH全注解

1.下载必要核心jar包,并引入到项目
2.需要注意的spring配置文件,由与需要使用全注解的方式 配置文件和之前有些许不同

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <!-- 让spring关注注解,并扫描com.lagersoft.*包下的所有类 -->
    <context:component-scan base-package="com.lagersoft.*"></context:component-scan>
    <context:annotation-config></context:annotation-config>

    <!-- 数据源配置项 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver">
        </property>
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl">
        </property>
        <property name="username" value="scott"></property>
        <property name="password" value="tiger"></property>
    </bean>

    <!-- SessionFactory配置项 这个bean相当于Hibernate的配置项 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="annotatedClasses">
            <list>  
                <value>com.lagersoft.po.Dept</value>
                <value>com.lagersoft.po.Emp</value>
            </list>  
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.Oracle9Dialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
    </bean>

    <!-- 使用声明式事务 -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="getAll*" read-only="true" propagation="SUPPORTS" />
            <tx:method name="find*" read-only="true" propagation="SUPPORTS" />
            <tx:method name="update*" read-only="false" propagation="REQUIRED" />
            <tx:method name="del*" read-only="false" propagation="REQUIRED" />
            <tx:method name="add*" read-only="false" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
    <!-- 切面的配置项 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice"
            pointcut="execution(* com.lagersoft.service.*.*(..))" />
    </aop:config>

</beans>

3.数据访问层的实现类 使用@org.springframework.stereotype.Repository注解,为了更方便的注入sessionFactory对象可以让所有DAO实现类继承DAO实现类的基类

package com.lagersoft.base;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
/**
 * DAO层实现类基类:实现sessionFactory的注入
 * @author xiaoping
 *
 */
public class BaseDAOImpl{
    private SessionFactory sessionFactory;
    private HibernateTemplate hibernateTemplate;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }
    @Autowired
    public void setHibernateTemplate(SessionFactory sessionFactory) {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

}

4.业务逻辑层的实现类使用 @org.springframework.stereotype.Service注解,
使用 @org.springframework.beans.factory.annotation.Autowired 来注入需要使用到的DAO

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired private EmpDAO empDAO;
    ...
}

5.xxAction类 使用 @org.springframework.stereotype.Controller注解
使用 @org.apache.struts2.convention.annotation.Action来注解具体的需要访问的action方法
使用@org.apache.struts2.convention.annotation.Result注解action返回的结果

@Controller
@Scope("prototype")
public class EmpAction extends ActionSupport{
    //spring注入的使用@Autowired
    @Autowired EmpService empService;
    @Autowired DeptService deptService;
    //这里访问/emp_list 这个Action的时候访问 list() 返回SUCCESS 则跳转到/emp_list.jsp页面
    @Action(value= "/emp_list",results={
        @Result(name="success",location="/emp_list.jsp")
    })
    public String list(){
        list = empService.getAll();
        return SUCCESS;
    }
    //这里跳转到/emp_list 这个Action
    @Action(value = "/emp_del",results={
        @Result(name = "queryAll",type ="redirectAction", location = "/emp_list")
    })
    public String del(){
        empService.del(emp);
        return "queryAll";
    }
    ...
}

6.DEMO例子下载

IT文库 » 【JavaEE】SSH全注解
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址