letmejustfixthat
letmejustfixthat

Reputation: 3459

Creating Java Boilerplate Code with IntelliJ

Is it possible to create a template/live template with IntelliJ to create the full stack of usual boilerplate for a Domain Object?

Let me give you an example: A usual structure in in a backend could look something like this:

  1. Define a functional domain object: Foobar
  2. Create the entity FoobarEntity:

    @Entity @Table(name="foobar") @Getter @Setter 
    public class FoobarEntity implements Persistable<Long> {
        @Id
        private Long id;
        @Column
        private String someData;
        @Column
        private String someMoreData;
    }
    
  3. Now the boilerplate party starts, create data transfer objects, data access objects, services, ...: Create FoobarDto (to get started), Interface FoobarDao (CRUD) and default implementation FoobarDaoJpa, Interface FoobarService (CRUD) and default implementation FoobarServiceImpl, a mapper to map from Entity to Dto FoobarDtoMapper, maybe a Spring config FoobarConfig, maybe a filter object to search FoobarSearchFilter, maybe some more Classes for a REST api like FoobarRessource, FoobarController, ...

Some further considerations: More annotations (like @Service or something like that) would be somehow useless since the all the classes start with the same code base (like add, delete, edit, load methods for a service and a dao) but, however, will grow in the further process of development.

Is this somehow possible with IntelliJ (or another tool you know)?

Upvotes: 0

Views: 1799

Answers (1)

baao
baao

Reputation: 73281

You can create entities like that with hibernate plugin. It creates entities according to your table structure. Just add hibernate framework to your project (in linux, press Ctrl + Shift + a, then type hibernate and select add hibernate framework), then you'll get a task window like that:

enter image description here

Now right click on your projects name (will be different in your case) and select Generate Persistence Mapping > By Database Schema.

Now a window will open and you can select the tables you want to create an entity for.

Note that you need to have set up your database in idea to make this work.

For your third point, use file templates. Again, press Ctrl + Shift + a, but then type file template - create the templates once and just use them...

Upvotes: 1

Related Questions