Spring、SpringMVC、Hibernate整合实例如何详细讲解?
- 内容介绍
- 文章标签
- 相关推荐
本文共计678个文字,预计阅读时间需要3分钟。
使用Maven构建项目,通过pom.xml引入相关jar包,并配置如下Spring.xml:
xml
使用Maven构建项目,用pom.xml引入相应jar,配置以下文件
创建spring.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns="www.springframework.org/schema/beans" xmlns:aop="www.springframework.org/schema/aop" xmlns:context="www.springframework.org/schema/context" xmlns:tx="www.springframework.org/schema/tx" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context-4.3.xsd www.springframework.org/schema/aop www.springframework.org/schema/aop/spring-aop-4.3.xsd www.springframework.org/schema/tx www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 整合Hibernate --> <context:property-placeholder location="classpath:db.properties"/> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:hibernate.cfg.xml"/> <property name="mappingLocations" value="classpath:mapping/*.hbm.xml"></property> </bean> <bean name="hibernateTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager"></tx:advice> <aop:config> <aop:pointcut expression="execution(* com.forum.service.*.*(..))" id="pointCut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/> </aop:config> <context:component-scan base-package="com.forum"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
创建springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns="www.springframework.org/schema/beans" xmlns:aop="www.springframework.org/schema/aop" xmlns:context="www.springframework.org/schema/context" xmlns:mvc="www.springframework.org/schema/mvc" xsi:schemaLocation="www.springframework.org/schema/mvc www.springframework.org/schema/mvc/spring-mvc-4.3.xsd www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context-4.3.xsd www.springframework.org/schema/aop www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 配置MessageConvertor --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 设置方言 --> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 配置生成SQL的打印 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 设置自动生成表 --> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.connection.isolation">4</property> </session-factory> </hibernate-configuration>
创建db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名
jdbc.username="" #这里自己填写
jdbc.password="" #这里自己填写
整合SSH的配置文件已经完成
到此这篇关于Spring+SpringMVC+Hibernate整合实例讲解的文章就介绍到这了,更多相关Spring+SpringMVC+Hibernate整合内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
本文共计678个文字,预计阅读时间需要3分钟。
使用Maven构建项目,通过pom.xml引入相关jar包,并配置如下Spring.xml:
xml
使用Maven构建项目,用pom.xml引入相应jar,配置以下文件
创建spring.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns="www.springframework.org/schema/beans" xmlns:aop="www.springframework.org/schema/aop" xmlns:context="www.springframework.org/schema/context" xmlns:tx="www.springframework.org/schema/tx" xsi:schemaLocation="www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context-4.3.xsd www.springframework.org/schema/aop www.springframework.org/schema/aop/spring-aop-4.3.xsd www.springframework.org/schema/tx www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 整合Hibernate --> <context:property-placeholder location="classpath:db.properties"/> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:hibernate.cfg.xml"/> <property name="mappingLocations" value="classpath:mapping/*.hbm.xml"></property> </bean> <bean name="hibernateTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager"></tx:advice> <aop:config> <aop:pointcut expression="execution(* com.forum.service.*.*(..))" id="pointCut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/> </aop:config> <context:component-scan base-package="com.forum"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
创建springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xmlns="www.springframework.org/schema/beans" xmlns:aop="www.springframework.org/schema/aop" xmlns:context="www.springframework.org/schema/context" xmlns:mvc="www.springframework.org/schema/mvc" xsi:schemaLocation="www.springframework.org/schema/mvc www.springframework.org/schema/mvc/spring-mvc-4.3.xsd www.springframework.org/schema/beans www.springframework.org/schema/beans/spring-beans.xsd www.springframework.org/schema/context www.springframework.org/schema/context/spring-context-4.3.xsd www.springframework.org/schema/aop www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 配置MessageConvertor --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 设置方言 --> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 配置生成SQL的打印 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 设置自动生成表 --> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.connection.isolation">4</property> </session-factory> </hibernate-configuration>
创建db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名
jdbc.username="" #这里自己填写
jdbc.password="" #这里自己填写
整合SSH的配置文件已经完成
到此这篇关于Spring+SpringMVC+Hibernate整合实例讲解的文章就介绍到这了,更多相关Spring+SpringMVC+Hibernate整合内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

