一.搭建流程(springMVC+spring+hibernate)
1 spring+orm
1.1 jar包引入
1.2 扫描dao,service,将数据源和sessionFactory放入springIOC容器
1.3 将sessionFactory委托给我们的spring进行事务管理(application-data.xml和db.properties)
2 springMVC整合进来
2.1 springMVC搜需要的包
2.2 编写springMVC的配置文件 扫描controller(spring-servlet.xml)
2.3 在web.xml中设置DispatcherServlet这个servlet
来保证springMVC可以运行工作
3 在web.xml中设置spring的监听器
二.详细搭建和注意事项
配置application-data.xml文件
从配置文件中获取连接数据库的相关信息
<context:property-placeholder location="classpath:db.properties"/>
<!-- 从连接池使用${}获取数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
管理sessionFactory
<!-- 管理sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 获取数据源 -->
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 扫描映射文件 -->
<property name="mappingLocations" value="classpath:com/lanou/entity/*.hbm.xml"></property>
</bean>
事务管理
<!-- 1.创建事务管理对象 将sessionFactory交给他管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- aop注解扫描 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
扫描路径
<context:component-scan base-package="com.lanou">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
配置spring-servlet.xml
<!--只需要扫描controller包下的-->
<context:component-scan base-package="com.lanou.controller"></context:component-scan>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- prefix的value + return的字符串 + suffix的value -->
<!-- /resource/views/index.jsp -->
<property name = "prefix" value="/resource/views/"></property><!--视图的路径 -->
<property name = "suffix" value = ".jsp"></property><!-- 视图的格式 -->
</bean>
<!-- 静态资源的处理 js css img -->
<mvc:resources location="/resource/" mapping="/resource/**"></mvc:resources>
<!-- 启动注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
配置web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application*.xml</param-value>
</context-param>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
注册springMVC来管理
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>