spring+hibernate整合(基本配置)
2016-12-18 21:35
369 查看
1导包
导入spring和hibernate相关的jar包。
spring的jar包:spring.jar,spring-aop.jar,spring-asm.jar,spring-beans.jar,spring-context.jar,spring-context-support.jar,spring-core.jar,spring-expression.jar,spring-
jdbc.jar,spring-jms.jar,spring-web.jar,spring-tx.jar等
spring配置中如果有事务还需要导入aspectjweaver.jar
hibernate的jar包:hibernate3.jar,hibernate-annotations.jar,hibernate-cglib-repack-2.1_3.jar,hibernate-commons-annotations.jar,hibernate-entitymanager.jar等
一般需要些hql语句,则需要导入antlr-2.7.6.jar,来检查和识别hql的语法
如果使用mysql 还需要导入mysql-connector-java-5.1.22-bin.jar
如果使用oracle则需要导入ojdbc14.jar
如果涉及json的使用则需要导入json-lib-2.4-jdk15.jar
还有一些jar包很有可能需要导入:
commons-beanutils-1.7.0.jar 动态的获取/设值Java Bean的属性
commons-chain-1.1.jar 实现责任链设计模式的Java 类库
commons-codec-1.3.jar 用来处理常用的编码方法的工具类包,例如DES、SHA1、MD5、Base64等等
commons-collections-3.1.jar 对标准java Collection的扩展
commons-collections.jar 对标准java Collection的扩展
commons-digester-1.8.jar 用于处理struts-config.xml配置文件
commons-fileupload-1.1.1.jar struts上传文件
commons-httpclient-3.1.jar 用来简化HTTP客户端与服务器端进行各种通信编程实现
commons-io-1.1.jar 针对java.io.InputStream和Reader进行了扩展
commons-lang-2.4.jar 对java.lang.*的扩展
commons-logging-1.1.1.jar 日志包
commons-pool-1.3.jar 实现对象池化框架
commons-validator-1.3.1.jar 用来把验证规则程序提取出来,以供重复使用
以上jar包概括的不一定全面,可能还需要其他的jar包,如果不知道还需要什么jar包的话,可以启动工程,看console的异常提示,会提示缺少设
什么jar包,小编也是使用这笨方法一步一步加的jar包,希望读者即时指正,以助于相互学习。
2整合
javaweb在开始学习的时候一般是spring配置和hibernate配置分开独立的,但是实际工作中,一般都是整合在一起的。
下面是我整合的配置:
<?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:jee="http://www.springframework.org/schema/jee"
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/context http://www.springframework.org/schema/context/spring-context-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.*" />
<!-- 数据库连接 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" name="dataSource2">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/moffice2</value>
</property>
<property name="
b950
username" value="root" />
<property name="password" value="root" />
</bean>
<!-- sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/matrix/ztree/model/CatalogInfo.hbm.xml</value>
</list>
</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">false</prop>
</props>
</property>
</bean>
<!-- 配置事务 -->
<!-- 生命事务管理,采用aop切入 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务传播性 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置参与事务的类 -->
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="txAdvice" pointcut="within(com.matrix.*)" />
</aop:config>
</beans>
如以上配置spring+hibernate就配置完成了,其中 注解扫描配置后,就可以在mvc模式中使用注解标记;在sessionFactory的bean中配置 xxx.hbm.xml就相当于在java中使用new Configuration("xxx.hbm.xml的路径"),就是我们平时说的javabean.
导入spring和hibernate相关的jar包。
spring的jar包:spring.jar,spring-aop.jar,spring-asm.jar,spring-beans.jar,spring-context.jar,spring-context-support.jar,spring-core.jar,spring-expression.jar,spring-
jdbc.jar,spring-jms.jar,spring-web.jar,spring-tx.jar等
spring配置中如果有事务还需要导入aspectjweaver.jar
hibernate的jar包:hibernate3.jar,hibernate-annotations.jar,hibernate-cglib-repack-2.1_3.jar,hibernate-commons-annotations.jar,hibernate-entitymanager.jar等
一般需要些hql语句,则需要导入antlr-2.7.6.jar,来检查和识别hql的语法
如果使用mysql 还需要导入mysql-connector-java-5.1.22-bin.jar
如果使用oracle则需要导入ojdbc14.jar
如果涉及json的使用则需要导入json-lib-2.4-jdk15.jar
还有一些jar包很有可能需要导入:
commons-beanutils-1.7.0.jar 动态的获取/设值Java Bean的属性
commons-chain-1.1.jar 实现责任链设计模式的Java 类库
commons-codec-1.3.jar 用来处理常用的编码方法的工具类包,例如DES、SHA1、MD5、Base64等等
commons-collections-3.1.jar 对标准java Collection的扩展
commons-collections.jar 对标准java Collection的扩展
commons-digester-1.8.jar 用于处理struts-config.xml配置文件
commons-fileupload-1.1.1.jar struts上传文件
commons-httpclient-3.1.jar 用来简化HTTP客户端与服务器端进行各种通信编程实现
commons-io-1.1.jar 针对java.io.InputStream和Reader进行了扩展
commons-lang-2.4.jar 对java.lang.*的扩展
commons-logging-1.1.1.jar 日志包
commons-pool-1.3.jar 实现对象池化框架
commons-validator-1.3.1.jar 用来把验证规则程序提取出来,以供重复使用
以上jar包概括的不一定全面,可能还需要其他的jar包,如果不知道还需要什么jar包的话,可以启动工程,看console的异常提示,会提示缺少设
什么jar包,小编也是使用这笨方法一步一步加的jar包,希望读者即时指正,以助于相互学习。
2整合
javaweb在开始学习的时候一般是spring配置和hibernate配置分开独立的,但是实际工作中,一般都是整合在一起的。
下面是我整合的配置:
<?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:jee="http://www.springframework.org/schema/jee"
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/context http://www.springframework.org/schema/context/spring-context-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.*" />
<!-- 数据库连接 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" name="dataSource2">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/moffice2</value>
</property>
<property name="
b950
username" value="root" />
<property name="password" value="root" />
</bean>
<!-- sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/matrix/ztree/model/CatalogInfo.hbm.xml</value>
</list>
</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">false</prop>
</props>
</property>
</bean>
<!-- 配置事务 -->
<!-- 生命事务管理,采用aop切入 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务传播性 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置参与事务的类 -->
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="txAdvice" pointcut="within(com.matrix.*)" />
</aop:config>
</beans>
如以上配置spring+hibernate就配置完成了,其中 注解扫描配置后,就可以在mvc模式中使用注解标记;在sessionFactory的bean中配置 xxx.hbm.xml就相当于在java中使用new Configuration("xxx.hbm.xml的路径"),就是我们平时说的javabean.
相关文章推荐
- Struts2 Hibernate Spring 整合的基本流程和步骤及其相关配置细节
- Spring整合Hibernate时,配置事务处理
- SSH学习笔记之关于struts-spring-hibernate整合配置问
- Spring+hibernate整合常用的配置文件
- 最新版的SSH框整合(Spring 3.1.1 + Struts 2.3.1.2 + BoneCP + Hibernate 4.1)更新增加C3P0和EhCache配置
- Spring+Struts+Hibernate框架整合配置
- Struts,hibernate,Spring整合开发各层之间联系的配置说明(初学者值得看下)
- Servlet, Spring, hibernate基本配置
- 配置Struts+Hibernate+Spring基本步骤
- Spring整合Hibernate配置(使用hibernate.cfg.xml文件)
- Spring 整合 Hibernate 的一处简化引入PO映射文件配置
- Struts,Hibernate,Spring整合的重点在于配置文件
- struts+hibernate+spring整合的文件配置详解
- Spring 整合 Hibernate 的一处简化配置
- SSH:SSH(Struts1 + Spring + hibernate)整合之配置文件
- spring整合hibernate的一个配置文件
- Spring+Hibernate如何配置数据源(Struts Spring Hibernate (SSH) 整合实例)
- spring hibernate 整合配置模板
- Spring+Hibernate整合配置
- Spring 联合Hibernate ,Struts开发之基本配置