搭建ssh框架

来源:互联网 发布:kali mysql暴力破解 编辑:程序博客网 时间:2024/06/06 00:58


范围

本次框架搭建的版本是Struts2.3.12+Spring3.2.2+Hibernate4.2.0,截止2013年3月18日是最新的版本。本次搭建的过程是先搭建Struts2.3.12,测试通过之后,整合Spring3.2.2,最后再整合Hibernate4.2.0,最后的整合内容有点多,因为这也是居于MVC思想搭建的环境。

Struts2搭建

搭建所需jar包

序号

名称

描述

1

commons-fileupload-1.2.2.jar

文件上传组件

2

commons-io-2.0.1.jar

io包

3

commons-lang3-3.1.jar

基础工具包,封装一些常用的基础操作

4

commons-loggin-1.1.1.jar

日志组件

5

freemarker-2.3.19.jar

一个基于模板生成文本输出的通用工具

6

ognl-3.0.6.jar

对象图导航语言(jar包库) 

7

strut2-core-2.3.12.jar

Struts2核心jar包

8

xwork-core-2.3.12.jar

xwork核心包

9

javassist-3.15.0-GA.jar

扩展java类和实现



整合Spring3

整合所需jar包

序号

名称

描述

1

struts2-spring-plugin-2.3.12.jar

Struts2+Spring整合包

2

spring-core-3.2.2.jar

Spring核心工具包

3

spring-context-3.2.2.jar

为Spqing提供核心扩展

4

spring-context-support-3.2.2.jar

为Spqing提供核心扩展

5

spring-beans-3.2.2.jar

管理beans的工具

6

spring-expression-3.2.2.jar

表达式语言

7

spring-web-3.2.2.jar

Web应用开发包

8

spring-aspects-3.2.2.jar

提供对AspectJ的支持,以便可以方便的将面向方面的功能集成进IDE中



 整合Hibernate4

整合所需jar包

序号

名称

描述

1

spring-aop-3.2.2.jar

事务管理

2

spring-jdbc-3.2.2.jar

对JDBC封装

3

spring-orm-3.2.2.jar

ORM映射支持

4

spring-tx-3.2.2.jar

Spring配置事务管理

5

hibernate-core-4.2.0.CR2.jar

Hibernate的核心模块

6

hibernate-jpa-2.0-api-1.0.1.Final.jar

用来定义java持久性

7

hibernate-commons-annotations-4.0.1.Final.jar

注解

8

aopalliance-1.0.jar

AOP提供了最普通和通用的接口

9

jta-1.1.jar

事务管理

10

antlr-2.7.7.jar

执行sql查询

11

dom4j-1.6.1.jar

Xml配置和映射解释器

12

proxool-0.9.1.jar

配置连接池的工具类

13

proxool-cglib.jar

数据库代理

14

jboss-logging-3.1.0.GA.jar

日志

15

jboss-transaction-api_1.1_spec-1.0.0.Final.jar

事务处理

16

mysql-connector-java-5.1.8-bin.jar

Mysql连接驱动

最后导入一个jar(aspectjweaver.jar)

 

 

加入配置文件

加入web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <display-name>mgwx</display-name>
 <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>
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>
public class UserDaoImpl<T> extends BasicHibernateDao {
 
 public void addUser(T t) {
  // TODO Auto-generated method stub
  getSession().save(t);
 }

// 
// public T get(Serializable id) {
//  return (T)super.getSession().get(, id);
// }
 
 
 @SuppressWarnings("unchecked")
 public ArrayList<T> list() {
  String hql = "from User";
  Query query = getSession().createQuery(hql);
  return (ArrayList<T>) query.list();
 }
 
 
 @SuppressWarnings("unchecked")
 public ArrayList<T> queryForPage(int offset, int length) {
  Query query = getSession().createQuery("from User u order by u.name asc");
  query.setFirstResult((offset-1)*length);
  query.setMaxResults(length);
  return (ArrayList<T>) query.list();
 }
 
 
 public void delete(List<T> list) throws OperationException {
  try{
   for(T t:list)
   {
    this.getSession().delete(t);
   }
  }catch(Exception e)
  {
   throw new OperationException("批量删除失败");
  }
 }

}


增加applicationContext-config.xml

<?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:jee="http://www.springframework.org/schema/jee"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
 xmlns:tool="http://www.springframework.org/schema/tool" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/jee
           http://www.springframework.org/schema/jee/spring-jee.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util.xsd
           http://www.springframework.org/schema/tool
           http://www.springframework.org/schema/tool/spring-tool.xsd"
 default-lazy-init="true" default->
 <context:component-scan base-package="cn.cdi.test">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
 <!-- 配置SessionFactory,由Spring容器来管理Hibernate -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
 </bean>
 <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" > 
   <property name="sessionFactory">
    <ref bean="sessionFactory"/>
  </property>
 </bean>
 
 <!-- 需要引入tx的命名空间 --> 
 <!-- 这是事务通知操作(传播特性),使用的事务管理器引用自 transactionManager --> 
 <tx:advice id="txAdvice" transaction-manager="txManager"> 
        <tx:attributes> 
         <!-- 指定哪些方法需要加入事务,这里懒惰一下全部加入,可以使用通配符来只加入需要的方法 --> 
             <tx:method name="save*" propagation="REQUIRED" rollback-for="OperationException"/>  <!-- 根据每个(类)方法配置事务的传播特性 -->
             <tx:method name="modify*" propagation="REQUIRED" rollback-for="OperationException"/> 
             <tx:method name="delete*" propagation="REQUIRED" rollback-for="OperationException"/> 
             <tx:method name="get*" propagation="REQUIRED" read-only="true"/> 
             <tx:method name="query*" propagation="REQUIRED" read-only="true"/> 
             <tx:method name="*" propagation="REQUIRED" /> 
         </tx:attributes> 
   </tx:advice>  
  
     <!-- 需要引入aop的命名空间(配置事物的切入点) --> 
    <aop:config proxy-target-class="true"> 
        <!-- 切入点指明了在执行Service的所有方法时产生事务拦截操作 --> 
        <aop:pointcut id="serviceMethods" expression="execution(* cn.cdi.*.business.*Business.*(..))" />     
       <!-- 定义了将采用何种拦截操作,这里引用到 txAdvice --> 
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" /> 
    </aop:config> 
</beans>


增加applicationContext-hibernate.xml

<?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"
 xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="configLocation">
   <value>classpath:hibernate.cfg.xml</value>
  </property>
 </bean>
</beans>


增加applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
 <!-- beans 的配置文件 -->
 <import resource="applicationContext-beans.xml"/>
 <import resource="applicationContext-config.xml"/>
 <import resource="applicationContext-hibernate.xml"/>
</beans>

 

增加hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
 <session-factory>

  <!-- hibernate dialect -->
  
  <!-- JDBC connection properties (begin) -->
  <!-- MySQL --> 
   <property name="hibernate.connection.username">root</property>
   <property name="hibernate.connection.password">111111</property>
   <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
   <property name="hibernate.connection.url">jdbc:mysql://192.168.6.171:3306/myuserdatabase</property>
   <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.use_outer_join">true</property>
      <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
   <property name="hibernate.hbm2ddl.auto">update</property>
   <property name="hibernate.show_sql">true</property>
   <property name="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</property>
   <!-- 数据库连接池
      <property name="c3p0.min_size">5</property>
   <property name="c3p0.max_size">30</property>
   <property name="c3p0.time_out">1800</property>
   <property name="c3p0.max_statement">50</property>
       -->

        
 </session-factory>
</hibernate-configuration>

0 0
原创粉丝点击