Hibernate连接数据库的配置
实体类的映射文件
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="po.Emp" table="emp">
<id name="empno" column="empno">
<generator class="native"/>
</id>
<property name="ename" column="ename" />
<property name="salary" column="salary" />
<property name="job" column="job" />
<property name="hiredate" column="hiredate" />
</class>
</hibernate-mapping>
连接数据库的配置文件:
完整的配置:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE hibernate-configuration PUBLIC
3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
5 <hibernate-configuration>
6 <session-factory>
7 <property name="connection.username">root1809</property>
8 <property name="connection.password">root1809</property>
9 <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?serverTimezone=UTC</property>
10 <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
11 <property name="connection.dialect">org.hibernate.dialect.MySQLDialect</property>
12 <property name="show_sql">true</property>
13 <!-- <property name="jdbc.fetch_size">50</property>
14 <property name="jdbc.batch_size">23</property>
15 <property name="hbm2ddl.auto">create</property>
16 <property name="hibernate.connection.pool.size">20</property> -->
17 <mapping resource="empHibernate.xml" />
18 <mapping resource="log4j2.xml"/>
19 </session-factory>
20 </hibernate-configuration>
Driver_class:
![](https://img2018.cnblogs.com/blog/1641302/201903/1641302-20190325135926092-787199890.png)
Dialect: 一定要把.class删掉。
![](https://img2018.cnblogs.com/blog/1641302/201903/1641302-20190325140023057-48011905.png)
show_sql: 显示SQL语句;
必须要添加mapping标签,因为框架读配置文件的时候只会读一个,读配置文件,不会读映射文件,所以需要添加映射属性的文件。
另外还需要导入jar包,并配置log4j2.xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.1</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/2002/xmlspec/dtd/2.10/xmlspec.dtd">
<Configuration status="warn">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
参考文档:
https://www.cnblogs.com/ciaos/p/4119705.html
下面这个大佬的博客居然还有小心心(@ ^_ ^@)
https://www.cnblogs.com/yadongliang/p/5851606.html