ssh基本整合(配置版本)

本篇文章,和大家分享一下如何使用ssh框架做基本的整合(配置式)

1.首先第一步我们需要部分jar包,我这里用的是idea工具,所以只需要引入对应的pom节点。

 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         <!--SpringWeb-->
14         <dependency>
15             <groupId>org.springframework</groupId>
16             <artifactId>spring-web</artifactId>
17             <version>4.1.8.RELEASE</version>
18         </dependency>
19         <!--JavaEE-->
20         <dependency>
21             <groupId>javaee</groupId>
22             <artifactId>javaee-api</artifactId>
23             <version>5</version>
24         </dependency>
25 
26         <dependency>
27             <groupId>javax.servlet</groupId>
28             <artifactId>jstl</artifactId>
29             <version>1.2</version>
30             <scope>runtime</scope>
31         </dependency>
32 
33         <dependency>
34             <groupId>org.springframework</groupId>
35             <artifactId>spring-tx</artifactId>
36             <version>4.2.5.RELEASE</version>
37         </dependency>
38 
39         <!--c3p0-->
40         <dependency>
41             <groupId>com.mchange</groupId>
42             <artifactId>c3p0</artifactId>
43             <version>0.9.5.2</version>
44         </dependency>
45 
46         <!--hibernate jar包-->
47         <!--jta的jar包-->
48         <dependency>
49             <groupId>javax.transaction</groupId>
50             <artifactId>jta</artifactId>
51             <version>1.1</version>
52         </dependency>
53 
54         <dependency>
55             <groupId>org.hibernate</groupId>
56             <artifactId>hibernate-core</artifactId>
57             <version>5.0.6.Final</version>
58         </dependency>
59 
60         <!--Spring-ORM-->
61         <dependency>
62             <groupId>org.springframework</groupId>
63             <artifactId>spring-orm</artifactId>
64             <version> 4.2.2.RELEASE</version>
65         </dependency>
66 
67         <dependency>
68             <groupId>org.apache.struts</groupId>
69             <artifactId>struts2-core</artifactId>
70             <version>2.3.4.1</version>
71         </dependency>
72 
73         <dependency>
74             <groupId>org.apache.struts.xwork</groupId>
75             <artifactId>xwork-core</artifactId>
76             <version>2.3.4.1</version>
77         </dependency>
78 
79         <!--Struts整合Spring的jar包-->
80         <dependency>
81             <groupId>org.apache.struts</groupId>
82             <artifactId>struts2-spring-plugin</artifactId>
83             <version>2.3.4.1</version>
84         </dependency>
85 
86         <!--Struts2注解支持jar包-->
87         <dependency>
88             <groupId>org.apache.struts</groupId>
89             <artifactId>struts2-convention-plugin</artifactId>
90             <version>2.3.4.1</version>
91         </dependency>
92 
93         <dependency>
94             <groupId>com.oracle</groupId>
95             <artifactId>ojdbc6</artifactId>
96             <version>11.2.0.1.0</version>
97         </dependency>
所需maven节点

2.配置web.xml文件,既然叫ssh整合,那么spring的配置和struts2的配置也就必不可少了。

首先是spring配置,我们需要配置两点:

<!--Spring配置
    1.上下文  识别applicationContext.xml
    2.监听器 在Tomcat容器启动的时候,帮我创建Spring容器,并且放入application中
-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

然后是struts2的配置

<!--Struts2配置  核心过滤器 -->
  <filter>
    <filter-name>struts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

注意:因为配置文件规定的顺序,所以spring的 listener配置要配置在struts2的filter后面。

3.接下来我们关注实体层

我这里以部门类为例

  private Integer deptno;        
private String deptname;
//set,get方法省略

然后配置其hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.ssh.entity">
    <class name="Dept" table="Dept" schema="y2166">
        <id name="deptno" column="DEPTNO">
            <generator class="native"></generator>
        </id>
        <property name="deptname" column="DEPTNAME"></property>
    </class>
</hibernate-mapping>

4.DAO层比较简单两个文件

IDeptDAO

public interface IDeptDAO {
    public int addDept(Dept dept);
}

DeptDAOImpl

public class DeptDAOImpl implements IDeptDAO {
    SessionFactory sessionFactory;
    //set,get方法省略
   
public int addDept(Dept dept) { Session session = sessionFactory.getCurrentSession(); Serializable count = session.save(dept); return (Integer)count; } }

5.service层

IDeptService

public interface IDeptService {
    public int addDept(Dept dept);
}

DeptServiceImpl

   private IDeptDAO dao;
   //set,get方法省略
   @Transactional
   public int addDept(Dept dept) {
        return dao.addDept(dept);
   }

6.action层

  public class DeptAction implements Action{
    private Dept dept;
//set,get方法省略

private IDeptService service; //set,get方法省略
public String execute() throws Exception { service.addDept(dept); return SUCCESS; } }

7.配置文件

首先jdbc.properties文件,此文件专门用于配置连接数据库的属性

jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.username=y2166
jdbc.password=y2166

然后就是最主要的applicationContext.xml文件

此文件内容较多

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
    <!--1.数据源  c3p0-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="driverClass" value="${jdbc.driverClassName}"></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.hbm2ddl.auto">update</prop>
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
            </props>
        </property>
        <!--关联小配置-->
        <property name="mappingDirectoryLocations" value="classpath:cn/ssh/entity"></property>
    </bean>

    <!--4.dao-->
    <bean id="deptDao" class="cn.ssh.dao.DeptDAOImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!--5.service-->
    <bean id="deptService" class="cn.ssh.service.DeptServiceImpl">
        <property name="dao" ref="deptDao"></property>
    </bean>

    <!--6.action-->
    <bean id="deptAction" class="cn.ssh.action.DeptAction">
        <property name="service" ref="deptService"></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>
</beans>

以及最后的struts2文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
   <!--修改这个文件的时候,可以自动重新部署-->
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
        <action name="add" class="deptAction">
            <result>/jsp/index.jsp</result>
        </action>
    </package>
</struts>

8.jsp界面

<form method="post" action="/add">
      部门名称: <input name="dept.deptname"/>
       <input type="submit" value="添加"/>
</form>

9.最终效果

 

点击添加后

数据库

以上就是使用ssh简单的使用

 

posted @ 2018-03-05 15:22  轨迹。  阅读(890)  评论(0编辑  收藏  举报