案例描述:使用SSH整合框架实现部门的添加功能
工程: Maven
数据库:Oracle
框架:Spring Struts2 Hibernate
案例架构:
1.依赖jar包 pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <parent> 4 <artifactId>01MyBatis</artifactId> 5 <groupId>cn.happy</groupId> 6 <version>1.0-SNAPSHOT</version> 7 </parent> 8 <modelVersion>4.0.0</modelVersion> 9 <artifactId>11SSHAnnotation</artifactId> 10 <packaging>war</packaging> 11 <name>11SSHAnnotation Maven Webapp</name> 12 <url>http://maven.apache.org</url> 13 <dependencies> 14 <!--单测--> 15 <dependency> 16 <groupId>junit</groupId> 17 <artifactId>junit</artifactId> 18 <version>4.3</version> 19 <scope>test</scope> 20 </dependency> 21 <!--spring配置--> 22 <dependency> 23 <groupId>org.springframework</groupId> 24 <artifactId>spring-context</artifactId> 25 <version>4.2.0.RELEASE</version> 26 </dependency> 27 <!--aop使用的jar--> 28 <dependency> 29 <groupId> org.aspectj</groupId > 30 <artifactId> aspectjweaver</artifactId > 31 <version> 1.8.7</version > 32 </dependency> 33 34 <!--SpringWeb--> 35 <dependency> 36 <groupId>org.springframework</groupId> 37 <artifactId>spring-web</artifactId> 38 <version>4.1.8.RELEASE</version> 39 </dependency> 40 41 <!--JavaEE--> 42 <dependency> 43 <groupId>javaee</groupId> 44 <artifactId>javaee-api</artifactId> 45 <version>5</version> 46 </dependency> 47 48 <dependency> 49 <groupId>javax.servlet</groupId> 50 <artifactId>jstl</artifactId> 51 <version>1.2</version> 52 <scope>runtime</scope> 53 </dependency> 54 55 <dependency> 56 <groupId>org.springframework</groupId> 57 <artifactId>spring-tx</artifactId> 58 <version>4.2.5.RELEASE</version> 59 </dependency> 60 61 <!--c3p0--> 62 <dependency> 63 <groupId>com.mchange</groupId> 64 <artifactId>c3p0</artifactId> 65 <version>0.9.5.2</version> 66 </dependency> 67 68 <!--hibernate jar包--> 69 <!--jta的jar包--> 70 <dependency> 71 <groupId>javax.transaction</groupId> 72 <artifactId>jta</artifactId> 73 <version>1.1</version> 74 </dependency> 75 76 <dependency> 77 <groupId>org.hibernate</groupId> 78 <artifactId>hibernate-core</artifactId> 79 <version>5.0.6.Final</version> 80 </dependency> 81 82 <!--Spring-ORM--> 83 <dependency> 84 <groupId>org.springframework</groupId> 85 <artifactId>spring-orm</artifactId> 86 <version> 4.2.2.RELEASE</version> 87 </dependency> 88 89 <!--Oracle驱动的jar--> 90 <dependency> 91 <groupId>com.oracle</groupId> 92 <artifactId>ojdbc6</artifactId> 93 <version>11.2.0.1.0</version> 94 </dependency> 95 96 <!--struts2--> 97 <dependency> 98 <groupId>org.apache.struts</groupId> 99 <artifactId>struts2-core</artifactId> 100 <version>2.3.4.1</version> 101 </dependency> 102 103 <dependency> 104 <groupId>org.apache.struts.xwork</groupId> 105 <artifactId>xwork-core</artifactId> 106 <version>2.3.4.1 </version> 107 </dependency> 108 109 <!-- struts2整合spring --> 110 <dependency> 111 <groupId>org.apache.struts</groupId> 112 <artifactId>struts2-spring-plugin</artifactId> 113 <version>2.3.4.1</version> 114 </dependency> 115 116 <!-- struts注解核心包 --> 117 <dependency> 118 <groupId>org.apache.struts</groupId> 119 <artifactId>struts2-convention-plugin</artifactId> 120 <version>2.3.4.1</version> 121 </dependency> 122 123 </dependencies> 124 <build> 125 <finalName>11SSHAnnotation</finalName> 126 </build> 127 </project>
2.web.xml
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5 <web-app> 6 <display-name>Archetype Created Web Application</display-name> 7 <context-param> 8 <param-name>contextConfigLocation</param-name> 9 <param-value>classpath:applicationContext.xml</param-value> 10 </context-param> 11 12 <filter> 13 <filter-name>struts</filter-name> 14 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 15 </filter> 16 <filter-mapping> 17 <filter-name>struts</filter-name> 18 <url-pattern>/*</url-pattern> 19 </filter-mapping> 20 21 <!--监听器--> 22 <listener> 23 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 24 </listener> 25 </web-app>
3.resource目录文件配置
3.1applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 8 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 9 "> 10 <!--包扫描器--> 11 <context:component-scan base-package="cn.happy"></context:component-scan> 12 <!--1.Datasource--> 13 <!--1.配置数据源c3p0--> 14 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 15 <property name="driverClass" value="${jdbc.driverClassName}"/> 16 <property name="user" value="${jdbc.username}"/> 17 <property name="password" value="${jdbc.password}"/> 18 <property name="jdbcUrl" value="${jdbc.url}"/> 19 </bean> 20 21 <!--jdbc.properties--> 22 <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> 23 24 <!--2.SessionFactory 类:Local--> 25 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 26 <property name="dataSource" ref="dataSource"></property> 27 <property name="hibernateProperties"> 28 <props> 29 <!--hibernate.xxxxxx必须以hibernate--> 30 <prop key="hibernate.show_sql">true</prop> 31 <prop key="hibernate.format_sql">true</prop> 32 <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 33 <!--with current thread bind session和线程绑定的session--> 34 <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop> 35 </props> 36 </property> 37 <!--扫描小配置文件 所有的hbm文件--> 38 <!--<property name="mappingDirectoryLocations" value="classpath:cn/happy/entity"></property>--> 39 40 <property name="packagesToScan" value="cn.happy.entity"></property> 41 </bean> 42 43 <!-- 5.事务管理器 --> 44 <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 45 <property name="sessionFactory" ref="sessionFactory"></property> 46 </bean> 47 48 <!--6.事务--> 49 <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> 50 <!-- <tx:advice id="txAdvice" transaction-manager="transactionManager"> 51 <tx:attributes> 52 <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/> 53 </tx:attributes> 54 </tx:advice> 55 56 <aop:config> 57 <!–配置了切点Pointcut–> 58 <aop:pointcut id="mypoint" expression="execution(* *..service.*.*(..))"/> 59 <!– 顾问–> 60 <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"></aop:advisor> 61 </aop:config>--> 62 </beans>
jdbc.properties:
1 jdbc.driverClassName=oracle.jdbc.OracleDriver 2 jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl 3 jdbc.username=liutao 4 jdbc.password=liutao
分层架构:
entity:
Dept
@Entity @Table(name = "Dept") public class Dept { @Id @GeneratedValue private Integer deptno; //部门编号 @Column private String dname; //部门名称 @Column private String loc; //部门位置 //set get方法省略 }
dao:
IDeptDAO:
1 public interface IDeptDAO { 2 /** 3 * @author: liutao 4 * @Description: 添加部门 5 * @param: 6 * @return: 7 * @throws: 8 * @date: 2018/2/26 16:23 9 */ 10 public void addDept(Dept dept); 11 }
DeptDAOImpl:
1 @Repository("deptDAO") 2 public class DeptDAOImpl implements IDeptDAO{ 3 //植入sessionFactory 4 @Resource(name = "sessionFactory") 5 private SessionFactory sessionFactory; 6 public void addDept(Dept dept) { 7 Session session = sessionFactory.getCurrentSession(); 8 session.save(dept); 9 } 10 11 public SessionFactory getSessionFactory() { 12 return sessionFactory; 13 } 14 15 public void setSessionFactory(SessionFactory sessionFactory) { 16 this.sessionFactory = sessionFactory; 17 } 18 }
service:
IDeptService:
1 public interface IDeptService { 2 /** 3 * @author: liutao 4 * @Description: 添加部门 5 * @param: 6 * @return: 7 * @throws: 8 * @date: 2018/2/26 16:23 9 */ 10 public void addDept(Dept dept); 11 }
DeptServiceImpl:
1 @Service("deptService") 2 public class DeptServiceImpl implements IDeptService { 3 @Resource(name = "deptDAO") 4 private IDeptDAO deptDAO; 5 @Transactional 6 public void addDept(Dept dept) { 7 deptDAO.addDept(dept); 8 } 9 10 public IDeptDAO getDeptDAO() { 11 return deptDAO; 12 } 13 14 public void setDeptDAO(IDeptDAO deptDAO) { 15 this.deptDAO = deptDAO; 16 } 17 }
action:
DeptAction:
1 @Controller("deptAction") 2 @ParentPackage("struts-default") 3 @Namespace("/") 4 @Scope("prototype") 5 public class DeptAction extends ActionSupport implements ModelDriven<Dept>{ 6 private Dept dept=new Dept(); 7 @Resource(name = "deptService") 8 private IDeptService deptService; 9 10 @Action(value = "add",results = {@Result(name = "success",location = "/jsp/success.jsp")}) 11 public String addDept(){ 12 deptService.addDept(dept); 13 return SUCCESS; 14 } 15 16 public Dept getDept() { 17 return dept; 18 } 19 20 public void setDept(Dept dept) { 21 this.dept = dept; 22 } 23 24 public IDeptService getDeptService() { 25 return deptService; 26 } 27 28 public void setDeptService(IDeptService deptService) { 29 this.deptService = deptService; 30 } 31 32 public Dept getModel() { 33 return dept; 34 } 35 }