Hibernate Learning XML setting explained in detail:

  1. Maven Project Pom.xml setting

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>

<build>
<finalName>JavaEE_HibernateDay2</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

The maven setting is set to allow the software to import external jars, some of which  are for dependencies, others for plugins.

 

2. hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD 3.0//EN”
“http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd”>

<hibernate-configuration>
<session-factory >
<!– 显示sql语句的配置 –>
<property name=”hibernate.show_sql”>true</property>
<property name=”hibernate.connection.driver_class”>com.mysql.jdbc.Driver</property>
<property name=”hibernate.connection.url”>jdbc:mysql:///test</property>
<property name=”hibernate.connection.username”>root</property>
<property name=”hibernate.connection.password”>123</property>
<property name=”hibernate.dialect”>org.hibernate.dialect.MySQL57Dialect</property>
<!– 更新方式配置 一般不要这么配 否则可能会乱表 –>
<property name=”hibernate.hbm2ddl.auto”>update</property>
<!– 加载映射文件 –>
<mapping resource=”edu/miis/bean/UserInfo.hbm.xml”/>

</session-factory>
</hibernate-configuration>

The first several lines contained in tag “Doctype” is to allow Eclipse IDE to find proper references so that you don’t end up hard coding everything.

tips: the “mapping resource” tag is added after the mapping xml is set to proper shape.

3. In specific cases where the Connection Pool is set to use “druid”, the hibernate.cfg.xml shall be set to the followings:

<property name=”hibernate.show_sql”>true</property>
<property name=”driverClassName”>com.mysql.jdbc.Driver</property>
<property name=”url”>jdbc:mysql:///test</property>
<property name=”username”>root</property>
<property name=”password”>123</property>
<property name=”hibernate.connection.provider_class”>com.alibaba.druid.support.hibernate.DruidConnectionProvider</property>
<property name=”initialSize”>2</property>
<property name=”minIdle”>1</property>
<property name=”maxActive”>300</property>
<property name=”maxWait”>60000</property>
<property name=”timeBetweenEvictionRunsMillis”>60000</property>
<property name=”minEvictableIdleTimeMillis”>300000</property>

 

4. className.hbm.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE hibernate-mapping PUBLIC
“-//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd”>
<!– package即是包名 –>
<hibernate-mapping package=”edu.miis.bean”>
<!– 表名 与 POJO名。table名如果不设,则默认与class名一致 –>
<class name=”UserInfo” table=”userinfo”>
<!– pojo标识列和主键 –>
<id name=”id” column=”uid”>
<!– 主键生成方式 –>
<generator class=”native”></generator>
</id>
<property name=”name” column=”uname”></property>
<property name=”password” column=”upassword”></property>
</class>
</hibernate-mapping>




Sites DOT MIISThe Middlebury Institute site network.