本文地址:1.框架搭建 - 荣慕平 - 博客园 (cnblogs.com)
一、新建mvn项目。
mvn依赖:
1 <dependencies> 2 <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> 3 <dependency> 4 <groupId>org.hibernate</groupId> 5 <artifactId>hibernate-core</artifactId> 6 <version>5.6.7.Final</version> 7 </dependency> 8 <!-- https://mvnrepository.com/artifact/junit/junit --> 9 <dependency> 10 <groupId>junit</groupId> 11 <artifactId>junit</artifactId> 12 <version>4.13.2</version> 13 <scope>test</scope> 14 </dependency> 15 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 16 <dependency> 17 <groupId>mysql</groupId> 18 <artifactId>mysql-connector-java</artifactId> 19 <version>8.0.28</version> 20 </dependency> 21 <dependency> 22 <groupId>org.junit.jupiter</groupId> 23 <artifactId>junit-jupiter</artifactId> 24 <version>RELEASE</version> 25 <scope>compile</scope> 26 </dependency> 27 <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 --> 28 <dependency> 29 <groupId>org.hibernate</groupId> 30 <artifactId>hibernate-c3p0</artifactId> 31 <version>5.6.7.Final</version> 32 </dependency> 33 34 </dependencies>
二、Program Setting -- Modules配置
点击加号添加核心配置文件
demo:
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 <property name="connection.url">jdbc:mysql://localhost:3306/678</property> 8 <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> 9 <property name="connection.username">root</property> 10 <property name="connection.password">678678</property> 11 <!-- <property name="connection.username"/> --> 12 <!-- <property name="connection.password"/> --> 13 14 <!-- DB schema will be updated if needed --> 15 <!-- <property name="hibernate.hbm2ddl.auto">update</property> --> 16 <!-- hibernate配置 --> 17 <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property> 18 <property name="hibernate.show_sql">true</property> 19 <property name="hibernate.format_sql">true</property> 20 <!-- 表结构变化时执行修改表命令 不删表重建 --> 21 <property name="hibernate.hbm2ddl.auto">update</property> 22 <!-- 设置每次查询从数据库中读取的条数 MySQL数据库不支持(所以企业级涉及大数据量数据的存储基本采用Oracle数据库) --> 23 <property name="hibernate.jdbc.fetch_size">100</property> 24 <!-- 设置批量删除和批量更新 --> 25 <property name="hibernate.jdbc.batch_size">30</property> 26 27 <!-- c3p0属性配置 --> 28 <!-- 配置启用c3p0连接池 --> 29 <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property> 30 <property name="hibernate.c3p0.max_size">500</property> 31 <property name="hibernate.c3p0.min_size">1</property> 32 <!-- 设置缓存里最多有多少个statement对象 --> 33 <property name="hibernate.c3p0.max_statements">100</property> 34 <!-- 超时时间 --> 35 <property name="hibernate.c3p0.timeout">5000</property> 36 <!-- 配置每隔多少秒 扫描连接池所有连接,清理超时连接 --> 37 <property name="hibernate.c3p0.idle_test_period">2000</property> 38 <!-- 配置当连接池连接数耗尽时,每次向MySQL申请多少条连接 --> 39 <property name="hibernate.c3p0.acquire_increment">100</property> 43 44 </session-factory> 45 </hibernate-configuration>
三、配置Database
四、使用idea生成Entity及hbm.xml
本文来自博客园,作者:荣慕平,转载请注明原文链接:https://www.cnblogs.com/rongmuping/articles/rongmuping_hibernate5_1.html