來源於:http://www.jianshu.com/p/8e2f92d0838c
具體配置參數:
Spring: spring-framework-4.2.2
Hibernate: hibernate-release-4.2.21.Final
Eclipse : eclipse MARS.2
MySQL : mysql 5.5 +Navicat Premiumd視圖器
System: win 8.1
Spring-framework下載(附地址)
需要下載的文件前面兩個就夠了,包和參考文檔
- spring-framework-4.2.2.RELEASE-dist.zip 包
-
spring-framework-4.2.2.RELEASE-docs.zip 文檔
-
spring-framework-4.2.2.RELEASE-schema.zip 配置
導入SSH框架整合所需的jar包
- Spring的jar包
- Hibernate(附地址)的jar包
- 第三方jar包(日志包)
- 數據庫的jar包

配置web.xml
手動創建一個config文件夾用與存放配置的文件,這里方便說明記為cf
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:xx.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
其中xx.xml文件是需要你在cf中手動創建的配置文件,里面具體內容后面配置,這里相當於是告訴系統文件在哪,這里為了說明方便命名為spring.xml
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:xxx.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
這里的xxx.xml同上,可自己命名,這里命名為springMVC.xml
其中filter-class里的名字不用記,可以通過ctrl+shift+T輸入
CharacterEncodingFilter獲得,且這一步需放在所有過濾器最前面,才有效果
<filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
其中filter-name里的類名可以通過ctrl+shift+T輸入HiddenHttpMethodFilter獲得,由於瀏覽器form表單只支持GET與POST請求,而DELETE、PUT等method並不支持,Spring3.0添加了一個過濾器,可以將這些請求轉換為標准的http方法,使得支持GET、POST、PUT與DELETE請求,該過濾器為HiddenHttpMethodFilter
<filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
-
配置SpringMVC
- 導入命名空間
打開我們自己定義的配置文件springMVC.xml選擇Namespace,勾選context和MVC -
<context:component-scan base-package="com.jikexueyuancrm" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan>
-
其中class類名可以通過ctrl+shift+T輸入InternalResourceViewResolver獲得
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean>
-
<mvc:default-servlet-handler />
-
<mvc:annotation-driven />
- 導入命名空間
-
配置Spring
- 導入命名空間
打開我們自己定義的配置文件spring.xml選擇Namespace,勾選context(上下文)和tx(事物) -
<context:component-scan base-package="com.jikexueyuancrm" use-default-filters="false"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /> </context:component-scan>
-
<context:property-placeholder location="classpath:db.properties" />
-
jdbc.user=root jdbc.passowrd= jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql:///jianlam
-
這里用到cp30的包,C3P0是一個開源的JDBC連接池,它實現了數據源和JNDI綁定,支持JDBC3規范和JDBC2的標准擴展。
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.passowrd}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> </bean>
-
需要orm包-Object Relational Mapping 對象關系映射,POJO(Plain Ordinary Java Object)簡單的Java對象,實際就是普通JavaBeans
<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory"> <!-- 配置數據源 --> <property name="dataSource" ref="dataSource"></property> <!-- 找到實體包(pojo) --> <property name="namingStrategy"> <bean class="org.hibernate.cfg.ImprovedNamingStrategy"></bean> </property> <property name="packagesToScan" value="com.jianlam.entity"></property>
-
<property name="hibernateProperties"> <props> <!-- 數據庫的方言 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean>
-
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
- 導入命名空間
測試
創建好實體類和數據庫表,查看連接操作數據庫是否能正常運作,若沒有問題則配置成功!
private spring ctx = null; @Test public void testDataSource() throws SQLException { //檢查spring配置 ctx = new ClassPathXmlApplicationContext("spring.xml"); // System.out.println(ctx); //檢查數據庫連接 DataSource dataSource = ctx.getBean(DataSource.class); // System.out.println(dataSource.getConnection().toString()); //檢查hibernate配置 SessionFactory sessionFactory = ctx.getBean(SessionFactory.class); System.out.println(sessionFactory); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); //測試數據庫 Users user = new Users ("jianlam", "123456"); session.save(user); tx.commit(); session.close(); }
如果你的mysql有問題或對於MVC模式下所需的包分類有所困惑可參考:這里
這是我找到的最好的配置文件之一,本來今天想整一個好一點的到網上。因為網上很多都是很糟糕的,各種問題。樓主這個好,每個配置都說的很清楚,終於完整的知道這些東西,是什么用的了。可惜不是maven的。我用的maven。對了,樓主有個類沒寫出來,一般新手要調試半天,我發出來 import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Users {
@ID
@column(name = "id", unique = false, nullable = false)
private int id;
private String name;
private String password;
public Users(String name, String password) {
super();
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
再次感謝!