hibernate的开发步骤示例

1.导入jar(hibernate+mysql)。
2.建表。
3.安装Jboss中的hibernate tools。
4.建hibernate.cfg.xml。(记得mapping model进去。,去掉session-factory的name属性 )
5.调用hibernate API编程。


create database t2;
create table person (id int not null auto_increment,
    name varchar(100),
    age  int,
    primary key(id)
);


<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE hibernate-configuration PUBLIC “-//Hibernate/Hibernate Configuration DTD 3.0//EN”
                                         “http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd”>
<hibernate-configuration>
 <session-factory >
  <property name=”hibernate.connection.driver_class”>org.gjt.mm.mysql.Driver</property>
  <property name=”hibernate.connection.url”>jdbc:mysql://localhost:3306/t2</property>
  <property name=”hibernate.connection.username”>root</property>
  <property name=”hibernate.connection.password”>123</property>
  <property name=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</property>
  <property name=”hibernate.show_sql”>true</property>
  <property name=”hibernate.use_sql_comments”>false</property>
  <property name=”hibernate.format_sql”>true</property>
  <mapping class=”test1.model.Person”/>
 </session-factory>
</hibernate-configuration>


package test1.model;
// Generated 2016-5-2 12:11:09 by Hibernate Tools 5.0.0.Alpha3

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Person generated by hbm2java
 */
@Entity
@Table(name = “person”)
public class Person implements java.io.Serializable {

    private Integer id;
    private String name;
    private Integer age;

    public Person() {
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)

    @Column(name = “id”, unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

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

    @Column(name = “name”, length = 100)
    public String getName() {
        return this.name;
    }

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

    @Column(name = “age”)
    public Integer getAge() {
        return this.age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}


package test1.r1;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import test1.model.Person;

public class HibernateSample1 {
    public static void main(String args[]){
        System.out.println(“开始事务”);
        Configuration config = new Configuration().configure();
       SessionFactory sessionFactory = config.buildSessionFactory();
       Session session = sessionFactory.openSession();
       try{
           Transaction tx = session.beginTransaction();
               
             Person p1 = new Person();
                 p1.setName(“bkd”);
                 p1.setAge(12);            
             session.save(p1);
             tx.commit();
       }catch(HibernateException e){
           e.printStackTrace();
           session.getTransaction().rollback();
       } finally {
           session.close();
           sessionFactory.close();
       }
       System.out.println(“事务结束”);

    }
}

Author: bkdwei