Spring整合Hibernate Spring的Web項目中,web.xml文件會自動載入,以出現歡迎首頁。也可以在這個文件中對Spring的配置文件進行監聽,自啟動配置文件, 以及之前的整合Struts2,放置過濾器 在Spring的核心配置文件中,進行資料庫連接池配置,建立sessionFac ...
Spring整合Hibernate
Spring的Web項目中,web.xml文件會自動載入,以出現歡迎首頁。也可以在這個文件中對Spring的配置文件進行監聽,自啟動配置文件,
以及之前的整合Struts2,放置過濾器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>2017-12-30_SSH</display-name> <!-- 監聽的文件名 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:bean.xml</param-value> </context-param> <!-- 伺服器啟動自動載入XML配置文件 監聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- structs2過濾器 --> <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> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
在Spring的核心配置文件中,進行資料庫連接池配置,建立sessionFactory對象,直接dao操作,也可以在Spring配置文件中配置Struts2的Action對象
<?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" xmlns:aop="http://www.springframework.org/schema/aop" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- c3p0連接池得到dataSource --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/lastday"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="userAction" class="com.swift.action.UserAction" scope="prototype"></bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- Hibernate核心配置文件沒有連接資料庫,所以需要註入 --> <property name="dataSource" ref="dataSource"></property> <!-- Hibernate核心配置文件的位置 --> <property name="configLocations" value="classpath:hibernate.cfg.xml"></property> </bean> </beans>
Hibernate的核心配置文件中,不需要在寫連接資料庫的屬性,因為已經在Spring的配置文件中用連接池了
<?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> <!-- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sw_database</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property> <property name="hibernate.show_sql">true</property> <!-- create: 先刪表,再建表。 create-drop: 啟動時建表,退出前刪表。 update: 如果表結構不一致,就創建或更新。 validate: 啟動時驗證表結構,如果不致就拋異常。 --> <property name="hibernate.hbm2ddl.auto">update</property> <!--指定映射文件,可映射多個映射文件 --> <mapping resource="com/swift/entity/User.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
Hibernate的這個核心配置文件需要實體類映射文件,體現映射關係
<?xml version="1.0" encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- 實體類映射文件 --> <hibernate-mapping> <class name="com.swift.entity.User" table="t_user"> <!-- 主鍵 --> <id name="uid"> <generator class="native"></generator> </id> <!-- 其他屬性 --> <property name="username"/> <property name="address"/> </class> </hibernate-mapping>
實體類的屬性設置setter和getter方法即可
package com.swift.entity; public class User { private Integer uid; private String username; private String address; public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Struts2的UserAction類代碼
package com.swift.action; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { @Override public String execute() throws Exception { System.out.println("action.................."); return NONE; } }
需要繼承ActionSupport類,覆寫execute()方法
在瀏覽器中運行項目後的地址,加上對象名.action(如userAction.action)就可以了
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" extends="struts-default" namespace="/"> <!-- action的class不要寫全名會創建兩個對象,而寫Spring配置文件中id的內容,只建一個對象 前提有struts2-spring-plugin-2.3.4.1.jar --> <action name="userAction" class="userAction"> </action> </package> </struts>