LoginSignup
2
1

More than 3 years have passed since last update.

SpringBootのScaffoldツールを作ってみた

Posted at

はじめに

RailsやLaravelにはScaffoldコマンドがありますが、SpringBootにはなさそうだったので自分で作ってみました。
このツールで作成されるのは、RestController、Service、Repository、Modelです。

準備

  1. こちらからリポジトリをダウンロードします。
  2. ダウンロード後、適当なところに解凍します。
  3. 解凍したフォルダ内にあるbinフォルダをパスに通します。

使い方

  1. SpringBootプロジェクトのルートに移動します。
  2. 以下のコマンドを実行します。
sbcui -p com.example.demo -c "User[id:Long,name:String]"

-c のクラス情報はダブルクォートで囲むのを忘れずに!

実行後、以下のようなファイルが作成されます。
コメント 2019-10-01 144949.png

domain/model/User.java
@Entity
@Table
@Getter
@Setter
@EqualsAndHashCode(of = { "id" })
public class User {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column
    private Long id;
    @Column
    private String name;
}
domain/repository/UserRepository.java
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
domain/service/UserService.java
@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public List<User> findAll(){
        return userRepository.findAll();
    }

    public User find(Long id){
        return userRepository.findById(id).orElse(null);
    }

    public User save(User entity){
        return userRepository.save(entity);
    }

    public void delete(User entity){
        userRepository.delete(entity);
    }
}
app/controller/UserController.java
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public List<User> findAll() {
        return userService.findAll();
    }

    @RequestMapping(method = RequestMethod.GET, value = "{id}")
    public User find(@PathVariable Long id) {
        return userService.find(id);
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<String> create(@Validated @RequestBody User input) {
        userService.save(input);
        return ResponseEntity.ok().build();
    }

    @RequestMapping(method = RequestMethod.PUT, value = "{id}")
    public ResponseEntity<String> update(@PathVariable Long id,
            @Validated @RequestBody User input) {

        User entity = userService.find(id);

        if (Objects.isNull(entity)) {
            return ResponseEntity.badRequest().build();
        }

        userService.save(input);

        return ResponseEntity.ok().build();
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "{id}")
    public ResponseEntity<String> delete(@PathVariable Long id) {

        User entity = userService.find(id);

        if (Objects.isNull(entity)) {
            return ResponseEntity.badRequest().build();
        }

        userService.delete(entity);

        return ResponseEntity.ok().build();
    }
}

おわりに

毎回、手動で1セット作るのが手間だったので、このツールを使って効率化できるかなと。
よかったら、ぜひ、使ってみてください。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1