開發SSH項目的時候搭建開發環境是一項很重要的工作,這篇博客從下載jar包開始一步一步在eclipse中進行配置。配置jdk、安裝eclipse、tomact相信每一個學習ssh框架的同學都輕車熟路,不熟悉的同學請自行百度之。
個人機器及軟件配置:
> 操作系統:Windows 7 64位
jdk:1.7
tomact:6.0
eclipse:: Luna Service Release 2 (4.4.2)
### 一.下載好jar包
首先下載好struts 2.3.4、spring 4.2.3、hibernate4.3.11、commons-logging、mysql驅動、c3p0
1. struts 2下載地址 [http://struts.apache.org/index.html](http://struts.apache.org/index.html)
2. spring下載地址[http://repo.springsource.org/libs-release-local/org/springframework/spring/](http://repo.springsource.org/libs-release-local/org/springframework/spring/)
3. hibernate下載地址[http://hibernate.org/orm/downloads/](http://hibernate.org/orm/downloads/)
4. commons-logging下載地址[http://commons.apache.org/proper/commons-logging/download_logging.cgi](http://commons.apache.org/proper/commons-logging/download_logging.cgi),下載commons-logging-1.2-bin.zip
5. mysql驅動下載地址[http://dev.mysql.com/downloads/connector/j/](http://dev.mysql.com/downloads/connector/j/)
6. c3p0地址地址[[http://mvnrepository.com/artifact/c3p0/c3p0][http://mvnrepository.com/artifact/c3p0/c3p0]](http://mvnrepository.com/artifact/c3p0/c3p0][http://mvnrepository.com/artifact/c3p0/c3p0])
下載好后分別解壓,lstruts-2.3.24-all、spring-framework-4.2.3.RELEASE-dist、hibernate-release-4.3.10.Final、commons-logging-1.2-bin這幾個文件夾內放的都是需要的jar包,以后我們需要加入配置文件的時候就從這幾個文件夾內取。
### 二、安裝eclipse插件
打開eclipse,找到help->eclipse marketplace,分別搜索 hibernate、spring,安裝hibernate tools插件和spring tool suite插件。
### 三、新建動態web工程
打開eclipse,新建dynamic web project,project name可以自定義,target runtime選擇Apache tomact 6.0,dynamic web module version選擇2.5.finish.
在webcontent目錄下新建index.jsp,tomcat中運行,文件目錄及運行效果如下:

### 四、加入spring
首先加入spring,需要下面這三個步驟:加入jar包、配置web.xml、加入spring配置文件
1. 加入jar包
打開spring-framework-4.2.3.RELEASE-dist\spring-framework-4.2.3.RELEASE\libs文件夾,復制以下jar包到WebContent/WEB-INF/lib:
> spring-aspects-4.2.3.RELEASE.jar
spring-beans-4.2.3.RELEASE.jar
spring-context-4.2.3.RELEASE.jar
spring-core-4.2.3.RELEASE.jar
spring-expression-4.2.3.RELEASE.jar
spring-instrument-4.2.3.RELEASE.jar
spring-jdbc-4.2.3.RELEASE.jar
spring-jms-4.2.3.RELEASE.jar
spring-messaging-4.2.3.RELEASE.jar
spring-orm-4.2.3.RELEASE.jar
spring-test-4.2.3.RELEASE.jar
spring-tx-4.2.3.RELEASE.jar
spring-web-4.2.3.RELEASE.jar
spring-webmvc-4.2.3.RELEASE.jar
spring-websocket-4.2.3.RELEASE.jar
打開commons-logging-1.2,找到commons-logging-1.2.jar,復制到lib目錄下.然后選中所有jar包,add to build path.
2.配置web.xml
打開web.xml,安裝spring tools插件后按alt+/自動補全鍵會提示,向上找到ContextLoaderListener.會自動生成以下代碼:
~~~
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>location</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
~~~
3.加入spring配置文件
點擊工程名,新建source folder(注意:不是folder),命名為conf,用來存放配置文件.在conf目錄下新建Spring Bean Definition file,名稱為applicationContext.xml.將web.xml中spring配置中的location改為:classpath:applicationContext.xml.
~~~
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
~~~
### 五、設置c3p0數據庫連接池
1.加入c3p0和mysql連接驅動.把下面兩個jar包加到lib目錄,每次加入新的jar包都要add to build path.
> c3p0-0.9.1.2.jar
mysql-connector-java-5.1.38-bin.jar
2.在conf目錄下新建資源文件
添加db.properties文件
~~~
jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///sshdb
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
~~~
更新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/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.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!-- 導入資源文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置C3P0數據源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<bean id="sessionFactorys" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:cn/ac/ucas/form/*.hbm.xml"></property>
</bean>
</beans>
~~~
### 六、spring整合hibernate
1. 加入jar包
將hibernate-release-4.3.10.Final\lib\required目錄下的jar包全部copy到工程lib目錄下.
1. 添加hibernate配置文件
在conf目錄下新建hibernate.cfg.xml,并配置hibernate基本屬性.
~~~
<?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基本屬性 -->
<!-- 方言 ctrl+shift+t 輸入mysql5,找到org.hibernate.dialect.MySQL5InnoDBDialect-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 是否顯示格式化sql -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 生成數據表的策略 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--二級緩存配置相關 -->
</session-factory>
</hibernate-configuration>
~~~
###七.創建持久化類
1. 新建cn.ac.ucas.form包,里面新建兩個類.
員工所屬部門類:
~~~
package cn.ac.ucas.form;
public class Department {
private Integer id;
private String departmentName;//部門名稱
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
~~~
員工信息類:
~~~
package cn.ac.ucas.form;
import java.util.Date;
public class Employee {
// id不能修改
private Integer id;
private String lastName;
private String email;
// 從前端傳來的是string類型,需要注意轉換
private Date birth;
// 創建時間不能被修改
private Date createTime;
// 單向多對一的關聯關系.
private Department department;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
~~~
點擊包名,new->other->hibernate->hibernate xml mapping file,會自動生成持久化類對應的.hbm.xml文件.
Department.hbm.xml
~~~
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-12-15 12:07:42 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="cn.ac.ucas.form.Department" table="SSH_DEPARTMENT">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="departmentName" type="java.lang.String">
<column name="DEPARTMENT_NAME" />
</property>
</class>
</hibernate-mapping>
~~~
Employee.hbm.xml
~~~
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-12-15 12:07:42 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="cn.ac.ucas.form.Employee" table="SSH_EMPLOYEE">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="lastName" type="java.lang.String">
<column name="LASTNAME" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" />
</property>
<property name="birth" type="java.util.Date">
<column name="BIRTH" />
</property>
<property name="createTime" type="java.util.Date">
<column name="CREATETIME" />
</property>
<many-to-one name="department" class="cn.ac.ucas.form.Department" fetch="join">
<column name="DEPARTMENT" />
</many-to-one>
</class>
</hibernate-mapping>
~~~
運行項目,成功的話會在數據庫中生成數據庫表.

###八.spring整合struts
1.加入jar包
把struts-2.3.24\apps\struts2-blank\WEB-INF\lib目錄下的jar拷貝到工程lib目錄下.
web.xml中加入下面代碼:
~~~
<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>/*</url-pattern>
</filter-mapping>
~~~
至此ssh項目環境配置已經完成.
代碼下載地址:[http://download.csdn.net/detail/napoay/9358803](http://download.csdn.net/detail/napoay/9358803)
- 前言
- [J2EE]java web項目中調用word轉html命令行工具
- [J2EE]jsp項目中使用UEditor富文本編輯器
- [J2EE]The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- [j2ee]Eclipse搭建SSH開發框架
- Could not open Hibernate Session for transaction
- class org.springframework.web.context.ContextLoaderListener
- [java01]Java基本數據類型
- [java02]運算符
- jsp、javabean學生信息管理系統
- [java03]java字符串
- [ssh新聞發布系統一]搭建開發環境
- [ssh新聞發布系統二] 讀取新聞
- [ssh新聞發布系統三]存儲新聞
- [ssh新聞發布系統四]使用富文本編輯器發布新聞
- [ssh新聞發布系統五]刪除新聞
- struts2 helloworld
- struts請求走向流程
- [java04]java大數類