在哪里可以找到使用注释将 hibernate 与 Spring MVC 3.0 集成的好教程?

发布于 2024-10-02 15:18:50 字数 1539 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈甜 2024-10-09 15:18:50

这些库取决于您可能想要实现的功能。

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate</artifactId>
        <version>3.2.7.ga</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.3.0.ga</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.3.0.ga</version>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>8.3-603.jdbc3</version>
    </dependency>
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1</version>
    </dependency>

如果您使用 Maven,这些应该会给您一个良好的开始。
c3p0 用于 postgres 连接上的连接池。

我强烈建议采用 hibernate 注释方法(正如您通过 maven 添加的注释包所看到的那样)。
因此,要使用 postgresql db + hibernate,请将以下内容添加到您的 applicationContext:

<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/jdbc.properties" />

<!-- Uses mchange connection pooling -->
<bean id="dataSource"
    class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
    p:driverClass="${jdbc.driverClassName}"
    p:jdbcUrl="${jdbc.url}"
    p:user="${jdbc.username}"
    p:password="${jdbc.password}" />

<bean id="hibernateProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.c3p0.minPoolSize">5</prop>
            <prop key="hibernate.c3p0.maxPoolSize">20</prop>
            <prop key="hibernate.c3p0.idleTestPeriod">300</prop>
            <prop key="hibernate.c3p0.timeout">600</prop>
            <prop key="hibernate.c3p0.max_statement">50</prop>
            <prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
            <prop key="hibernate.c3p0.preferredTestQuery">select 1;</prop>
        </props>
    </property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
      p:dataSource-ref="dataSource"
      p:packagesToScan="package.with.your.domain.objects.in.it"
      p:hibernateProperties-ref="hibernateProps" />

您在这里基本上所做的是使用 jdbc.properties 文件配置 jdbc 连接(您需要制作一个 - 如果您需要帮助,请给我最重要的是jdbc驱动程序,即org.postgresql.Driver)。然后,您将这些属性连接到 c3p0 连接池中,并将其传递给 AnnotationSessionfactory。

要在应用程序中使用此 sessionFactory,您可以在 Dao 类中扩展 HibernateDaoSupport(HibernateDaoSupport 随 Spring 一起提供,基本上为您进行事务管理),然后在 sessionFactory 参数上注入 sessionFactory。但是,建议不要再使用 HibernateDaoSupport,而是使用 @Repository Annotation - 在 google 上检查一下。

我希望这能给你一些开始的东西。您可以随时通过谷歌搜索更多教程,例如 http://www.vaannila .com/hibernate/hibernate-example/hibernate-annotations-1.html

干杯

The libraries depend on the functionalities you may want to implement.

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate</artifactId>
        <version>3.2.7.ga</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.3.0.ga</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.3.0.ga</version>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>8.3-603.jdbc3</version>
    </dependency>
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1</version>
    </dependency>

If you're using maven, these should give you a good start to work with.
c3p0 is for connection pooling on your postgres connection.

I'd strongly advise to go for the hibernate annotation approach (as you can see by the annotation packages added via maven).
So to make use of your postgresql db + hibernate, add the following to your applicationContext:

<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/jdbc.properties" />

<!-- Uses mchange connection pooling -->
<bean id="dataSource"
    class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
    p:driverClass="${jdbc.driverClassName}"
    p:jdbcUrl="${jdbc.url}"
    p:user="${jdbc.username}"
    p:password="${jdbc.password}" />

<bean id="hibernateProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.c3p0.minPoolSize">5</prop>
            <prop key="hibernate.c3p0.maxPoolSize">20</prop>
            <prop key="hibernate.c3p0.idleTestPeriod">300</prop>
            <prop key="hibernate.c3p0.timeout">600</prop>
            <prop key="hibernate.c3p0.max_statement">50</prop>
            <prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
            <prop key="hibernate.c3p0.preferredTestQuery">select 1;</prop>
        </props>
    </property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
      p:dataSource-ref="dataSource"
      p:packagesToScan="package.with.your.domain.objects.in.it"
      p:hibernateProperties-ref="hibernateProps" />

What you basically did here was to configure the jdbc connection using a jdbc.properties file (you need to make one - if you need help with it, gimme a comment. Most important thing is the jdbc driver, which is org.postgresql.Driver). Then you wired these properties into a c3p0 connection pool and pass this to a AnnotationSessionfactory.

To use this sessionfactory in your application, you could extend the HibernateDaoSupport in your Dao class (the HibernateDaoSupport comes with Spring and basically does the transaction management for you) and there inject the sessionfactory on the sessionFactory parameter. However, it is advised to not use the HibernateDaoSupport any longer, but instead go for the @Repository Annotation - check that on google.

I hope this gives you something to start with. You could always google for further tutorials like http://www.vaannila.com/hibernate/hibernate-example/hibernate-annotations-1.html

cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文