hibernate正向生成简单教程

来源:互联网 发布:vb中对象的属性 编辑:程序博客网 时间:2024/05/20 13:17

所谓正向生成就是通过实体类生成表。

本教程以一个实体类为例演示正向生成的方法。

实体类User.java

package tm.change.domain;public class User {private String id;private String name;private String phone;private String email;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + ", phone=" + phone + ", email=" + email + "]";}}

实体类相应的映射文件User.hbm.xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2017-2-16 14:56:38 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping>    <class name="tm.change.domain.User" table="USERS">        <id name="id" type="java.lang.String">            <column name="ID" />            <generator class="assigned" />        </id>        <property name="name" type="java.lang.String">            <column name="NAME" not-null="true" length="20"/>        </property>        <property name="phone" type="java.lang.String">            <column name="PHONE" length="20"/>        </property>        <property name="email" type="java.lang.String">            <column name="EMAIL" length="20"/>        </property>    </class></hibernate-mapping>

下面是最重要的,也就是配置文件hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="hibernate.connection.password">root</property>        <property name="hibernate.connection.url">jdbc:mysql://localhost/goods</property>        <property name="hibernate.connection.username">root</property>        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  <property name="hibernate.format_sql">true</property>  <property name="hibernate.show_sql">true</property>  <!-- 面试题:create update 表已经存在时对表进行更新-->  <property name="hibernate.hbm2ddl.auto">update</property>  <!-- 资源配置 -->  <mapping resource="tm/change/domain/User.hbm.xml"/>    </session-factory></hibernate-configuration>


用一个测试类来测试一下吧

Test.java

package tm.change.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.registry.StandardServiceRegistry;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import tm.change.domain.User;public class Test {@org.junit.Testpublic void saveTest() {// 1.加载配置文件 hibernate.cfg.xmlStandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure() // configures settings from hibernate.cfg.xml.build();try {// 2.SessionFactory工厂对象SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();System.out.println(sessionFactory);} catch (Exception e) {e.printStackTrace();// The registry would be destroyed by the SessionFactory, but we had// trouble building the SessionFactory// so destroy it manually.StandardServiceRegistryBuilder.destroy(registry);}}}


如果你的配置没有错误的话,数据库中应该已经生成表了。





0 0