Hibernate整合Spring后,如何使用SchemaExport生成数据库表

一.Hibernate原生状态

 

Configuration cfg = new Configuration().configure();

SchemaExport export = new SchemaExport(cfg);

export.create(true, true);

 

二.Hibernate整合Spring


       1.使用hibernate.cfg.xml原生配置


              hibernate.cfg.xml同原生一样编写

              Spring主配置文件applicationContext中,引入hibernate.cfg.xml

             使用SchemaExport生成数据库表的代码同上一致。

Spring applicationContext.xml

<bean id="sessionFactory"

   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

      <property name="configLocation"

        value="file:src/hibernate.cfg.xml">

      </property>

</bean>


       2.不使用hibernate.cfg.xml,Spring的主配置文件applicationContext.xml中配置


              完全不编写hibernate.cfg.xml,全部都在applicationContext.xml中配置   

ClassPathResource ac = new ClassPathResource("applicationContext.xml");

      XmlBeanFactory xbf = new XmlBeanFactory(ac);

      //注意: &sessionFactory ,一定要包含 & ,不加Spring返回的是Hibernate下的SessionFactoryImpl类

      LocalSessionFactoryBean lsfb=(LocalSessionFactoryBean) xbf.getBean("&sessionFactory");

      Configuration cfg=lsfb.getConfiguration();

      SchemaExport export=new SchemaExport(cfg);

      export.create(true, false);



<!-- 配置数据源-->

   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

      <property name="driverClassName" value="${jdbc.driverClassName}"/>

        <property name="url" value="${jdbc.url}"/>

        <property name="username" value="${jdbc.username}"/>

        <property name="password" value="${jdbc.password}"/>

   </bean>

  

   <!-- 配置sessionfactory,该配置替代了hibernate.cfg.xml的配置 -->

   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

      <property name="dataSource" ref="dataSource"></property>

      <property name="mappingResources">

        <list>

           <value>xxx/xxx/model/User.hbm.xml</value>

        </list>

      </property>

      <property name="hibernateProperties">

        <props>

           <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

           <prop key="hibernate.show_sql">true</prop>

           <prop key="hibernate.format_sql">true</prop>

        </props>

      </property>

   </bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值