Hibernate4学习笔记(一): 生成mysql数据库表

日期: 2016-7-19


内容: Hibernate4生成数据库表。


1、 实体类Users.java的设计:

package com.hibtest.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;


/* 用户实体类 */
@Entity
//改变数据库表的名字
@Table(name="user_info")
public class Users implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// 编写用户实体类的字段信息,对应数据库中的表字段信息
	// 编号
	//private int id;由于在从表中设置了外键,所以这个主键取消
	//private IdCard card;
	private int id;//用户ID
	// 用户名
	private String username;
	// 密码
	private String password;
	// 邮箱
	private String email;
	// 手机号码
	private String phone;
	// 地址信息
	private String address;
	// 性别
	private String sex;
	//出生日期
	private Date birthday;

	// 不带参数的构造函数
	public Users() {

	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	// 全参数的构造函数
	public Users(int id,String username, String password, String email,
			String phone, String address, String sex,Date birthday) {
		super();
		//this.card = card;
		this.id = id;
		this.username = username;
		this.password = password;
		this.email = email;
		this.phone = phone;
		this.address = address;
		this.sex = sex;
		this.birthday = birthday;
	}
	
//	@OneToOne(cascade=CascadeType.ALL)//表示全级联
//	@JoinColumn(name="pid",unique=true)
//	public IdCard getCard() {
//		return card;
//	}
//
//	public void setCard(IdCard card) {
//		this.card = card;
//	}

	//将Id使用Hibernate的注解设置数据库主键
	@Id
	//设置数据库主键字段为自增
	@GeneratedValue
	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 String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}


	//为了方便测试可以重写toStrong()方法
	@Override
	public String toString() {
		return "Users [id=" + id + ", username="
				+ username + ", password=" + password + ", email=" + email
				+ ", phone=" + phone + ", address=" + address + ", sex=" + sex
				+ ", birthday=" + birthday + "]+"+"\n";
	}


}

2、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>
    <!-- 显示sql语句 -->
    <property name="show_sql">true</property>
    <property name="myeclipse.connection.profile">bookshop</property>
    <!-- <property name="connection.url">
        jdbc:mysql://localhost:3306/bookshop
        jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8 
    </property> -->
    
    <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property>
    <property name="connection.username">root</property>
    <property name="connection.password">910214</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    <property name="hibernate.current_session_context_class">thread</property>

    <mapping class="com.hibtest.entity.Account"/>
    <mapping class="com.hibtest.entity.Users"/>
    
</session-factory>
</hibernate-configuration>

3、测试方法生成数据库表格:

@Test
	public void testShemaExport()
	{
		//加载Hibernate配置文件
		Configuration configuration = new Configuration().configure();
		
		//创建服务注册对象
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		
		//创建sessionFactory
		SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		
		//生成SchmaExport
		SchemaExport schemaEport = new SchemaExport(configuration);
		
		//生成数据库表
		schemaEport.create(true, true);
	}


4、打印log:

2016/07/19 15:07:17 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
2016/07/19 15:07:17 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.4.Final}
2016/07/19 15:07:17 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
2016/07/19 15:07:17 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
2016/07/19 15:07:17 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2016/07/19 15:07:17 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2016/07/19 15:07:17 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
2016/07/19 15:07:17 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
2016/07/19 15:07:17 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
2016/07/19 15:07:17 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
2016/07/19 15:07:17 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8]
2016/07/19 15:07:17 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
2016/07/19 15:07:17 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2016/07/19 15:07:17 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
2016/07/19 15:07:17 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
2016/07/19 15:07:17 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: hibernate.account
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [id, balance, accountno]
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: hibernate.user_info
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [id, birthday, username, sex, phone, email, address, password]
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
2016/07/19 15:07:18 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2016/07/19 15:07:18 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2016/07/19 15:07:18 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
2016/07/19 15:07:18 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
2016/07/19 15:07:18 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
2016/07/19 15:07:18 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
2016/07/19 15:07:18 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8]
2016/07/19 15:07:18 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
2016/07/19 15:07:18 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2016/07/19 15:07:18 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
2016/07/19 15:07:18 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
2016/07/19 15:07:18 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: hibernate.account
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [id, balance, accountno]
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: hibernate.user_info
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [id, birthday, username, sex, phone, email, address, password]
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
2016/07/19 15:07:18 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
2016/07/19 15:07:18 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
2016/07/19 15:07:18 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
2016/07/19 15:07:18 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
2016/07/19 15:07:18 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8]
2016/07/19 15:07:18 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}

    drop table if exists Account

    drop table if exists user_info

    create table Account (
        id integer not null auto_increment,
        accountNo varchar(255),
        balance integer not null,
        primary key (id)
    )

    create table user_info (
        id integer not null auto_increment,
        address varchar(255),
        birthday datetime,
        email varchar(255),
        password varchar(255),
        phone varchar(255),
        sex varchar(255),
        username varchar(255),
        primary key (id)
    )
2016/07/19 15:07:18 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8]
2016/07/19 15:07:18 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete


是一个hibernate完成mysql实体类到数据库表字段的映射。

























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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值