hibernate由spring管理,配置文件如下:
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 配置扫瞄注解service,controller -->
<context:annotation-config/>
<context:component-scan base-package="扫瞄的包名" scoped-proxy="targetClass">
<!-- <context:exclude-filter type="annotation" expression="cn.bonoon.controllers.*"/> -->
</context:component-scan>
<!-- 配置数据库连接 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<!-- 配置hibernate相关信息 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 以下列表写入实体类 -->
<property name="annotatedClasses">
<list>中间写入hibernate注解的实体</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
有了applicationContext再在web.xml在配置一下便可同时使用spring和hibernate了,但是要使用spring mvc ,还得在web里做一点手脚,具体如下:
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- 上下文配置文件 -->
<context-param>
<description>spring config</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<description>spring listerner</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring mvc的相关内容,此处的servlet-name任意,但必须有<your servlet-name>-servlet.xml与之对应 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 配置对应以.do结尾的请求 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 配置过滤器,同时把所有的请求都转为utf-8编码 -->
<filter>
<filter-name>Spring character encoding filter</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>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置找不到页面时返回的页面 -->
<error-page>
<error-code>404</error-code>
<location>/error/404.html</location>
</error-page>
<!-- 配置项目主页 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
如上面配置文件的注释所说,由于配置了spring mvc ,还得有相关的文件,以上面的配置对应的文件名为:
springmvc-servlet.xml
[html] view plaincopy
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 对定义包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="cn.bonoon" />
<!-- 启动spring mvc的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<!-- 配置信息转换,将用@responsebody注解的返回值转换为json返回前台,编码为utf-8-->
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<!-- 上传文件时需要用到的分解器,默认将编码转为utf-8 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="" p:suffix=".jsp" />
</beans>
按照本文所提到的配置文件配置的话,三个文件全部都放在web-inf里面,其中springmvc-servlet.xml里面配置的信息转换用到了两个外加的包
jackson-core-asl-1.4.2.jar
jackson-mapper-asl-1.4.2.jar
分享到:
相关推荐
SpringMVC与Hibernate整合所需jar
SpringMVC和Hibernate整合所需的jar包,基本已经满足了日常所需
基于SpringMvc Hibernate的登录Demo 适合初次接触SpringMvc Hibernate当整合练习 内有数据库创建sql语句 导入项目发布即可直接运行 内有注释及说明
spring+springMVC+Hibernate整合
springmvc spring hibernate整合Demo 包含了CRUD 帮助初学者快速学习springmvc
SpringMVC+Hibernate+Spring整合实例源码下载,很完整的例子,需要的可以下载源码学习
Spring+SpringMVC +Hibernate 整合,直接可以使用。hibernate配置数据库可以任意。
springmvc与hibernate整合,包含spring的事务处理
本Demo利用Springmvc+hibernate搭建的Demo,是一个典型的CRM系统
很全面的jar包,可以直接用于springmvc+hibernate项目开发。
Spring+SpringMVC+Hibernate简单整合,实现增删改查,jdk7 tomcat7以上版本。
springmvc demo,整合了hibernate4作为dao层实现。使用c3p0连接池管理连接。
springMVC+hibernate的简单整合,只做了查询,仅供参考
springMvc整合hibernate Demo
spring + spring MVC + hibernate三大框架整合开发必要的jar包,完整版本。没有缺失。spring + spring MVC + hibernate三大框架整合开发必要的jar包,完整版本。没有缺失。
AngularJs整合spring和springmvc以及hibernate整合案例
SpringMVC+hibernate整合,还有写了一个简单的单元测试类
百度ueditor(jsp)1.4.3最新版,完整的整合了springMVC和hibernate源码,借鉴了他人的源码,做成了一个完整可运行的demo,需自己根据实体类建立数据库。
springMVC&hibernate整合实例
系统采用技术:Spring+SpringMVC+Hibernate+jQuery+Ajax+面向接口编程。 简介:该图书管理项目实现了三种角色的功能,分别是超级管理员,图书管理员和读者。在系统中,实现了图书管理,统计管理,系统管理和系统监控...