Hibernate自动创建数据库表

来源:互联网 发布:数据库账号管理办法 编辑:程序博客网 时间:2024/05/22 12:51

  Hibernate自动创建数据库表的操作步骤:

  1.导入Hibernate库(jar包)

 2.看截图

  

建立以4个文件,看着名字应该大概知道内容了

3.下面看具体代码:

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"><!-- Generated by MyEclipse Hibernate Tools.                   --><hibernate-configuration><session-factory><property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl  </property><property name="connection.username">hsx</property><property name="connection.password">hsx</property><property name="connection.driver_class">oracle.jdbc.OracleDriver  </property><property name="dialect">org.hibernate.dialect.OracleDialect  </property><property name="format_sql">true</property><property name="show_sql">true</property><mapping resource="com/gem/hsx/bean/User.hbm.xml" /></session-factory></hibernate-configuration>

User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.gem.hsx.bean.Users" table="TESTUSERS" schema="HSX"><id name="id" type="java.lang.Integer"><column name="ID" precision="0" /><generator class="native" /></id><property name="username" type="java.lang.String"><column name="USERNAME" length="50" not-null="true" /></property><property name="age" type="java.lang.Integer"><column name="AGE" precision="0" not-null="true" /></property></class></hibernate-mapping>

Users.java

package com.gem.hsx.bean;public class Users {private Integer id;private String username;private Integer age;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Users() {super();}public Users(Integer id, String username, Integer age) {super();this.id = id;this.username = username;this.age = age;}}


CreateTable.java

package com.gem.hsx.bean;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;public class CreateTable {public static void main(String[] args) {Configuration conf = new Configuration().configure("hibernate.cfg.xml");//1、读取配置文件SchemaExport dbExport = new SchemaExport(conf);dbExport.create(true, true);System.out.println("创建表完毕");//下面进行测试SessionFactory sf=conf.buildSessionFactory();Session session=sf.openSession();Transaction tx=null;try {tx=session.beginTransaction();Users users=new Users();users.setUsername("张三");users.setAge(15);session.save(users);tx.commit();} catch (Exception e) {if (tx!=null) {tx.rollback();}e.printStackTrace();}finally{session.close();}System.out.println("插入完成");}}



原创粉丝点击