使用SchemaExport生成数据表

转载自:一口一口吃掉Hibernate(一)——使用SchemaExport生成数据表

hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。

今天就来演示一下Hibernate最初级的操作,使用SchemaExport创建数据表。

1.首先建立POJO类

package com.bjpowernode.hibernate;

import java.util.Date;

/**
 * 用户
 * @author Longxuan
 *
 */
public class User {

    private String  id;

    private String name;

    private String password;

    private Date createTime;

    private Date expireTime;

    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 getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Date expireTime) {
        this.expireTime = expireTime;
    }

}

2.根据POJO类里面里面相关的字段,在包中创建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">
<hibernate-mapping>
    <class name="com.bjpowernode.hibernate.User" >
        <!--hibernate为我们生成主键id-->
        <id name="id">
            <generator class="uuid" />
        </id>

        <!--默认把类的变量映射为相同名字的表列,当然我们使用column属性修改表字段-->
        <property name="name" column="name"></property>
        <property name="password"></property>
        <property name="createTime"></property>
        <property name="expireTime"></property>
    </class>
</hibernate-mapping>

3.在src中建立hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC  
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  

<hibernate-configuration>  
    <session-factory name="foo">  
        <!-- 数据库的连接也可以直接使用hibernate.properties文件 -->  
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_test</property>  
        <property name="hibernate.connection.username">root</property>  
        <property name="hibernate.connection.password">root</property>  

        <property name="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property><!-- 指定sql方言 -->  
        <property name="hibernate.show_sql">true</property><!-- 设置是否显示生成sql语句 -->  
        <property name="hibernate.format_sql">true</property><!-- 设置是否格式化sql语句-->  

        <mapping resource="com/bjpowernode/hibernate/User.hbm.xml"  />  
    </session-factory>  
</hibernate-configuration>  

4.建立ExportDB类

package com.bjpowernode.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;



/**
 * 将hbm生成ddl
 * @author Longxuan
 *
 */
public class ExportDB {

    /**
     * @param args
     */
    public static void main(String[] args) {

        // 默认读取hibernate.cfg.xml文件
        Configuration cfg = new Configuration().configure();

        // 生成并输出sql到文件(当前目录)和数据库
        SchemaExport export = new SchemaExport(cfg);

        // true 在控制台打印sql语句,true 导入sql语句到数据库,即可执行
        export.create(true, true);
    }
}

5.建立log4j.properties日志文件

### direct log messages to stdout ###  
log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
log4j.appender.stdout.Target=System.out  
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n  

### set log levels - for more verbose logging change 'info' to 'debug' ###  

log4j.rootLogger=warn, stdout  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot并不提供自定义SQL生成建表工具,但是可以通过使用JPA(Java Persistence API)实现这个功能。JPA是一种Java规范,它定义了对象-关系映射(ORM)的标准,可以将Java对象映射到关系型数据库中的表结构。 下面是一个简单的示例,演示如何使用Spring Boot和JPA来生成建表SQL: 1. 添加依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> ``` 2. 配置数据源 在application.properties文件中配置数据源: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 3. 实体类定义 定义一个实体类,使用JPA注解指定表名和字段名: ``` @Entity @Table(name = "user") public class UserEntity { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String email; // getters and setters } ``` 4. 生成建表SQL 使用Hibernate提供的SchemaExport工具生成建表SQL: ``` @Configuration public class JpaConfig { @Autowired private EntityManagerFactory entityManagerFactory; @Bean public CommandLineRunner commandLineRunner() { return args -> { SchemaExport schemaExport = new SchemaExport(); schemaExport.setFormat(true); schemaExport.setDelimiter(";"); schemaExport.setOutputFile("schema.sql"); schemaExport.execute(EnumSet.of(TargetType.SCRIPT), SchemaExport.Action.CREATE, entityManagerFactory); }; } } ``` 这段代码定义了一个CommandLineRunner bean,它会在应用启动时执行。在执行过程中,它使用HibernateSchemaExport工具生成建表SQL,并将其输出到文件schema.sql中。 5. 运行应用程序 现在,您可以运行应用程序并查看生成建表SQL。如果您使用的是默认配置,建表SQL将输出到项目根目录下的schema.sql文件中。 以上就是使用Spring Boot和JPA生成建表SQL的简单示例。当然,实际上,您可能需要更复杂的实体类和更复杂的数据库模式,但这个示例可以为您提供一个好的起点。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值