使用 Groovy 和 Spring Boot 创建 JPA 应用程序

x33g5p2x  于2022-09-28 转载在 Spring  
字(2.5k)|赞(0)|评价(0)|浏览(413)

您可以在 IDE 中使用 Groovy 创建 Spring Boot 应用程序,也可以使用在线 Spring Boot 应用程序生成器 http://start.spring.io 并选择 Groovy 作为语言。

您现在将看到如何使用 Groovy、Spring Data JPA 和 Thymeleaf 开发一个简单的 Spring Boot Web 应用程序。将 Web、Thymeleaf、JPA 和 H2 启动器依赖项添加到您的应用程序。

spring init --language=groovy -dweb,thymeleaf,jpa,h2 demogroovy

创建一个名为 Customer.groovy 的 JPA 实体:

package com.example.demogroovy;
import javax.persistence.*@Entity @Table(name = "CUSTOMER")
class Customer {
  @Id @GeneratedValue(strategy = GenerationType.AUTO)
  Long id
  String name
  String email
}

当您使用 Groovy 时,您不需要为实体属性创建 setter 和 getter。

为实体创建一个 Spring Data JPA 存储库:

package com.example.demogroovy;

import org.springframework.data.jpa.repository.JpaRepository;

interface CustomerRepository extends JpaRepository<Customer, Long> {
  Customer findByName(String name);
}

接下来,创建一个 SpringMVC 控制器来显示客户列表,如下所示:

package com.example.demogroovy;
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping @Controller class HomeController {
  @Autowired CustomerRepository repo;
  @GetMapping("/") String home(Model model) {
    model.addAttribute("customers", repo.findAll())
    "home"
  }
}

创建 Thymeleaf 视图 src/main/resources/templates/home.html 以呈现客户:

<html xmlns:th="http://www.thymeleaf.org">
     
    <head>
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
             
        <title>Customers List</title>
             
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         
    </head>
     
    <body>
         
        <table>
                 
            <thead>
                     
                <tr>
                             <th>Id</th>         <th>Name</th>     
                </tr>
                     
            </thead>
                 
            <tbody>
                     
                <tr th:each="Customer : ${customers}">
                             <td th:text="${customer.id}">Id</td>         <td th:text="${customer.name}">Name</td>     
                </tr>
                     
            </tbody>
             
        </table>
         
    </body>
     
</html>

生成应用程序时,会创建主入口点类,如下所示:

package com.example.demogroovy;
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication @SpringBootApplication class DemoApplication {
  static void main(String[] args) {
    SpringApplication.run(DemoApplication, args)
  }
}

您可以使用 src/main/resources/data.sql 中的 SQL 脚本使用示例数据初始化数据库

insert into customer(id, name, email) values (1,'john','john@gmail.com'), (2,'frank','frank@gmail.com'), (3,'test','test@gmail.com');

最后,在您的 src/main/resources/application.properties 中包含以下属性,该属性将在启动时自动生成客户表:

spring.jpa.hibernate.ddl-auto = update

现在您可以通过执行以下命令来运行应用程序:

./mvnw spring-boot:run

如果您将浏览器指向 http://localhost:8080/,您应该能够看到客户详细信息。

相关文章

微信公众号

最新文章

更多