1、框架搭建
(1)檢查plugins是否安裝:file->settings->plugins,搜索“Hibernate”(有support ,還有bundled,都可以)
(2)新建Hibernate+javaweb工程:new->project->project/module,選中本地jdk,選中java、Hibernate,且選中Download選項,Configurate...選中Module Library,確定
(3)創建Database,導入mysql驅動:在第一次導入的時候會提示從maven庫中下載(Driver files are not downloaded),(配置後就不需要再配置了),會提示自己設置代理下載,(Set up HTTP proxy settings),則將自己本機地址IP設置爲代理地址,從maven中下載:
(4)創建Database,注意一定要選擇時區
(5)生成持久化類:
OK之後會自動生成entity實體類以及相應的配置文件,且hibernate.cfg.xml會更新,但是hibernate.cfg.xml中可能報錯:
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
此時要手動導入mysql-connector-java-8.0.15.jar;
也有可能時區會報錯,使用JDBC連接MySql時出現:The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration
(這句話出自https://blog.csdn.net/aayygg1234/article/details/78205240)則必須在配置文件中寫入時區,即
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate?serverTimezone=UTC</property>
且一定要設置用戶名和密碼,否則會報錯,連接失敗。
(6)創建測試類,測試,可能會出現“idea 啓動時報 error:java 無效的源發行版11”,修改關於jdk的配置: