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;}}
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("插入完成");}}
- Hibernate自动创建数据库表
- hibernate自动创建数据库
- hibernate自动创建数据库
- Hibernate无法自动创建数据库表
- Hibernate自动创建表
- Hibernate自动创建表
- Hibernate自动创建表
- Hibernate自动创建表
- Hibernate自动创建表
- Hibernate自动创建表
- Hibernate自动创建表
- Hibernate自动创建表
- Hibernate自动创建表
- hibernate自动创建表
- Spring集成hibernate映射表自动创建数据库表
- Hibernate连接mysql数据库并自动创建表
- Hibernate连接mysql数据库并自动创建表
- 利用hibernate反向自动创建数据库
- 又见一帘幽梦
- QEMU安装使用全攻
- win+Apache+mod_wsgi部署python程序-apache2.2+python2.7
- lnmp安装包出现Nginx 502 Bad Gateway错误解决方案
- JAVA的StringBuffer类
- Hibernate自动创建数据库表
- 成都JAVA培训,10月15日基础班智播客
- SQL语句的优化
- 教你如何把书本上的字快速弄到电脑上
- android界面之自定义标题栏
- Java学习(回顾)笔记之day1
- 雨中飘荡的回忆
- c++面向对象编程必备“良方”
- 1.ring3-反调试-IsDebuggerPresent