hibernate的几个配制 ,其一:hibernate.cfg.xml,其二:Emp.hbm.xml ;其三:Dept.hbm.xml ;其四:Student.hbm.xml

总配制hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
              
              
 <hibernate-configuration>
        <session-factory>
             <property name="connection.url">jdbc:mysql://localhost:3306/cool</property>
        <property name="connection.username">root</property>
        <property name="connection.password">lovo</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
             <!--        控制台打印出相应的sql语句  -->
        <property name="show_sql">true</property>
        <!--          让显示的sql语句有一定的格式  -->
        <property name="format_sql">true</property>
        
            <mapping resource="com/lovo/pojo/Emp.hbm.xml"/>  
  
              
        </session-factory>
     
 </hibernate-configuration>

Emp.hbm.xml 多对一

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.lovo.pojo.Emp" table="emp">
          <id name="id" column="id" type="integer" > 
                  <generator class="native" />
          </id>
          <property name="empno" column="empno " type="integer"></property>
          <property name="ename" column="eName " type="string"></property>
         <property name="job" column="job " type="string"></property>
         <property name="mgr" column="mgr " type="integer"></property>
         <property name="hireDate" column="hireDate " type="date"></property>
         <property name="sal" column="sal " type="double"></property>
         <property name="comm" column="comm " type="double"></property>
        <many-to-one name="dept" class="com.lovo.pojo.Dept" column="deptNo" fetch="join"></many-to-one>    
    </class>
    
</hibernate-mapping>

Dept.hbm.xml 一对多:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

    <hibernate-mapping>
     <class  name="com.lovo.pojo.Dept"  table="dept"> 
         <id name="deptNo" column="deptNo " type="java.lang.Integer">
             <generator  class="native"/>
         </id>
                 <property name="dname" column="dname" type="java.lang.String" />
                  <property name="loc" column="loc" type="java.lang.String" />
                 <set name="emps">
                      <key column="deptNo"></key>
                      <one-to-many class="com.lovo.pojo.Emp"/>
                 </set>
         </class>        
           
    </hibernate-mapping>

Student.hbm.xml 多对多:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
     <class name="com.lovo.pojo.Student" table="student">
      <!-- <cache usage="read-only" region="sampleCache1"/> -->
        <id name="sId" column="s_id" type="integer">
             <generator class="native"/>
        </id>
     <property name="sName" column="s_name" type="string" />
     
     <set name="courses" table="t_s_fk">
         <key column="sId"></key>
         <many-to-many class="Course" column="cId" />
     </set>
   
     </class>
</hibernate-mapping>

 

posted on 2016-08-25 01:29  loongs  阅读(703)  评论(0编辑  收藏  举报

导航