您的位置:首页 > 编程语言 > Java开发

用eclipse开发工具,maven进行包管理,整合SSH框架

2017-12-17 23:31 417 查看
整合SSH 后台我大概都用了truts2,Spring,Hibernate,Maven,等插件进行整合,在界面用的是bootstrap+Vue.js的结合
简洁陈诉一下我的思路吧!
之前使用的都是已注解的形式对SSH进行整合,而现在则是用xml 来代替注解的功能(其实个人觉得哈还是注解用的比较舒服,不容易报错)
首先:
1。在pom.xml中引入必备的jar包 (未避免下载不必要的JAR 包,建议一边整合,根据整合的需求来下载JAR包,如果之前仓库有下载的版本建议使用仓库内用过的版本)
这是我所使用的所有jar 包 以及版本  我的eclipse 是 4.6.3 的
<!--servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>

<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>

<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>

<!-- c3p0数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.1</version>
</dependency>

<!-- Hibernate整合Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>

<!-- 引入spring-aspects:解析事务的表达式 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>

<!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.33</version>
</dependency>

<!-- struts2整合Spring的 插件包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.12</version>
</dependency>

<!-- log4J -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.40</version>
</dependency>

<!-- 引入Axis2依赖 -->
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2</artifactId>
<version>1.6.2</version>
</dependency>

<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>1.6.2</version>
</dependency>

<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>1.6.2</version>
</dependency>

<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.4.0</version>
</dependency>

<!-- axis2整合spring -->
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-spring</artifactId>
<version>1.6.2</version>
</dependency>


2。在web.xml中配置Spring的监听器ContextLoaderListener,注意此时没有加入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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:c="http://www.springframework.org/schema/c"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 引入db.Properties -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置数据源: 配置数据库的链接池c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${uname}"></property>
<property name="password" value="${upass}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="initialPoolSize" value="${initPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>

<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 引入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载hibernate -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 加载映射文件 -->
<property name="mappingLocations" value="classpath:com/entity/*.hbm.xml"></property>

</bean>
<!-- 配置事物管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事物的属性 -->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="*"/>
</tx:attributes>
</tx:advice>

<!-- 配置事物的切点 -->
<aop:config>
<aop:pointcut expression="execution(* com.dao.*.*(..))"
id="myPoint" />
<aop:advisor advice-ref="myAdvice" pointcut-ref="myPoint" />
</aop:config>

</beans>

( 这里的 classpath:db.properties 是一个 同名的文件它的后缀是 properties 里面都是这样的 在引用的 界面则用变量名即可)
uname=root
upass=123
url=jdbc:mysql://localhost:3306/test
driverClass=com.mysql.jdbc.Driver

initPoolSize=5
maxPoolSize=20


3.所有配置写完后 记得为web 中配置
(有些东西感觉描述不清...)
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>

<!-- 加载 struts2 -->
<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>*.action</url-pattern>
</filter-mapping>

<!-- 加载spring -->
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

接下来是最重要的一步,也是项目整合是否成功的检测;

4.客户端浏览器访问action
通过action的依赖对象访问userService的save方法
通过依赖对象访问Dao的所有方法

如果你发现所有的功能都能实现,那就可以啦
当然如果之前有完整的SSH 项目那么整合起来是非常容易的!

以上就是我整合项目的过程!



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息