时代发展迅速,技术更新迅猛,越来越多优秀的技术站在巨人的肩膀上被开发,此篇文档是关于作者在实战项目中对ssh总结的一些使用经验,这也是作者的处女作,仁者见仁智者见智望各位给原创作者一点鼓励不喜勿喷。
1:本人主要是使用maven开发的ssh项目,所以经验之谈也是基于maven而言,首先网速一定要好,因为有很多依赖要下载,一旦中途网络出错就要删掉仓库重头再来。因为是配置版的ssh项目,最重要的当然是配置文件了,但是配置文件都有个构建的先后顺序,应该按照:先配置最底层的hibernate,然后spring,最后struts,所有的配置文件最后都归与web.xml加载。
ps:基于本人实际犯过的错误,我觉得有必要给一些有需要的博友讲一下:eclipse里每新建一个工作区间都需要配置一下maven环境,就是在Windows-preferences-maven-installations,user settings选择你的maven安装环境。如果你自己工作区间选择的仓库路径跟你maven安装路径里面默认仓库不一样,你可以把maven-config-setting.xml这个文件copy到你项目选择的那个仓库路径下,这个时候它的setting.xml文件在这里是局部的,但是跟maven安装路径下的是一模一样的,所以你项目随便引用那个都是一样的。
D盘是我自定义的仓库路径,C盘是系统默认的
2:关于pom文件的依赖,最好是需要什么就加什么
以下是我一个项目基本需要的依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 引入Servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<!-- 引入Hibernate依赖 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
<!-- 引入Mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.43</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>
</dependencies>
3:配置hibernate时:每个实体类最开始都要配置一个X(表名).hbm.xml配置文件,里面是各种驱动类让hibernate与数据库连接的api配置,以及实体类与表之间的映射。但是实际上hibernate.cfg.xml里的东西都可以提取到db.properties最后在spring加载
以下是相关截图:
4:接下来就是配置的重头戏了!!!
maven项目中src的每个包都要配置spring配置文件:先引入db.properties文件;然后配置数据源,并且不要忘记配好数据库的连接池;接下来配置crud数据时需要用到的相关api,比如在配置sessionfactory是:先 引入数据源,然后加载hibernate配置文件,最后加载映射文件;
以下是我applicationContext-public.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:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 引入db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置数据源:配置数据库连接池c3p0 -->
<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>
<!-- 加载hibernate配置文件 -->
<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>
5:然后就是Struts2的配置文件:
ps,Struts2配置文件有自己的命名约束,比如filter注解不能写在context-param前面
<!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>
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>location</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>
</web-app>
6:恭喜你!终于到最后一步了:web.xml
ps:strutsPrepareAndExecuteFilter是 加载struts2的核心类
contextconfiglocation是加载spring的核心类![]()
contextconfiglocation是加载spring的核心类
7:当你完成以上所有步骤,此时你就该有勇气发布你的汤姆猫咯~
总结:永远相信一个完整健康的ssh项目背后绝对拥有一堆精简的配置文件!!
很多人会觉得ssm框架用注解代替配置文件更简单方便,跪求各位大佬千万不要因此看不上ssh,毕竟现在很多大公司依旧离不开ssh框架去运行和维护以前那些经典的crm等等项目,所以前人栽树后人乘凉各自芬芳嘛~~