hibernate.cfg.xml配置文件和hbm.xml配置文件该如何配置

 

 

hibernate.cfg.xml配置文件格式

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6 <session-factory>
 7  <property name="connection.driver_class">
 8   com.mysql.jdbc.Driver
 9  </property>
10  <property name="connection.url">
11   jdbc:mysql://localhost:3306/dang?useUnicode=true&amp;characterEncoding=utf8
12  </property>
13  <property name="connection.username">root</property>
14  <property name="connection.password">123</property>
15 
16  <!-- 显示SQL语句 -->
17  <property name="show_sql">true</property>
18  <property name="format_sql">true</property>
19  <!-- 定义方言 -->
20  <property name="dialect">
21   org.hibernate.dialect.MySQLDialect
22  </property>
23    <mapping  resource="entity/User.hbm.xml"/>
24 </session-factory>
25 </hibernate-configuration>

 

hbm.xml配置文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 4 <hibernate-mapping>
 5     <!--name 实体类的 包名+ 类名        table数据库的表名    -->  
 6  <class name="entity.User" table="t_user">
 7      <!-- name对应实体类的属性id -->
 8      <id  name="id" type="integer">
 9        <!-- name 对应数据库的列名id  -->
10        <column name="id"></column>
11        <!--主键的生成策略 native可以适应多种数据库  increment mysql自动增长策略  sequence oracle自动增长策略 -->
12        <generator class="native"></generator>
13      </id>
14     <property name="name">
15       <column name="name"></column>
16     </property>
17    
18     <property name="pwd">
19        <column name="pwd"></column>
20     </property>
21     <property name="age">
22        <column name="age"></column>
23     </property>
24  </class>
25 
26 </hibernate-mapping>

 

 以上方法通过配置文件方式 关联映射的类和数据库表,方式比较繁琐,可以通过使用注解的方式简化配置。

 

hibernate.cfg.xml配置文件格式

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 3 <hibernate-configuration>
 4   <session-factory>
 5     <!-- 设置绑定JNDI-->
 6     <property name="hibernate.session_factory_name_is_jndi">false</property>   
 7     <!-- 设置使用的方言-->
 8     <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
 9     <!-- 设置数据库连接信息-->
10     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
11      <!-- 设置有关连接池的不知道什么属性-->
12     <property name="connection.provider_class">com.lietou.data.dao.hibernate.connection.provider.EncryptedC3p0ConnectionProvider</property>
13     <property name="connection.encryptor_registered_name">configurationHibernateEncryptor</property>
14     <property name="hibernate.connection.url">jdbc:mysql://主机路径:3306/candidate?zeroDateTimeBehavior=convertToNull&amp;characterEncoding=UTF-8</property>
15     <property name="hibernate.connection.username">用户名</property>
16     <property name="hibernate.connection.password">密码</property>
17      <!-- 注入需要映射的实体类-->
18     <mapping class="类完整路径"/>
19     <mapping class="com.lxxx.web.dao.model.xxx.Class"/>
20      <!-- 需要匹配到的query语句 -->
21     <mapping resource="candidate.hbm.xml"/>
22   </session-factory>
23 </hibernate-configuration>    

注:在hibernate.cfg.xml中包括3种信息:数据库连接信息     实体类    query语句 ,每个cfg.xml文件对应一个数据库,包含库下多个表的映射信息。

 

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>
    <query name="getCandidateEntryManagementIdByIds">
        select column from TABLE where xxId = :xxId and xxxId = :xxxId and xxxxId = :xxxxId
    </query>
   
    <sql-query name="getReviewCountByUpdateAt">
        <return-scalar column="total_count" type="long" />
        select count(distinct hunter_id,candidate_id,hr_id) as total_count from candidate_review_management  where review_at >= :start and :end >
review_at and review_progress>=10 and review_progress!=14 
    </sql-query>
        
</hibernate-mapping>

 

类中进行标注

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
//标注为entity ,标注对应的表名
@Entity
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Table(name="profile")
public class Profile implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private long xx_id;
        private long xxx_id;
    private PersonBasicInformation personBasicInformation; 
    //将属性与表中的列对应
    @Column(name="xx_id")
    public long getxxId() {
        return xxId;
    }
    public void setHrId(long xxId) {
        this.xxId = xxId;
    }
    @Id
    @Column(name="xxx_id")
    @GeneratedValue
    public long getxxxId() {
        return xxxId;
    }
    public void set xxxId(long  xxxId) {
        this. xxxId = xxxId;
    }
    //一对一  一对多关系配置
    @OneToOne
    @JoinColumn(name="person_basic_information_id")
    public PersonBasicInformation getPersonBasicInformation() {
        return personBasicInformation;
    }
    public void setPersonBasicInformation(
            PersonBasicInformation personBasicInformation) {
        this.personBasicInformation = personBasicInformation;
    }
    @Transient
    public String getCreateDateStr() {
        return createDateStr;
    }
    public void setCreateDateStr(String createDateStr) {
        this.createDateStr = createDateStr;
    }    
    
}    

 

Dao中对sql语句的调用

private DaoOperation<xx> xxDao;
...

List<Long> ids = hrContractDao.getIdByQuery("getContractByHrIdAndContractType", params);

通过query name查找对应sql。  注意service中的dao属性需要在configuration中进行配置,指向.cfg.xml  找到对应数据库的配置文件。

你可能感兴趣的:(hibernate.cfg.xml配置文件和hbm.xml配置文件该如何配置)