欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

精通Hibernate:第一个Hibernate应用(1)

来源: javaer 分享于  点击 43388 次 点评:43

精通Hibernate:第一个Hibernate应用(1)


一、在Java应用中使用Hibernate的步骤

  • 创建Hibernate的配置文件
  • 创建持久化类
  • 创建对象-关系映射文件
  • 通过Hibernate API编写访问数据库的代码

二、Helloapp应用的结构

三、Hibernate的配置文件(hibernate.properties)

  1. hibernate.dialect=org.hibernate.dialect.MySQLDialect  
  2. hibernate.connection.driver_class=com.mysql.jdbc.Driver  
  3. hibernate.connection.url=jdbc:mysql://localhost:3306/SAMPLEDB  
  4. hibernate.connection.username=root  
  5. hibernate.connection.password=1234 
  6. hibernate.show_sql=true 

四、创建持久化类Customer

  • 持久化类符合JavaBean的规范,包含一些属性,以及与之对应的getXXX()和setXXX()方法。
  • 持久化类有一个id属性,用来惟一标识Customer类的每个对象。在面向对象术语中,这个id属性被称为对象标识符OID,Object Identifier),通常它都用整数表示
  • Hibernate要求持久化类必须提供一个不带参数的默认构造方法
  1. package mypack;  
  2. import java.io.Serializable;  
  3. import java.sql.Date;  
  4. import java.sql.Timestamp;  
  5.  
  6. public class Customer implements Serializable {  
  7.   private Long id;  
  8.   private String name;  
  9.   private String email;  
  10.   private String password;  
  11.   private int phone;  
  12.   private String address;  
  13.   private char sex;  
  14.   private boolean married;  
  15.   private String description;  
  16.   private byte[] image;  
  17.   private Date birthday;  
  18.   private Timestamp registeredTime;  
  19.  
  20.   public Customer(){}  
  21.  
  22.   public Long getId(){  
  23.     return id;  
  24.   }  
  25.  
  26.   private void setId(Long id){  
  27.     this.id = id;  
  28.   }  
  29.  
  30.   public String getName(){  
  31.     return name;  
  32.   }  
  33.  
  34.   public void setName(String name){  
  35.     this.name=name;  
  36.   }  
  37.  
  38.   public String getEmail(){  
  39.     return email;  
  40.   }  
  41.  
  42.   public void setEmail(String email){  
  43.     this.email =email ;  
  44.   }  
  45.  
  46.   public String getPassword(){  
  47.     return password;  
  48.   }  
  49.  
  50.   public void setPassword(String password){  
  51.       this.password =password ;  
  52.   }  
  53.  
  54.   public int getPhone(){  
  55.     return phone;  
  56.   }  
  57.  
  58.   public void setPhone(int phone){  
  59.     this.phone =phone ;  
  60.   }  
  61.  
  62.   public String getAddress(){  
  63.     return address;  
  64.   }  
  65.  
  66.   public void setAddress(String address){  
  67.     this.address =address ;  
  68.   }  
  69.   public char getSex(){  
  70.     return sex;  
  71.   }  
  72.  
  73.   public void setSex(char sex){  
  74.     this.sex =sex ;  
  75.   }  
  76.  
  77.   public boolean isMarried(){  
  78.     return married;  
  79.   }  
  80.  
  81.   public void setMarried(boolean married){  
  82.     this.married =married ;  
  83.   }  
  84.  
  85.   public String getDescription(){  
  86.       return description;  
  87.   }  
  88.  
  89.   public void setDescription(String description){  
  90.       this.description =description ;  
  91.   }  
  92.  
  93.   public byte[] getImage() {  
  94.         return this.image;  
  95.   }  
  96.  
  97.   public void setImage(byte[] image) {  
  98.         this.image = image;  
  99.   }  
  100.  
  101.   public Date getBirthday() {  
  102.         return this.birthday;  
  103.   }  
  104.  
  105.   public void setBirthday(Date birthday) {  
  106.         this.birthday = birthday;  
  107.   }  
  108.  
  109.   public Timestamp getRegisteredTime() {  
  110.         return this.registeredTime;  
  111.   }  
  112.  
  113.   public void setRegisteredTime(Timestamp registeredTime) {  
  114.         this.registeredTime = registeredTime;  
  115.   }  
  116.  

注意:

  • getXXX()和setXXX()方法可以采用任意的访问级别,他的命名规则必须符合特定的命名规则,“get”和“set”后面紧跟属性的名字,并且属性名的首字母为大写,如name属性的get方法为getName()。
  • 如果持久化类的属性为boolean类型,那么它的get方法名可以用get做前缀也可以用is做前缀。

五、创建数据库Schema

  1. drop database if exists SAMPLEDB;  
  2. create database SAMPLEDB;  
  3. use SAMPLEDB;  
  4.  
  5. create table CUSTOMERS (  
  6.   ID bigint not null primary key,  
  7.   NAME varchar(15) not null,  
  8.   EMAIL varchar(128) not null,  
  9.   PASSWORD varchar(8) not null,    
  10.   PHONE int ,    
  11.   ADDRESS varchar(255),  
  12.   SEX char(1) ,  
  13.   IS_MARRIED bit,  
  14.   DESCRIPTION text,  
  15.   IMAGE blob,  
  16.   BIRTHDAY date,  
  17.   REGISTERED_TIME timestamp  
  18. ); 


相关栏目:

用户点评