【问题标题】:What maven artifacts do I need for spring hibernate and mysql support?spring hibernate 和 mysql 支持需要哪些 maven 工件?
【发布时间】:2011-03-17 05:52:38
【问题描述】:

我有一个使用 maven2 的 IDEA 项目。

我要使用hibernate + mysql,需要什么依赖?

【问题讨论】:

    标签: java hibernate spring maven-2


    【解决方案1】:

    首先,我将版本与工件分开:

    <properties>
        <spring.version>3.0.3.RELEASE</spring.version>
        <hibernate.version>3.5.3-Final</hibernate.version>
        <mysql.version>5.1.13</mysql.version>
        <junit.version>4.7</junit.version>
    </properties>
    

    然后我这样引用它们:

    <dependencies>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
            <!-- perhaps using scope = provided, as this will often
             be present on the app server -->
        </dependency>
    
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <!-- or hibernate-entitymanager if you use jpa -->
            <version>${hibernate.version}</version>
        </dependency>
    
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    
    </dependencies>
    

    这样您就可以将所有版本保存在一个地方,并且可以轻松更新它们,尤其是在您引用例如多个弹簧工件。

    顺便说一句:这些应该是当前版本,但您始终可以使用 MvnRepository.com 查找当前版本

    【讨论】:

      【解决方案2】:

      在&lt;depdendencies&gt; 之后将这些依赖项粘贴到pom.xml 应该可以:

      <!-- MySQL database driver -->
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.9</version>
      </dependency>
      
      <!-- Hibernate framework -->
      <dependency>
          <groupId>hibernate</groupId>
          <artifactId>hibernate3</artifactId>
          <version>3.2.3.GA</version>
      </dependency>
      
      
      <!-- Hibernate library dependecy start -->
      <dependency>
          <groupId>dom4j</groupId>
          <artifactId>dom4j</artifactId>
          <version>1.6.1</version>
      </dependency>
      
      <dependency>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
          <version>1.1.1</version>
      </dependency>
      
      <dependency>
          <groupId>commons-collections</groupId>
          <artifactId>commons-collections</artifactId>
          <version>3.2.1</version>
      </dependency>
      
      <dependency>
          <groupId>cglib</groupId>
          <artifactId>cglib</artifactId>
          <version>2.2</version>
      </dependency>
      
      <dependency>
          <groupId>javax.transaction</groupId>
          <artifactId>jta</artifactId>
          <version>1.1</version>
      </dependency>
      <!-- Hibernate library dependecy end -->
      

      无耻地从http://www.mkyong.com/hibernate/quick-start-maven-hibernate-mysql-example/克隆(根据评论者的推荐添加了jta)

      您可能需要调整依赖项的版本号。

      IntelliJ IDEA 9 可以根据类名查找 Maven 依赖项。如果您开始使用当前依赖项中不可用的类,您可以让 IntelliJ 使用 Alt-Enter 帮助找到它。

      我在工作中构建了一个基于 Java 的 Subversion 挂钩实现,效果非常好。通过这种方式,我能够相当轻松地将 SVNKit 和 Google Guice 依赖项添加到我的项目中。

      在您的情况下,MySQL 可能更棘手,因为在使用 Hibernate 时它更多是运行时依赖。

      【讨论】:

      • spring 依赖项不存在 - blog.springsource.com/2009/12/02/…
      • 没错。我读了你的问题,但不是它的标题。需要 Spring 的哪些功能? Spring 中有很多组件,您可能不需要全部。
      • 这些依赖确实过时了,不推荐使用(Hibernate使用SLF4J有一段时间了)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-05
      • 2016-04-06
      • 2014-12-13
      相关资源
      最近更新 更多