我在spring / hibernate中使用了哪些mysql驱动程序? [英] What mysql driver do I use with spring/hibernate?

查看:83
本文介绍了我在spring / hibernate中使用了哪些mysql驱动程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有点困惑,是'driverclassname'和'hibernate.dialect'都是指mysql驱动程序?



我该用什么? connectorJ是我应该使用的吗?

 < bean id =dataSourceclass =org.apache.commons。 dbcp.BasicDataSourcedestroy-method =close> 
< property name =driverClassNamevalue =org.hsqldb.jdbcDriver/>
< property name =urlvalue =jdbc:mysql:// localhost / blah/>



<值>
hibernate.dialect = org.hibernate.dialect.MySQLDialect

我使用Maven如果我可以从maven获得这个理想的驱动程序。



在tomcat中运行我的应用程序我收到错误:

 无法为连接URL创建类'org.hsqldb.jdbcDriver'的JDBC驱动程序

小小的困惑,是'driverclassname'和'hibernate.dialect'都是指mysql驱动程序吗?

$ b $ b

不,他们不是。 driverclassname 指的是驱动程序类名称,它是实现 java的给定JDBC驱动程序的类.sql.Driver 。当使用MySQL的JDBC驱动程序又名MySQL Connector / J时,这个类是 com.mysql.jdbc.Driver ,如MySQL Connector / J文档中所述:


20.3.4.1。驱动程序/数据源类名,连接器/ J的URL语法和配置属性



实现
java.sql.Driver 在MySQL Connector / J
中是 com.mysql.jdbc.Driver 。 (...)


实际上,他们甚至提供了指令来在Spring中使用它们的驱动程序。请参阅 20.3.5.2.4。



hibernate.dialect 是不同的,这个配置属性是用于定义Hibernate的类名 org.hibernate.dialect.Dialect ,它允许Hibernate生成针对特定关系数据库优化的SQL 。这再次在Hibernate文档中解释: 3.5 / reference / en / html / session-configuration.html#configuration-optionalrel =nofollow noreferrer> 3.4。可选配置属性

(...)Hibernate的类名
org.hibernate.dialect.Dialect 其中
允许Hibernate生成针对特定关系
数据库优化的SQL



eg full.classname.of.Dialect



在大多数情况下,Hibernate实际上是
be能够根据JDBC驱动程序返回的JDBC
元数据选择正确的
org.hibernate.dialect.Dialect
实现。 / p>

对于MySQL 5.x,您应该使用 org.hibernate.dialect.MySQL5InnoDBDialect 如果你使用的是InnoDB表(这将是我的建议),或者如果你不是,那么 org.hibernate.dialect.MySQL5Dialect 。请参阅 3.4 0.1。 SQL Dialects (非详尽)列表。



最后一点,您甚至没有在您的问题中提及的Maven部分... MySQL JDBC驱动程序在Maven中央存储库中可用,您应该使用存储库搜索引擎(如我已经建议)。例如,以下查询:

http://www.jarvana.com/jarvana/search?search_type=project&project=mysql



可以找到最终版本的maven坐标点击两次:

 <依赖关系> 
< groupId> mysql< / groupId>
< artifactId> mysql-connector-java< / artifactId>
< version> 5.1.13< / version>
< /依赖关系>

PS:我不是故意粗鲁,我很乐意帮忙,但你应该真的尝试利用您使用的产品或框架的文档。你在这个问题上提出的问题有很好的文档记录(如我所示),并且很容易找到。在我看来,学习自己发现基本信息是软件开发人员的基本技能。

Little confused, is 'driverclassname' and 'hibernate.dialect' both referring to the mysql driver?

What should I be using? Is the connectorJ the one I should use?

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
        <property name="url" value="jdbc:mysql://localhost/blah"/>



<property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect

I'm using Maven so if I can get the driver from maven that would be ideal.

Running my app in tomcat I get the error:

Cannot create JDBC driver of class 'org.hsqldb.jdbcDriver' for connect URL 

解决方案

Little confused, is 'driverclassname' and 'hibernate.dialect' both referring to the mysql driver?

No, they are not. The driverclassname is referring to, well, the driver class name which is the class from a given JDBC driver that implements java.sql.Driver. The driver class name is driver specific.

When using MySQL's JDBC driver aka MySQL Connector/J, this class is com.mysql.jdbc.Driver as explained in the MySQL Connector/J documentation:

20.3.4.1. Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J

The name of the class that implements java.sql.Driver in MySQL Connector/J is com.mysql.jdbc.Driver. (...)

And actually, they even provide instructions to use their driver with Spring. See the section 20.3.5.2.4. Using Connector/J with Spring.

The hibernate.dialect is different, this configuration property is used to define the classname of a Hibernate org.hibernate.dialect.Dialect which allows Hibernate to generate SQL optimized for a particular relational database. Again this is explained in the Hibernate documentation:

3.4. Optional configuration properties

(...) The classname of a Hibernate org.hibernate.dialect.Dialect which allows Hibernate to generate SQL optimized for a particular relational database.

e.g. full.classname.of.Dialect

In most cases Hibernate will actually be able to choose the correct org.hibernate.dialect.Dialect implementation based on the JDBC metadata returned by the JDBC driver.

For MySQL 5.x, you should use org.hibernate.dialect.MySQL5InnoDBDialect if you are using InnoDB tables (this would be my recommendation) or org.hibernate.dialect.MySQL5Dialect if you're not. See the section 3.4.1. SQL Dialects for a (non exhaustive) list.

Last point, the Maven part that you didn't even mention in your question... The MySQL JDBC driver is available in the Maven central repository and you should use a repository search engine (as I already suggested). For example, the following query:

http://www.jarvana.com/jarvana/search?search_type=project&project=mysql

allows to find the maven coordinates of the ultimate version in two clicks:

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.13</version>
</dependency>

PS: I don't mean to be rude and I'm glad to help but you should really try to leverage the documentation of the products or frameworks you're using. What you're asking in this question is well documented (as I showed) and can be found easily. Learning to find basic information by yourself is a fundamental skill for a software developer in my opinion.

这篇关于我在spring / hibernate中使用了哪些mysql驱动程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