SpringMVC+Hibernate的配置文件

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
	<display-name>Dispatcher-Servlet</display-name>
	<!-- 统一编码 -->
	<filter>
		<filter-name>charsetEncoding</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>charsetEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 前端控制器 -->
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 加载/WEB-INF/[servlet-name]-servlet.xml -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

dispatcher-servlet.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd">

    <!-- 扫描路径 -->
    <context:component-scan base-package="xxx.xxx.xxx.xxx" />

    <!-- 配置根视图 -->
    <mvc:view-controller path="/" view-name="index"/>

    <!-- 激活基于注解的配置 @RequestMapping, @ExceptionHandler,数据绑定 ,@NumberFormat , @DateTimeFormat ,@Controller ,@Valid ,@RequestBody ,@ResponseBody等 -->
    <mvc:annotation-driven />

    <!-- 静态资源配置 -->
    <!-- <mvc:resources location="/assets/" mapping="/assets/**"></mvc:resources> -->

    <!-- 视图层配置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/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/databasename</property> -->
		<!-- 解决中文传入到数据库编程???的问题 -->
		<property name="hibernate.connection.url"><![CDATA[jdbc:mysql://localhost:3306/databasename?useUnicode=true&characterEncoding=utf8]]></property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password"></property>
			<!-- 数据库方言 使用的数据库类型 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		
		<!-- 可选配置 -->
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		
		<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<property name="hibernate.c3p0.max_size">5</property>
		<property name="hibernate.c3p0.min_size">10</property>
		<property name="hibernate.c3p0.timeout">120</property>
		
		<!-- auto create table -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<!-- 把session绑定到当前线程,使用getCurrentSession获取当前session -->
		<property name="hibernate.current_session_context_class">thread</property>
		
		
		<!-- 配置映射文件 -->
	 	<mapping resource="xxx/xxx/xxx/xxx.hbm.xml" />
	 	
	 	
	</session-factory>
</hibernate-configuration>


hibernate Entity
Entity.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
    	<class name="xxx.xxx.xxx" table="tablename">
    		<id name="id" column="id">
    			<!-- native 自增长 -->
    			<generator class="native"></generator>
    		</id>

    		<property name="xxx" column="xxx"></property>
    		
    		<!-- 多对一关系配置 name对应javaBean中的客户属性 colum外键名称 -->
    		<many-to-one name="xxx" class="xxx.xxx.xxx" column="xxx"></many-to-one>
    		
    		
    		<!-- 一对多配置 -->
    		<set name="xxx">
    			<!-- 外键 -->
    			<key column="xxx"></key>
    			<!-- 一对多关系 -->
    			<one-to-many class="xxx.xxx.xxx" />
    		</set>
    	</class>
    	
    </hibernate-mapping>