HIBERNATE:Hibernate 学习一--注解方式自动建表

一.必备包

  1.commons-collections-3.2.1.jar

  2.ejb3-persistence.jar

  3.hibernate3.jar

  4.hibernate-jpa-2.0-api-1.0.1.Final.jar

  5.javassist-3.15.0-GA.jar

  6.jboss-logging-3.1.0.GA.jar

  7.jboss-transaction-api_1.1_spec-1.0.0.Final.jar

  8.slf4j-api-1.6.1.jar

  9.数据库驱动包(MySql:mysql-connector-java-5.1.9-bin.jar)

 

二.配置文件hibernate.cfg.xml

 

<?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="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/DREAM_20121208</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.current_session_context_class">thread</property>

        <mapping class="org.apache.dream.entity.User" />
    </session-factory>
</hibernate-configuration>

 三.实体 User.java

 

@Entity
@Table(name = "T_USER")
public class User {
    private int id;
    private String username;
    private int age;

    @Id
    @GenericGenerator(name = "generator", strategy = "increment")
    @GeneratedValue(generator = "generator")
    @Column(name = "I_ID")
    public int getId() {
        return id;
    }

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

    @Column(name = "STR_USER_NAME", length = 20)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name = "I_AGE")
    public int getAge() {
        return age;
    }

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

 四.工具类

 

public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    static {
        try {
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

 五.测试类:Test.java

 

public class Test {

    public static void main(String[] args) {
        createAndStorePerson();
        HibernateUtil.getSessionFactory().close();
    }

    private static void createAndStorePerson() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();

        session.beginTransaction();

        User user = new User();
        user.setUsername("test");
        user.setAge(18);
        session.save(user);
        session.getTransaction().commit();
    }
}

 六.创建数据库(要与配置文件hibernate.cfg.xml中相符)

 七.结束

 

 

 

你可能感兴趣的:(Hibernate)