SSH注解整合

Posted 不羁少年;

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSH注解整合相关的知识,希望对你有一定的参考价值。

首先需要导入依赖jar包

复制代码
  1   <!--spring配置-->
  2         <dependency>
  3             <groupId>org.springframework</groupId>
  4             <artifactId>spring-context</artifactId>
  5             <version>4.2.0.RELEASE</version>
  6         </dependency>
  7         <!--aop使用的jar-->
  8         <dependency>
  9             <groupId> org.aspectj</groupId >
 10             <artifactId> aspectjweaver</artifactId >
 11             <version> 1.8.7</version >
 12         </dependency>
 13 
 14         <!--SpringWeb-->
 15         <dependency>
 16             <groupId>org.springframework</groupId>
 17             <artifactId>spring-web</artifactId>
 18             <version>4.1.8.RELEASE</version>
 19         </dependency>
 20 
 21         <!--JavaEE-->
 22         <dependency>
 23             <groupId>javaee</groupId>
 24             <artifactId>javaee-api</artifactId>
 25             <version>5</version>
 26         </dependency>
 27 
 28         <dependency>
 29             <groupId>javax.servlet</groupId>
 30             <artifactId>jstl</artifactId>
 31             <version>1.2</version>
 32             <scope>runtime</scope>
 33         </dependency>
 34 
 35         <dependency>
 36             <groupId>org.springframework</groupId>
 37             <artifactId>spring-tx</artifactId>
 38             <version>4.2.5.RELEASE</version>
 39         </dependency>
 40 
 41         <!--c3p0-->
 42         <dependency>
 43             <groupId>com.mchange</groupId>
 44             <artifactId>c3p0</artifactId>
 45             <version>0.9.5.2</version>
 46         </dependency>
 47 
 48         <!--hibernate jar包-->
 49         <!--jta的jar包-->
 50         <dependency>
 51             <groupId>javax.transaction</groupId>
 52             <artifactId>jta</artifactId>
 53             <version>1.1</version>
 54         </dependency>
 55 
 56         <dependency>
 57             <groupId>org.hibernate</groupId>
 58             <artifactId>hibernate-core</artifactId>
 59             <version>5.0.6.Final</version>
 60         </dependency>
 61 
 62         <!--Spring-ORM-->
 63         <dependency>
 64             <groupId>org.springframework</groupId>
 65             <artifactId>spring-orm</artifactId>
 66             <version> 4.2.2.RELEASE</version>
 67         </dependency>
 68 
 69         <!--Oracle驱动的jar-->
 70         <dependency>
 71             <groupId>com.oracle</groupId>
 72             <artifactId>ojdbc6</artifactId>
 73             <version>11.2.0.1.0</version>
 74         </dependency>
 75 
 76         <dependency>
 77             <groupId>org.apache.struts</groupId>
 78             <artifactId>struts2-core</artifactId>
 79             <version>2.3.4.1</version>
 80         </dependency>
 81 
 82         <dependency>
 83             <groupId>org.apache.struts.xwork</groupId>
 84             <artifactId>xwork-core</artifactId>
 85             <version>2.3.4.1</version>
 86         </dependency>
 87 
 88         <!--Struts整合Spring的jar包-->
 89         <dependency>
 90             <groupId>org.apache.struts</groupId>
 91             <artifactId>struts2-spring-plugin</artifactId>
 92             <version>2.3.4.1</version>
 93         </dependency>
 94 
 95         <!--Struts2注解支持jar包-->
 96         <dependency>
 97             <groupId>org.apache.struts</groupId>
 98             <artifactId>struts2-convention-plugin</artifactId>
 99             <version>2.3.4.1</version>
100         </dependency>
复制代码

resources层:

applicationContext.xml:

复制代码
 1 <!--
 2 1.数据源  c3p0
 3 -->
 4     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 5         <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
 6         <property name="driverClass" value="${jdbc.driverClass}"></property>
 7         <property name="user" value="${jdbc.username}"></property>
 8         <property name="password" value="${jdbc.password}"></property>
 9     </bean>
