精典Hibernate 学习笔记1代码

来源:互联网 发布:yum卸载 保留依赖 编辑:程序博客网 时间:2024/04/24 04:56

1.用Java应用中的属性文件!!

 hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/SAMPLEDB
hibernate.connection.username=root
hibernate.connection.password=1234
hibernate.show_sql=true

 

 

2.HIBERNATE 持久类!

package mypack;
import java.io.Serializable;
import java.sql.Date;
import java.sql.Timestamp;

public class Customer implements Serializable {
  private Long id;
  private String name;
  private String email;
  private String password;
  private int phone;
  private String address;
  private char sex;
  private boolean married;
  private String description;
  private byte[] image;
  private Date birthday;
  private Timestamp registeredTime;

  public Customer(){}

  public Long getId(){
    return id;
  }

  public void setId(Long id){
    this.id = id;
  }

  public String getName(){
    return name;
  }

  public void setName(String name){
    this.name=name;
  }

  public String getEmail(){
    return email;
  }

  public void setEmail(String email){
    this.email =email ;
  }

  public String getPassword(){
    return password;
  }

  public void setPassword(String password){
      this.password =password ;
  }

  public int getPhone(){
    return phone;
  }

  public void setPhone(int phone){
    this.phone =phone ;
  }

  public String getAddress(){
    return address;
  }

  public void setAddress(String address){
    this.address =address ;
  }
  public char getSex(){
    return sex;
  }

  public void setSex(char sex){
    this.sex =sex ;
  }

  public boolean isMarried(){
    return married;
  }

  public void setMarried(boolean married){
    this.married =married ;
  }

  public String getDescription(){
      return description;
  }

  public void setDescription(String description){
      this.description =description ;
  }

  public byte[] getImage() {
        return this.image;
  }

  public void setImage(byte[] image) {
        this.image = image;
  }

  public Date getBirthday() {
        return this.birthday;
  }

  public void setBirthday(Date birthday) {
        this.birthday = birthday;
  }

  public Timestamp getRegisteredTime() {
        return this.registeredTime;
  }

  public void setRegisteredTime(Timestamp registeredTime) {
        this.registeredTime = registeredTime;
  }

}

 

3.HIBERNATE持久类配置

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

<hibernate-mapping>
  <class name="mypack.Customer" table="CUSTOMERS">
    
 
    <id name="id" column="ID" type="long">
      <generator class="increment"/>
    </id>
    <property name="name"  column="NAME"  type="string" not-null="true" /> 
    <property name="email"     column="EMAIL"     type="string" not-null="true" />
    <property name="password"  column="PASSWORD"  type="string" not-null="true"/>
    <property name="phone"     column="PHONE"     type="int" />
    <property name="address"   column="ADDRESS"   type="string" />
    <property name="sex"       column="SEX"       type="character"/> 
    <property name="married"   column="IS_MARRIED"  type="boolean"/>     
    <property name="description"   column="DESCRIPTION"  type="text"/>     
    <property name="image"         column="IMAGE"        type="binary"/>
    <property name="birthday"      column="BIRTHDAY"     type="date"/>
    <property name="registeredTime" column="REGISTERED_TIME"  type="timestamp"/> 

  </class>

</hibernate-mapping>