1. 程式人生 > >【JavaEE】SSH全註解

【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";
    }
    ...
}