[ssh] spring-springmvc-hibernate整合之配置檔案
阿新 • • 發佈:2019-01-06
1.web.xml檔案:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--spring mvc-->
<servlet>
<servlet-name>stuManagement</servlet-name>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--配置檔名稱及路徑-->
<param-value>classpath:config/springmvc.xml</param-value>
</init-param>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1 </load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>stuManagement</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!---->
<!--spring-hibernate-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!--配置檔名稱及路徑-->
<param-value>classpath:config/spring-hibernate.xml</param-value>
</context-param>
<!---->
<!--字元編碼-->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!---->
</web-app>
2.spring-mvc配置檔案:
依賴的jar包:
1. spring-framwork.jar ,commons-log.jar ,commons-dbcp.jar ,commons-pool.jar
2. com.fasterxml.jackon.annotations.jar ,jackon-core.jar ,jackon-databind.jar
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<mvc:annotation-driven/>
<!--控制器(bean)-->
<context:component-scan base-package="Controller"/>
<!---->
<!--攔截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/Management/*"/> <!-- 管理員登入攔截 -->
<bean class="Interceptor.UserInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!--json資料轉換器-->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!---->
<!--檢視解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/View/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!---->
</beans>
3.spring-hibernate配置檔案:
依賴的jar包:
1. hibernate-required.jar ,mysql-connection-java.jar
<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: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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--bean-->
<context:component-scan base-package="Model"/>
<!---->
<!--注入session factory-->
<!--指定JDBC要連線的資料庫-->
<bean id="DataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/資料庫名"/>
<property name="username" value="賬戶名"/>
<property name="password" value="密碼"/>
</bean>
<!---->
<!--配置hibernate-->
<bean id="SessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource"/>
<!--配置hibernate關於資料庫操作-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</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>
<!---->
<!---->
<!--配置事務管理機制-->
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory"/>
</bean>
<!---->
</beans>