的文件,没有进行说明。
1 Hibernate.cfg.xml 的内容
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testdb</property>
<property name="hibernate.connection.username">mysql</property>
<property name="hibernate.connection.password">mysql_2016</property>
<property name="hibernate.connection.pool_size">5</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="connection.useSSL">false</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
</session-factory>
</hibernate-configuration>
2 Hibernate.cfg.xml 属性解释
从名字,可以清楚的知道这是一个Hibernate配置文件。
这个配置文件中,主要是设置session-factory的属性。
hibernate.dialect是方言, 主要用于描述数据库数据类型和JAVA数据类型之间的映射关系
hibernate.connection.driver_class是驱动,定义通过何种驱动连接数据库。由于我们需要连接的是MySQL数据库,所以使用mySQL的驱动
这也是在Hibernate 连接本地MySQL 服务器 加入“ compile 'mysql:mysql-connector-java:6.0.4' ” 的原因。
hibernate.connection.url, 指定数据库的地址。 //localhost:3306/是指本机MySQL服务器的监听地址,testdb是数据库名称。
hibernate.connection.username,登录时的用户名。
hibernate.connection.password,登陆密码。
hibernate.connection.pool_size,最大连接数。
hibernate.hbm2ddl.auto,自动创建表。
以上属性配置正确,就能通过Hibernate 连接本地MySQL 服务器 第5步中的代码 生成session-factory。
添加Entity类到配置文件
<mapping class="Donation" /> Donation是类名称,需要添加全路径。