【问题标题】:Generate model classes from the database从数据库生成模型类
【发布时间】:2018-06-12 08:58:00
【问题描述】:

我创建了一个 Spring Boot 项目,并希望与在 Docker 容器上运行的 SQL Server 2017 数据库进行通信。

在 Eclipse(Oxygen.2 Release 4.7.2)中为模型类生成代码的最快方法是什么?

如果有帮助,我把我的pom.xmlapplication.properties 放在这里。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.blahblahblah</groupId>
    <artifactId>querytofile</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>querytofile</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

application.properties

spring.datasource.url=jdbc:sqlserver://localhost;databaseName=myDB
spring.datasource.username=sa
spring.datasource.password=BLAHBLAHBLAH
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.hibernate.ddl-auto = create-drop

更新:来自 Marketplace 的快照

【问题讨论】:

  • 您可以使用 JPA 工具从 db 表中生成实体。 eclipse.org/webtools/dali/docs/3.2/user_guide/tasks006.htm
  • 当我在 Project Explorer 中右键单击我的项目时,没有出现“JPA 工具”选项。
  • 您需要从 Eclipse 市场安装插件。
  • 我似乎在市场上找不到名为 JPA Tools 的插件。
  • 请看下面我的回答。

标签: java sql-server eclipse spring-boot jpa


【解决方案1】:

打开 Eclipse IDE:

转到 > 帮助 > eclipse 市场 > 搜索 JPA > 选择 g9 数据库导入,如下所示。

重启eclipse IDE。

右击项目>导入>搜索导入数据库模型如下。

提供必填字段并生成实体。

【讨论】:

  • 不幸的是,当我在 g9 数据库导入上单击“安装”时,它不会显示在我的市场窗口中。当我在市场上搜索 JPA 时,我得到了其他选择。在我点击“浏览更多解决方案”之前,g9 不会出现。
  • 但是它出现了还是什么?
  • @rick 我在市场上搜索“JPA”,它没有出现在初始结果中。所以我一直向下滚动并单击“单击以获取更多解决方案”。这将在 Eclipse Web 浏览器中打开一个选项卡,在其中我可以在搜索结果中看到 g9 数据库导入。所以我点击“安装”。市场打开,结果中没有 g9。这有点令人困惑。
  • 您使用的是哪个 IDE 版本?
  • @rick Oxygen.2 发布 4.7.2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-01
  • 2011-03-23
  • 2018-12-12
  • 2011-02-23
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多