Spring SpringMVC Hibernate整合时的一些问题
来源:互联网 发布:结婚幻灯片制作软件 编辑:程序博客网 时间:2024/05/21 18:31
目录结构为:
(1)springMVC + spring: exclude,include
SpringMVC 专注于Controller,其他的放到Spring中
SpringMVC配置为:
<context:component-scan base-package="com.controller"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/></context:component-scan>
<context:component-scan base-package="com"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>
(2)c3p0数据源和普通的jdbc 配置中的name是有区别的,还是不一样的
(3)Hibernate注解方式与spring整合时可能会报找不到hibernate.cfg.xml的错误,这时在归Hibernate管理的DAO中获取Session的方式要做一些修改,因为交给Spring管理了,所以更改如下:
@Repositorypublic class PersonDao {@Autowired SessionFactory sessionFactory;public Session getSession(){return sessionFactory.openSession();// return sessionFactory.getCurrentSession();}}
Spring的ApplicationContext配置文件相应的定义SessionFactory的部分为:
<!-- 配置Hibernate的相关的属性 --><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><prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop></props></property><property name="packagesToScan"><list><value>com.entity</value></list></property></bean>
最后,贴上ApplicationContext.xml和springmvc-servlet.xml,Web.xml的全部配置信息:
Web.xml:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 加载applicationContext.xml --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 运行的时候才会访问DispatcherServlet,如果想改变这种状态,就用load-on-startup --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app>
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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 引入外部的属性文件 --><context:property-placeholder location="classpath:jdbc.properties"/><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${jdbc.driverClass}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><context:component-scan base-package="com"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 配置业务层类 --><bean id="personService" class="com.service.PersonService"><property name="personDao" ref="personDao"></property></bean><!-- 配置DAO的类,直接注入连接池,可自动创建jdbc模板 --><bean id="personDao" class="com.dao.PersonDao"></bean><!-- 配置Hibernate的相关的属性 --><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><prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop></props></property><property name="packagesToScan"><list><value>com.entity</value></list></property></bean><!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 开启注解事务 --><tx:annotation-driven transaction-manager="transactionManager"/></beans>
springmvc-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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 会自动加载一些验证类 --> <mvc:annotation-driven/> <!-- 扫描包下的所有注解类 : 哪个可以处理我的请求?--> <context:component-scan base-package="com.controller"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/></context:component-scan><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"p:viewClass="org.springframework.web.servlet.view.JstlView"p:prefix="/WEB-INF/view/"p:suffix=".jsp"/><!-- <bean id="jsonConvert" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>--></beans>
因为没有hibernate.cfg.xml,所以要在Entity中配置相应的注解:
package com.entity;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;/* * 这里用Hibernate的注解方式 */@Entity@Table(name="person")public class Person {String id;String name;String idCard;String phone;String address;@Id@Column(name="id", nullable=false,length=32,unique=true)@GenericGenerator(name="generator",strategy="uuid.hex")@GeneratedValue(generator="generator")public String getId() {return id;}public void setId(String id) {this.id = id;}@Column(name="name", nullable=false,length=32)public String getName() {return name;}public void setName(String name) {this.name = name;}@Column(name="idCard", nullable=false,length=32)public String getIdCard() {return idCard;}public void setIdCard(String idCard) {this.idCard = idCard;}@Column(name="phone", nullable=false,length=32)public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}@Column(name="address", nullable=false,length=32)public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public Person(String id, String name, String idCard, String phone,String address) {super();this.id = id;this.name = name;this.idCard = idCard;this.phone = phone;this.address = address;}public Person() {super();}}
1 0
- Spring SpringMVC Hibernate整合时的一些问题
- spring+springmvc+hibernate 整合出现的问题
- Spring+SpringMvc+Hibernate整合问题
- 关于spring+hibernate整合的一些问题
- SpringMVC+Hibernate+Spring的整合
- spring、springmvc、hibernate的整合
- SpringMVC+Spring+Hibernate整合
- SpringMVC整合Spring、Hibernate
- Spring-SpringMVC-Hibernate整合
- SpringMVC+Spring+Hibernate整合
- SpringMVC+Spring+hibernate整合
- springmvc-spring-hibernate整合
- Spring+SpringMVC+Hibernate整合
- spring+springMVC+hibernate整合
- SpringMVC+Hibernate+Spring整合
- spring+springmvc+hibernate整合
- flex 整合spring,hibernate遇到的一些问题
- Spring 与hibernate整合过程中出现的一些问题
- c++ Friend
- HTML与ppt的对比 网页状态描述
- Android Studio中Git的配置及协同开发
- 如何提高 Java 中锁的性能
- GC调优在Spark应用中的实践
- Spring SpringMVC Hibernate整合时的一些问题
- npm should be run outside of the node repl, in your normal shell
- NOIP2001 提高组 复赛 一元三次方程求解
- SpringBoot集成ActiveMQ
- jQuery中$(function(){})与(function($){})(jQuery)、$(document).ready(function(){})等的区别详细讲解
- POJ2488 A Knight's Journey(深搜DFS,字典序,骑士游历问题)
- 抓包工具Fiddler的使用教程(七):打断点修改Response
- Solr6.3.0部署tomcat
- 微信小程序css篇----所有属性(按字母排列:c,d,e开头)