hibernate hbm2ddl.import_files 中文亂碼問題

來源:互聯網
上載者:User

標籤:hibernate hbm2ddl.import_files 中文亂碼

hibernate提供如下配置,支援在建立資料庫時,執行import檔案初始化資料庫:

<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop><prop key="hibernate.hbm2ddl.import_files">init_sql.sql</prop>

在init_sql.sql檔案中,寫了這樣一條語句:

insert into sys_menu(id, href, icon, is_activiti, is_show, name, parent_ids, permission, sort, parent_id) values(‘1‘,‘/hello‘,‘‘,‘0‘,‘1‘,‘歡迎頁面‘,‘‘,‘‘,1,‘1‘);

在啟動時,初始化報錯,看到其中的sql語句中文亂碼:

org.hibernate.tool.hbm2ddl.ImportScriptException: Error during statement execution (file: ‘init_sql.sql‘): insert into sys_menu(id, href, icon, is_activiti, is_show, name, parent_ids, permission, sort, parent_id) values(‘1‘,‘/hello‘,‘‘,‘0‘,‘1‘,‘嬈㈣繋欏?,‘‘,‘‘,1,‘1‘)

init_sql.sql檔案是utf8編碼,與資料庫編碼一致。配置hibernate編碼格式如下:

<prop key="hibernate.connection.useUnicode">true</prop><prop key="hibernate.connection.characterEncoding">UTF-8</prop><prop key="hibernate.connection.charSet">UTF-8</prop>

問題依舊,於是開始找解決辦法,找到了這裡,

http://javacimrman.blogspot.ru/2011/07/hibernate-importsql-encoding-when.html

說的是maven工程如何解決這個問題。而我的工程是用tomcat啟動的,類推,啟動時應配置-Dfile.encoding=UTF8,步驟如下:

按右鍵工程-->debug as/run as-->debug configuration/run configuration-->Arguments-->VM Arguments中加入-Dfile.encoding=UTF8

啟動即可。

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/53/8A/wKiom1RqtFzy5sXiAAR8idZInMY969.jpg" title="2014-11-18_105207.png" alt="wKiom1RqtFzy5sXiAAR8idZInMY969.jpg" />


但是現在console的輸出成了這樣:

鍗佷竴鏈?18, 2014 10:40:27 涓婂崍 org.apache.catalina.core.ApplicationContext log淇℃伅: Initializing Spring FrameworkServlet ‘spring‘鍗佷竴鏈?18, 2014 10:40:29 涓婂崍 org.apache.coyote.http11.Http11Protocol start淇℃伅: Starting Coyote HTTP/1.1 on http-8080鍗佷竴鏈?18, 2014 10:40:29 涓婂崍 org.apache.jk.common.ChannelSocket init淇℃伅: JK: ajp13 listening on /0.0.0.0:8009鍗佷竴鏈?18, 2014 10:40:29 涓婂崍 org.apache.jk.server.JkMain start淇℃伅: Jk running ID=0 time=0/32  config=null鍗佷竴鏈?18, 2014 10:40:29 涓婂崍 org.apache.catalina.startup.Catalina start淇℃伅: Server startup in 12756 ms


所以現在應該修改一下console的編碼(如):

 1.Run -> Run configurations...

    2.在Java Application中選中你的引用程式。

    3.選中Common標籤頁。

    4.在Console encoding項中選擇Other,然後選中UTF-8。

    5.點擊Apply按鈕。

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/53/8A/wKiom1Rqs4_Ag_gOAAQst-vGrtA333.jpg" title="2014-11-18_104847.png" alt="wKiom1Rqs4_Ag_gOAAQst-vGrtA333.jpg" />

另外,這裡關於hibernate的配置不影響這裡的問題,去掉後應用上述方法依然可以work。

<prop key="hibernate.connection.useUnicode">true</prop><prop key="hibernate.connection.characterEncoding">UTF-8</prop><prop key="hibernate.connection.charSet">UTF-8</prop>


hibernate hbm2ddl.import_files 中文亂碼問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.