10 
11     <!--
12  2.识别到jdbc.properties
13 -->
14     <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
15     <!--
16  3.形成SessionFactory
17 -->
18     <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
19         <property name="dataSource" ref="dataSource"></property>
20         <property name="hibernateProperties">
21             <props>
22                 <!--方言-->
23               <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
24                 <!--是否打印sql-->
25                 <prop key="hibernate.show_sql">true</prop>
26                 <prop key="hibernate.format_sql">true</prop>
27                 <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
28             </props>
29         </property>
30         <!--关联小配置-->
31          <property name="packagesToScan" value="entity"></property>
32     </bean>
33 
34 <!--
35 7.事务管理器
36 -->
37     <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
38         <property name="sessionFactory" ref="sessionFactory"></property>
39     </bean>
40 <!--
41 8.事务真实配置
42 -->
43     <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 }
复制代码

 

dao:

IDeptDAO:

DeptDAO.xml:

复制代码
 1 @Repository("deptDAO")
 2 public class DeptDAOImpl implements IDeptDAO {
 3     @Resource(name = "sessionFactory")  //JDK 1.4
 4             /*@Autowired()
 5             @Qualifier("sessionFactory")*/
 6     SessionFactory sessionFactory;
 7     public int addDept(Dept dept) {
 8         Session session = sessionFactory.getCurrentSession();
 9         Serializable count = session.save(dept);
10         return (Integer)count;
11     }
12 
13     public SessionFactory getSessionFactory() {
14         return sessionFactory;
15     }
16 
17     public void setSessionFactory(SessionFactory sessionFactory) {
18         this.sessionFactory = sessionFactory;
19     }
20 }
复制代码

 

service:

IDeptServcie:

DeptServiceImpl:

 1 @Service("deptService")
 2 public class DeptServiceImpl implements IDeptService {
 3     @Resource(name = "deptDAO")
 4     IDeptDAO dao;
 5     @Transactional
 6     public int addDept(Dept dept) {
 7         return dao.addDept(dept);
 8     }
 9 
10     public IDeptDAO getDao() {
11         return dao;
12     }
13 
14     public void setDao(IDeptDAO dao) {
15         this.dao = dao;
16     }
17 }
复制代码

 

entity:

复制代码
 1 @Entity
 2 @Table(name = "Dept")
 3 public class Dept {
 4     @Id
 5     @GeneratedValue  //native  xml中native  Oracle
 6     private Integer deptno;
 7     @Column
 8     private String deptname;
 9 
10     public Integer getDeptno() {
11         return deptno;
12     }
13 
14     public void setDeptno(Integer deptno) {
15         this.deptno = deptno;
16     }
17 
18     public String getDeptname() {
19         return deptname;
20     }
21 
22     public void setDeptname(String deptname) {
23         this.deptname = deptname;
24     }
25 }
复制代码

然后配置web.xml:

复制代码
 1   <!--Spring配置
 2  1.上下文  识别applicationContext.xml
 3 
 4  2.监听器 在Tomcat容器启动的时候,帮我创建Spring容器,并且放入application中-->
 5   <context-param>
 6     <param-name>contextConfigLocation</param-name>
 7     <param-value>classpath:applicationContext.xml</param-value>
 8   </context-param>
 9 
10   <!--Struts2配置  核心过滤器 -->
11   <filter>
12     <filter-name>struts</filter-name>
13     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
14   </filter>
15   <filter-mapping>
16     <filter-name>struts</filter-name>
17     <url-pattern>/*</url-pattern>
18   </filter-mapping>
19 
20   <listener>
21     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
22   </listener>

以上是关于SSH注解整合的主要内容,如果未能解决你的问题,请参考以下文章

SSH整合步骤之注解和非注解

SSH注解整合

ssh全注解整合

ssh整合之七注解结合xml形式

SSH整合总结(xml与注解)

SSH整合案例注解式