前言:在IDEA中,通过相关插件,可以利用Hibernate逆向生成数据表对应的实体类。具体操作及注意事项见本篇随笔。
1.创建一个基于maven的hibernate工程。并在工程中添夹hibernate核心配置文件hibernate.cfg.xml,其工程结构如下图所示。
其中,hibernate.cfg.xml的配置如下:
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 8 <!-- 主要三部分配置--> 9 <!-- 1.配置数据库信息,必须的--> 10 <property name="connection.url">jdbc:mysql://localhost:3306/hibernatestudy?useUnicode=true&characterEncoding=UTF-8</property> 11 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 12 <property name="connection.username">root</property> 13 <property name="connection.password"></property> 14 15 <!-- 2.配置hibernate信息,可选的--> 16 17 <!-- 输出底层的sql语句--> 18 <property name="hibernate.show_sql">true</property> 19 20 <!-- 对底层sql进行格式化--> 21 <property name="hibernate.format_sql">true</property> 22 23 <!--配置数据的方言,如mysql中limit 24 oracle的rownum--> 25 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 26 27 <property name="hibernate.current_session_context_class">thread</property> 28 29 <!-- 3.把映射文件配置到核心文件中,必须的--> 30 31 </session-factory> 32 </hibernate-configuration>