hibernate框架配置

实体类

 1 package entity;
 2 
 3 public class Customer {
 4 
 5     /*
 6      * CREATE TABLE `cst_customer` (
 7       `cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
 8       `cust_name` VARCHAR(32) NOT NULL COMMENT '客户名称(公司名称)',
 9       `cust_source` VARCHAR(32) DEFAULT NULL COMMENT '客户信息来源',
10       `cust_industry` VARCHAR(32) DEFAULT NULL COMMENT '客户所属行业',
11       `cust_level` VARCHAR(32) DEFAULT NULL COMMENT '客户级别',
12       `cust_linkman` VARCHAR(64) DEFAULT NULL COMMENT '联系人',
13       `cust_phone` VARCHAR(64) DEFAULT NULL COMMENT '固定电话',
14       `cust_mobile` VARCHAR(16) DEFAULT NULL COMMENT '移动电话',
15       PRIMARY KEY (`cust_id`)
16     ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
17      */
18     private Long cust_id;
19 
20     private String cust_name;
21     private String cust_source;
22     private String cust_industry;
23     private String cust_level;
24     private String cust_linkman;
25     private String cust_phone;
26     private String cust_mobile;
27     public Long getCust_id() {
28         return cust_id;
29     }
30     public void setCust_id(Long cust_id) {
31         this.cust_id = cust_id;
32     }
33     public String getCust_name() {
34         return cust_name;
35     }
36     public void setCust_name(String cust_name) {
37         this.cust_name = cust_name;
38     }
39     public String getCust_source() {
40         return cust_source;
41     }
42     public void setCust_source(String cust_source) {
43         this.cust_source = cust_source;
44     }
45     public String getCust_industry() {
46         return cust_industry;
47     }
48     public void setCust_industry(String cust_industry) {
49         this.cust_industry = cust_industry;
50     }
51     public String getCust_level() {
52         return cust_level;
53     }
54     public void setCust_level(String cust_level) {
55         this.cust_level = cust_level;
56     }
57     public String getCust_linkman() {
58         return cust_linkman;
59     }
60     public void setCust_linkman(String cust_linkman) {
61         this.cust_linkman = cust_linkman;
62     }
63     public String getCust_phone() {
64         return cust_phone;
65     }
66     public void setCust_phone(String cust_phone) {
67         this.cust_phone = cust_phone;
68     }
69     public String getCust_mobile() {
70         return cust_mobile;
71     }
72     public void setCust_mobile(String cust_mobile) {
73         this.cust_mobile = cust_mobile;
74     }
75     @Override
76     public String toString() {
77         return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";
78     }
79 
80 }

 

实体类的配置文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 3         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 4 
 5 <hibernate-mapping package="entity">
 6       <class name="Customer" table="CUST">
 7             <id name="cust_id" column="cust_id">
 8                     <generator class="uuid"/>
 9             </id>
10 
11             <property name="cust_name" column="cust_name" />
12             <property name="cust_source" column="cust_source" />
13             <property name="cust_industry" column="cust_industry" />
14             <property name="cust_level" column="cust_level" />
15             <property name="cust_linkman" column="cust_linkman" />
16             <property name="cust_phone" column="cust_phone" />
17             <property name="cust_mobile" column="cust_mobile" />
18       </class>
19 </hibernate-mapping>

 

  • <hibernate-mapping>标签用来配置表与实体对象的关系
  • <class>标签用来配置表与实体的对应关系,name类名,table数据库表名
  • <id>配置主键映射的属性 name:填写主键对应的属性名,column:填写表中的主键列名
  • <peoperty>用来配置除主键之外的属性与列名的映射关系,name:属性名,column:列名,not-null:配置该属性列是否不能为空,默认值为false,即不能为空,length:配置在数据库中列的长度,默认是数据库类型的最大长度,例如设置某个属性的类型为string类型,则其对应的数据库类型为varchar类型,最大长度为255。

 

主配置文件

 1 <!--
 2   ~ Hibernate, Relational Persistence for Idiomatic Java
 3   ~
 4   ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 5   ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 6   -->
 7 <!DOCTYPE hibernate-configuration PUBLIC
 8         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 9         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
10 
11 <hibernate-configuration>
12     <session-factory name="foo">
13 
14     <!--
15         #hibernate.dialect org.hibernate.dialect.Oracle8iDialect
16         #hibernate.dialect org.hibernate.dialect.Oracle9iDialect
17         #hibernate.dialect org.hibernate.dialect.Oracle10gDialect
18         #hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
19         #hibernate.connection.username ora
20         #hibernate.connection.password ora
21         #hibernate.connection.url jdbc:oracle:thin:@localhost:1521:orcl
22         #hibernate.connection.url jdbc:oracle:thin:@localhost:1522:XE
23     -->
24         <!--数据库驱动-->
25         <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
26         <!--连接数据库URL-->
27         <property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.12.172:1521:indsdb</property>
28         <!--数据库连接用户名-->
29         <property name="hibernate.connection.username">mtstest</property>
30         <!--数据库连接密码-->
31         <property name="hibernate.connection.password">mtstest</property>
32         <!--数据库方言-->
33         <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
34         <!--show_sql,hibernate会将其生成的sql进行打印-->
35         <property name="show_sql">true</property>
36         <!--将打印的sql进行格式化显示-->
37         <property name="hibernate.format_sql">true</property>
38 
39         <!--
40             ## auto schema export
41             #hibernate.hbm2ddl.auto create-drop  自动创建表,并且每次框架运行完之后会将所有表进行删除
42             #hibernate.hbm2ddl.auto create   自动建表,每次框架运行完之后都会创建新的表,原来的表数据会丢失!
43             #hibernate.hbm2ddl.auto update (推荐使用)  自动生成表,如果已经存在就不会再生成,如果有变动,会更新表
44             #hibernate.hbm2ddl.auto validate  任何情况下都不会自动生成表。每次启动都会校验数据库中的表是否正确。
45         -->
46         <!--自动导出表结构,自动建表-->
47         <property name="hibernate.hbm2ddl.auto">update</property>
48 
49         <!--orm元数据:mapping中写实体类的hibernate配置文件-->
50         <mapping resource="hibernate/Customer.hbm.xml"/>
51         <class-cache
52                 class="org.hibernate.test.legacy.Simple"
53                 region="Simple"
54                 usage="read-write"/>
55     </session-factory>
56 </hibernate-configuration>

 4.配置文件详解
 orm元数据(xxx.hbm.xml)
  <hibernate-mapping package="">
   <class name table>
    <id name >
     <generator class="">
    </id>
    <property name="" />
  
 hibernate.cfg.xml
  必选配置
          4+1 方言
  可选配置
         显示sql
         格式化sql
         自动生成表
         |- update
  orm元数据引入
         <mapping resource="" />

 

posted @ 2017-12-12 21:56  Garcia11  阅读(193)  评论(0)    收藏  举报