Spring+Springmvc+Hibernate整合开发
1、导包
spring+springmvc(在spring基本上多了web和mvc包)+hibernate基本包
2、创建Spring的配置文件
在src下新建一个名为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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
3、在Web环境的Spring启动配置
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
4、在Web中配置SpringMVC入口(DispatcherServlet)
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/example/*</url-pattern>
</servlet-mapping>
5、新建springmvc配置文件
在src下新建一个springmvc.xml文件,该名和上一步中,init-param中的xml文件名一致。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
</beans>
6、整合Hibernate
(1)导入spring整合Hibernate包
(2)创建Hibernate核心配置文件
如果已经有数据表,属性设置hbm2ddl不要了。
添加属性:
<property name="javax.persistence.validation.mode">none</property>
(3)spring接管数据库配置
使用c3p0数据源
在Spring中配置数据源
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="jdbc.properties"/>
创建jdbc.properties的属性文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\:///examsoft
jdbc.username=root
jdbc.passwrod=qq123456
(4)让Spring接管SessionFactory和常用属性配置
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="mappingResources">
<list>
<value>product.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
</value>
</property>
</bean>
7、杂项配置
Spring视图解析器
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
注解扫描
xmlns:context=http://www.springframework.org/schema/context
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/springcontext.xsd">
静态资源过滤
<mvc:resources location="/WEB-INF/img/" mapping="/img/**"></mvc:resources>
配置HibernateTemplate
<!-- 配置HibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="mySessionFactory"></property></bean>
到这里一个完整的ssm框架就搭建完成了!!祝大家工作顺利学习愉快!