你的位置:首页 > Java教程

[Java教程]SpringMVC+JPA+Hibernate配置


首先,Spring配置文件 

<?<beans
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">

<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter" >
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean >
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter"/> <!-- json转换器 -->
</list>
</property>
</bean>

<!-- 开启基本扫描配置 -->
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.as.web">
<context:exclude-filter type="regex" expression=".controller"/>
<context:exclude-filter type="regex" expression=".service.impl"/>
</context:component-scan>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>

<!-- 加载jdbc的配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 注入c3p0连接池 -->
<bean id="dataSource"
destroy-method="close">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${user}" />
<property name="password" value="${password}" />
<!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="1" />
<!-- 连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="1" />
<!-- 连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="300" />
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60" />
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!-- 每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60" />
</bean>

<!-- 注入jpa事物模型 -->
<bean id="entityManagerFactory"
>
<property name="dataSource" ref="dataSource"></property>
<property name="persistence <property name="loadTimeWeaver">
<bean />
</property>
</bean>

<!-- 开启jpa的事物 -->
<bean id="transactionManager" >
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

<!-- 视图解析器 -->
<bean id="view" >
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<!-- 开启文件上传功能 -->
<bean >
<property name="defaultEncoding" value="UTF-8"></property>
</bean>

</beans>

 

JPA配置文件, 文件名为 persistence.

<?<persistence version="1.0"
xsi:schemaLocation="http://java.sun.com/ <persistence-unit name="kuanyu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<!-- 加入spring之后,不必要的配置 <property name="hibernate.connection.driver_class"
value="org.gjt.mm.mysql.Driver" /> <property name="hibernate.connection.username"
value="root" /> <property name="hibernate.connection.password" value="root"
/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/myshop?useUnicode=true&amp;characterEncoding=UTF-8"
/> -->
<property name="hibernate.max_fatch_depth" value="3" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.jdbc.fetch_size" value="18" />
<property name="hibernate.jdbc.batch_size" value="10" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
</properties>
</persistence-unit>
</persistence>

 

最后,在web.

<?<web-app version="2.5" <filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-mvc. </init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>

 

jdbc.properties配置文件内容

driverClass=org.gjt.mm.mysql.Driver
jdbcUrl=jdbc:mysql://xx:3306/xx?useUnicode=true&characterEncoding=UTF-8
user=xx
password=xx