-
Notifications
You must be signed in to change notification settings - Fork 49
Usage
There are several commands the main one is the scaffold
where it generates for you the model, repository, service, controler and the view with thymeleaf.
When running spring scaffold -n "User" -p "name: String email: String"
This command generates all crud operation
you will see.
created src/main/java/com/example/model/UserModel.java
created src/main/java/com/example/repository/UserRepository.java
created src/main/java/com/example/service/UserService.java
created src/main/java/com/example/controller/UserController.java
created src/main/resources/layout.html
created src/main/resources/templates/user/index.html
created src/main/resources/templates/user/form.html
created src/main/resources/templates/user/show.html
with this simple command you already have a complete crud (create, read, update, delete).
see example: Sample
it is also possible to generate a scaffold for api rest:
spring scaffold -n "User" -p "name: String email: String" -r "true"
Are simple and clean generators.
Another very common command is the spring controller -n "Home"
Output:
created src/main/java/com/example/controller/HomeController.java
created src/main/resources//templates/home/index.html
Files generated:
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/home")
public class HomeController {
@GetMapping
public String index() {
return "home/index";
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="layout">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<meta name="description" content="" />
<meta name="author" content="" />
</head>
<body>
<div layout:fragment="content">
<h1>Home</h1>
<hr />
</div>
</body>
</html>
command spring model -n "Admin" -p "name:String email:String"
Output:
created src/main/java/com/example/model/AdminModel.java
@Entity
@Table(name = "admins")
public class AdminModel implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name="name")
private String name;
@Column(name="email")
private String email;
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return this.email;
}
}
command spring repository -n "Admin"
Output:
created src/main/java/com/example/repository/AdminRepository.java
@Repository
public interface AdminRepository { //extends JpaRepository<?, ?> {
}
command spring service -n "Admin"
Output:
created src/main/java/com/example/service/AdminService.java
@Service
@Transactional(readOnly = true)
public class AdminService {
}