怎么用hibernate自动创建数据库表

当当当~又来啦!这次想介绍一下怎么用hibernate自动创建数据库表

其实百度一下有很多但是在实现的过程中还是遇见了问题,包括现在也没解决,跪请各路大神帮忙指点

首先使用hibernate自动创建数据库表肯定是要先创建一个java项目啦,老生常谈直接上图

强调一下我这里用的是hibernate5.0.1,为什么强调他呢肯定是有原因的,因为新版本自动创建数据库表的方式已经跟以前不一样了嗷


1.导入相关的jar包

2.实体类

package demo;

public class User {
  private int id;
  private String username;
  private String password;
  private int age;
  public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getUsername() {
	return username;
}
public void setUsername(String username) {
	this.username = username;
}
public String getPassword() {
	return password;
}
public void setPassword(String password) {
	this.password = password;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public User(){
	  
  }
  
}

2.配置文件

<?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.dialect">org.hibernate.dialect.MySQLDialect</property>
 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
 <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
  <property name="hibernate.connection.username">root</property>
 <property name="hibernate.connection.password"></property>
 
  <property name="show_sql">true</property>

  <property name="hbm2ddl.auto" >update</property>
    <mapping resource="demo/user.hbm.xml"/>
  
</session-factory>

</hibernate-configuration>
这里声明一下,在这个地方跟之前配置的不同就是加了一个标签,这是自动创建数据库的关键
<property name="hbm2ddl.auto" >update</property>

create: 每次加载hibernate时都会删除上一次的生成的表,然后根据你的类再重新来生成新表,这样很容易丢失数据库

 update: 第一次加载hibernate时会根据实体类自动建立表的结构,以后加载hibernate时根据 实体类自动更新数据库表

注意!敲黑板!hibernate只能自动创建数据库表不可以自动创建数据库嗷~

3.映射关系

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="demo.User" table="t_user">
    <id name="id" column="ID"  type="int">
      	<generator class="native"/>
    </id>
    <property name="username" column="username" type="java.lang.String"  />
    <property name="password" column="password" type="java.lang.String" />
    <property name="age" column="age" type="int"/>
  </class>

</hibernate-mapping>

4.测试类

package test;

import java.util.EnumSet;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;

import demo.User;

public class UserTest {
	public static void main(String[] args) {
		  Configuration conf = new Configuration().configure();//1、读取配置文件
	 	     StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
		        Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata();
		        SchemaExport schemaExport = new SchemaExport();
		    schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata)}


重点是加红部分的代码,由于用的是hibernate5.0版本所以创新数据库表的代码改变,旧版本创建数据库的代码是

 Configuration cfg = new Configuration().configure();  
     
        SchemaExport export = new SchemaExport(cfg);  
         
        export.create(true, true);  

5。运行


控制台显示

然后问题来了,控制台显示成功后数据库里并没找到表。。也就是创表成功但是找不到,百度了很久有说是方言问题的又说是数据问题的都调试过没有用,请大神指教小学渣。。。。

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值