|
1 | | -# fast-crud |
| 1 | +# fast-crud |
| 2 | +Automatic creation of simple CRUD API of Spring boot and JPA project. |
| 3 | + |
| 4 | +<br> |
| 5 | + |
| 6 | +## How to use |
| 7 | + |
| 8 | +#### Step 1. Add the dependency. |
| 9 | + |
| 10 | +<details> |
| 11 | +<summary>Gradle (build.gradle)</summary> |
| 12 | + |
| 13 | + ``` groovy |
| 14 | +repositories { |
| 15 | + maven { url 'https://jitpack.io' } |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | + ``` groovy |
| 20 | +dependencies { |
| 21 | + implementation 'com.github.ecsimsw:fast-crud:Tag' |
| 22 | +} |
| 23 | +``` |
| 24 | +</details> |
| 25 | + |
| 26 | +<details> |
| 27 | +<summary>Maven (pom.xml)</summary> |
| 28 | + |
| 29 | +``` xml |
| 30 | +<repositories> |
| 31 | + <repository> |
| 32 | + <id>jitpack.io</id> |
| 33 | + <url>https://jitpack.io</url> |
| 34 | + </repository> |
| 35 | +</repositories> |
| 36 | +``` |
| 37 | +``` xml |
| 38 | +<dependency> |
| 39 | + <groupId>com.github.ecsimsw</groupId> |
| 40 | + <artifactId>fast-crud</artifactId> |
| 41 | + <version>Tag</version> |
| 42 | +</dependency> |
| 43 | +``` |
| 44 | + |
| 45 | +</details> |
| 46 | + |
| 47 | +<br> |
| 48 | + |
| 49 | +#### Step 2. Put @CRUD on your entity class. |
| 50 | + |
| 51 | +``` java |
| 52 | +@CRUD |
| 53 | +@Enity |
| 54 | +class Sample { |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +<br> |
| 59 | + |
| 60 | +#### Step 3. Declare JpaRepository with entity name. |
| 61 | + |
| 62 | +``` java |
| 63 | +public interface SampleRepository extends JpaRepository<Sample, Long> {} |
| 64 | +``` |
| 65 | + |
| 66 | +<br> |
| 67 | + |
| 68 | +#### Step 4. That's it. You just made basic CRUD http api bellow. |
| 69 | + |
| 70 | +| |HttpMethod|Path|RequestBody (Json)| |
| 71 | +|----|------|----|-----| |
| 72 | +|save|POST|/{entityName}|O| |
| 73 | +|findAll|GET|/{entityName}|X| |
| 74 | +|findById|GET|/{entityName}/{id}|X| |
| 75 | +|update|PUT|/{entityName}/{id}|O| |
| 76 | +|delete|DELETE|/{entityName}/{id}|X| |
| 77 | + |
| 78 | +<br> |
| 79 | + |
| 80 | +#### Step 5. Example |
| 81 | +``` java |
| 82 | +@CRUD |
| 83 | +@Entity |
| 84 | +public class Sample { |
| 85 | + |
| 86 | + @GeneratedValue |
| 87 | + @Id |
| 88 | + private Long id; |
| 89 | + private String name; |
| 90 | + |
| 91 | + public Sample() { |
| 92 | + } |
| 93 | + |
| 94 | + public Sample(String name) { |
| 95 | + this.name = name; |
| 96 | + } |
| 97 | + |
| 98 | + public Long getId() { |
| 99 | + return id; |
| 100 | + } |
| 101 | + |
| 102 | + public String getName() { |
| 103 | + return name; |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +``` java |
| 109 | +public interface SampleRepository extends JpaRepository<Sample, Long> {} |
| 110 | +``` |
| 111 | + |
| 112 | +``` |
| 113 | +[POST] localhost:8080/sample |
| 114 | +{ |
| 115 | + "name" : "ecsimsw" |
| 116 | +} |
| 117 | +[PUT] localhost:8080/sample/1 |
| 118 | +{ |
| 119 | + "name" : "new_name" |
| 120 | +} |
| 121 | +[GET] localhost:8080/sample |
| 122 | +[GET] localhost:8080/sample/1 |
| 123 | +[DELETE] localhost:8080/sample/1 |
| 124 | +``` |
| 125 | + |
| 126 | +<br> |
| 127 | + |
| 128 | +## Additional features |
| 129 | + |
| 130 | +#### Repository name |
| 131 | + |
| 132 | +You can set your repository bean name in @CRUD with `repositoryBean` parameter. |
| 133 | + |
| 134 | +``` java |
| 135 | +@CRUD(repositoryBean = "anotherName") |
| 136 | +``` |
| 137 | + |
| 138 | +``` java |
| 139 | +public interface AnotherName extends JpaRepository<Sample, Long> {} |
| 140 | +``` |
| 141 | + |
| 142 | +<br> |
| 143 | + |
| 144 | +#### API root path |
| 145 | + |
| 146 | +You can set your api `root path` |
| 147 | + |
| 148 | +``` java |
| 149 | +@CRUD(rootPath = "anotherRoot") |
| 150 | +``` |
| 151 | + |
| 152 | +``` |
| 153 | +/anotherRoot |
| 154 | +/anotherRoot/{id} |
| 155 | +``` |
| 156 | + |
| 157 | +<br> |
| 158 | + |
| 159 | +#### Exclude method |
| 160 | + |
| 161 | +Method can be excluded in @CRUD with `exclude` parameter. |
| 162 | + |
| 163 | +``` java |
| 164 | +@CRUD(exclude = {CrudMethod.UPDATE, CrudMethod.DELETE}) |
| 165 | +``` |
0 commit comments