您的位置:首页 > 编程语言 > Java开发

spring-springmvc-hibernate整合之配置文件

2017-05-10 20:53 477 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  web.xml