spring+hibernate 两种整合方式配置文件的方法

网友投稿 244 2023-05-26


spring+hibernate 两种整合方式配置文件的方法

之前的文章都是讲解springmvc+spring+mybatis 的整合,而很少有springmvc+spring+hibernate 因为工作的需要,最近在使用hibernate 所以下面我们来看看 spring整合hibernate的配置文件,这里只说spring+hibernate 的配置文件而不说springmvc 因为这些是不用变的。

spring整合hibernate 有两种方式 1、注解方式 2、xml方式实现

1、注解方式实现:

applicationContext.xml配置文件:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

classpath:jdbc.properties

class="com.mchange.v2.c3p0.ComboPooledDataSource">

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

com.test.bean

${dialect}

${show_sql}

${format_sql}

${use_sql_comments}

${hbm2ddl.auto}

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

classpath:jdbc.properties

class="com.mchange.v2.c3p0.ComboPooledDataSource">

class="com.mchange.v2.c3p0.ComboPooledDataSource">

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

com.test.bean

${dialect}

${show_sql}

${format_sql}

${use_sql_comments}

${hbm2ddl.auto}

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

com.test.bean

${dialect}

${show_sql}

${format_sql}

${use_sql_comments}

${hbm2ddl.auto}

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

2.xml方式实现

applicationContext.xml配置:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

/com/cdzg/spring/bean/User.hbm.xml

${dialect}

${hbm2ddl.auto}

${show_sql}

${format_sql}

${use_sql_comments}

&ltSvitp;/tx:attributes>

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd">

/com/cdzg/spring/bean/User.hbm.xml

${dialect}

${hbm2ddl.auto}

${show_sql}

${format_sql}

${use_sql_comments}

&ltSvitp;/tx:attributes>

两种配置最大的区别就是注解方式不用在写O/R映射配置文件而xml方式Svitp实现的要配置O/R映射配置文件

注解的这种方式,直接扫描bean包就可以,剩下的对应关系由框架完成

而xml配置方式要配置O/R 映射文件并在这里指定文件,如果多的话可以使用通配符 "*"


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring中BeanFactory解析bean详解
下一篇:java web过滤器处理乱码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~