首先需要导入依赖jar包
<!--spring配置-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<!--aop使用的jar-->
<dependency>
<groupId> org.aspectj</groupId >
<artifactId> aspectjweaver</artifactId >
<version> 1.8.7</version >
</dependency>
<!--SpringWeb-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.8.RELEASE</version>
</dependency>
<!--JavaEE-->
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!--c3p0-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--hibernate jar包-->
<!--jta的jar包-->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.6.Final</version>
</dependency>
<!--Spring-ORM-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version> 4.2.2.RELEASE</version>
</dependency>
<!--Oracle驱动的jar-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.struts.xwork</groupId>
<artifactId>xwork-core</artifactId>
<version>2.3.4.1</version>
</dependency>
<!--Struts整合Spring的jar包-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.4.1</version>
</dependency>
<!--Struts2注解支持jar包-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.4.1</version>
</dependency>
resources层:
applicationContext.xml:
<!--
1.数据源 c3p0
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--
2.识别到jdbc.properties
-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--
3.形成SessionFactory
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<!--方言-->
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<!--是否打印sql-->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
<!--关联小配置-->
<property name="packagesToScan" value="entity"></property>
</bean>
<!--
7.事务管理器
-->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--
8.事务真实配置
-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
jdbc.properties:
jdbc.jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.username=y2166
jdbc.password=y2166
搭建架构分层:
action:
1 @Controller("deptAction") 2 @ParentPackage("struts-default") 3 @Namespace("/") 4 @Scope("prototype") //多例的 5 public class DeptAction implements Action { 6 private Dept dept; 7 @Resource(name = "deptService") // DI 依赖注入 8 IDeptService service; 9 @org.apache.struts2.convention.annotation.Action(value = "/add",results={@Result(name = "success",location="/jsp/index.jsp")}) 10 public String execute() throws Exception { 11 service.addDept(dept); 12 return SUCCESS; 13 } 14 15 public Dept getDept() { 16 return dept; 17 } 18 19 public void setDept(Dept dept) { 20 this.dept = dept; 21 } 22 23 }