1. 程式人生 > >mysql資料庫方言的選擇問題

mysql資料庫方言的選擇問題

1.問題引導

    在開發專案的時候,我接受到一個用spring+hibernate+springMVC的專案demo,hibernate用的jpa規範,我配置hibernate.hbm2ddl.auto=create,這樣程式本應該一執行就建立表,然而當我,向資料庫中新增資料的時候就報錯:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.userinfo' doesn't exist

說我操作的表不存在。然後,我設定hibernate.show_sql=true,檢視日誌發現有建立資料庫表的語句

Hibernate: 
    drop table if exists userinfo
Hibernate: 
    create table userinfo (
        id integer not null auto_increment unique,
        address varchar(255),
        age integer,
        birthday datetime,
        name varchar(255),
        password varchar(255),
        primary key (id)
    ) type=InnoDB
2017-07-05 21:31:59,052 ERROR

[org.hibernate.tool.hbm2ddl.SchemaExport] - <HHH000389: Unsuccessful: create table userinfo (id integer not null auto_increment unique, address varchar(255), age integer, birthday datetime, name varchar(255), password varchar(255), primary key (id)) type=InnoDB>
2017-07-05 21:31:59,053 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] - <You have an error in your SQL syntax
; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 9>
七月 05, 2017 9:31:59 下午 org.apache.catalina.core.ApplicationContext log

仔細檢視日誌發現,說我們建立資料庫的語句有問題

然後,我把數庫方言改為  hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

2.mysql不同版本對應的方言


MySQL 5.5及以上版本  :  org.hibernate.dialect.MySQL5InnoDBDialect  這個是網上人說的,我安裝的就是mysql 5.5,開始方言用的org.hibernate.dialect.MySQLInnoDBDialect老報錯,改了之後就正常了,實踐證明這是正確的。

MySQL 5.5以下的

org.hibernate.dialect.MySQLInnoDBDialect,這個是推測吧,我沒有試過

org.hibernate.dialect.MySQLDialect 這個方言好像通用的