Hibernate之映射

来源:互联网 发布:网络批发城 编辑:程序博客网 时间:2024/06/03 21:40

一、映射文件

 

 Hibernate在实现ORM功能的时候主要用到的文件有:
    1、 映射类(*.Java)

       描述数据库表的结构,表的字段映射类的属性,表的记录映射为类的对象
  
    2、映射文件(*.hbm.xml)

       指定数据库表和映射类之间的关系。

       包括映射类和数据库、表字段和类属性类型以及表字段和类属性名称的对应关系。
 
    3、 hibernate核心配置文件(*.properties/*.cfg.xml)

       指定hibernate的一些核心配置,包含与数据库连接时需要的连接信息。

       如:连接数据库、登录数据库的用户名、登录密码以及连接字符串、映射文件的地址。

 

   4、分类

 

            

 

    

二、XML方式配置映射


classpath:com/test目录下的User.hbm.xml

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping>  
  6.     <class name="com.test.User" >  
  7.         <id name="id">  
  8.             <!-- 算法的核心思想是结合机器的网卡、当地时间、一个随机数来生成id -->  
  9.             <generator class="uuid2"></generator>  
  10.         </id>  
  11.         <property name="name"></property>  
  12.         <property name="password"></property>    
  13.     </class>  
  14. </hibernate-mapping>  

 

三、注解映射配置


1、加入hibernate annotion支持包

        hibernate-commons-annotations-5.0.1.Final.jar

       hibernate-jpa-2.1-api-1.0.0.Final.jar


 2、hibernate5必须导入jar包    -hibernate-release-5.2.7.Final.zip(hibernate项目文件的jar)

         antlr-2.7.7.jar (生成SQL语句)
         classmate-1.3.0.jar
         dom4j-1.6.1.jar
         geronimo-jta_1.1_spec-1.1.1.jar
         hibernate-commons-annotations-5.0.1.Final.jar
         hibernate-core-5.2.7.Final.jar
         hibernate-jpa-2.1-api-1.0.0.Final.jar
         jandex-2.0.3.Final.jar
         javassist-3.20.0-GA.jar
         jboss-logging-3.3.0.Final.jar
         mysql-connector-java-6.0.5.jar    


3、建立实体类User,采用注解完成映射

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. package com.test;  
  2.   
  3. import javax.persistence.Column;  
  4. import javax.persistence.Entity;  
  5. import javax.persistence.GeneratedValue;  
  6. import javax.persistence.GenericGenerator;  
  7. import javax.persistence.Id;  

  8.   
  9. @Entity //不写Table默认为user,@Table(name="user")  
  10. public class User {  
  11.   
  12.     @Id //主键  
  13.     @GenericGenerator(name = "generator", strategy = "uuid2")
        @GeneratedValue(generator = "generator")
  14.     private String id;  
  15.     @Column(name="name"//字段为name,可以不注解@Column  
  16.     private String name;  
  17.     private String password;  

  18.     public int getId() {  
  19.         return id;  
  20.     }  
  21.     public void setId(int id) {  
  22.         this.id = id;  
  23.     }  
  24.     
  25.     public String getName() {  
  26.         return name;  
  27.     }  
  28.     public void setName(String name) {  
  29.         this.name = name;  
  30.     }  
  31.     public String getPassword() {  
  32.         return password;  
  33.     }  
  34.     public void setPassword(String password) {  
  35.         this.password = password;  
  36.     }  
  37.   
  38.   
  39. }  

 

四、hibernate.cfg.xml文件配置

1、将User加入到hibernate.cfg.xml配置文件

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0"?> 
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.     <session-factory>  
  8.         <!-- 驱动 -->  
  9.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  10.         <!-- 数据库URL -->  
  11.         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_db</property>  
  12.         <!-- 数据库用户名 -->  
  13.         <property name="hibernate.connection.username">root</property>  
  14.         <!-- 数据库密码 -->  
  15.         <property name="hibernate.connection.password">admin</property>  
  16.         <!-- mysql的方言 -->  
  17.         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  18.           
  19.         <!-- 映射文件配置 -->  
  20.         <!--  <mapping resource="com/test/User.hbm.xml"/>  -->    
  21.           
  22.         <!-- 由原来的映射文件,改成实体类 -->  
  23.         <mapping class="com.test.User"/>  
  24.           
  25.     </session-factory>  
  26. </hibernate-configuration>

2、不配置User类的mapping

     

     Configuration 指定映射文件

     Configuration cfg=new Configuration().configure("/hibernate.cfg.xml");
     cfg.addResource("com/test/User.hbm.xml");

     或 cfg.addClass(com.test.User.class);



五、hibernate.properties文件配置

1、hibernate.properties

#数据库使用的驱动类hibernate.connection.driver_class=com.mysql.jdbc.Driver#数据库连接串hibernate.connection.url=jdbc:mysql://localhost:3306/hibernate_db#数据库连接的用户名hibernate.connection.username=user#数据库连接的密码hibernate.connection.password=admin#数据库使用的方言hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect#是否打印SQL语句hibernate.show_sql=truejavax.persistence.validation.mode=none

2、配置工具类

Configuration cfg = new Configuration();cfg.configure("/hibernate.properties");cfg.addResource("com/test/User.hbm.xml");//cfg.addClass(com.test.User.class);






0 0
原创粉丝点击