首页 > 代码库 > hibernate.cfg.xml

hibernate.cfg.xml

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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>
    <property name="connection.username">root</property>
        <property name="connection.password">xxx</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_demo</property>
<!--选择数据库方言-->        
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--是否在console输出sql语句-->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
<!--update用于更新数据库表,常见create:如果数据库存在该表先删除再重建-->
        <property name="hbm2ddl.auto">update</property>
        <mapping resource="Students.hbm.xml" />
    </session-factory>
</hibernate-configuration>

Students.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-7-10 22:35:01 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<!-- class name是package.classname -->
    <class name="xml_onetomany.Students" table="STUDENTS">
        <id name="sid" type="int">
            <column name="SID" />
<!--assigned:初始化对象的时候SID要手动赋值,否则int默认为0,其他类型报错;native:自动增长,针对不同数据库意义不同-->
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="gender" type="java.lang.String">
            <column name="GENDER" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="ADDRESS" />
        </property>
    </class>
</hibernate-mapping>

 

hibernate.cfg.xml