|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +title: 'Read Data' |
| 4 | +--- |
| 5 | + |
| 6 | +Dalam arsitektur Spring Boot MVC, operasi **Read** (Membaca Daftar Data) digunakan untuk mengambil seluruh kumpulan data dari database dan menampilkannya kepada pengguna, contoh dalam bentuk tabel atau list. |
| 7 | + |
| 8 | +Untuk contoh kasus pada tutorial ini kita akan menggunakan data `Product` yang nantinya akan di mapping menjadi table ke database MySQL. |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +## 1️⃣ Membuat Entity |
| 13 | + |
| 14 | +Membuat entity `Product.java`: |
| 15 | + |
| 16 | +```java |
| 17 | +import java.math.BigDecimal; |
| 18 | + |
| 19 | +import jakarta.persistence.Column; |
| 20 | +import jakarta.persistence.Entity; |
| 21 | +import jakarta.persistence.GeneratedValue; |
| 22 | +import jakarta.persistence.GenerationType; |
| 23 | +import jakarta.persistence.Id; |
| 24 | +import jakarta.persistence.Table; |
| 25 | +import jakarta.validation.constraints.NotEmpty; |
| 26 | +import jakarta.validation.constraints.NotNull; |
| 27 | + |
| 28 | +@Entity |
| 29 | +@Table(name = "product") |
| 30 | +public class Product { |
| 31 | + |
| 32 | + @Id |
| 33 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 34 | + @Column(name = "id") |
| 35 | + private Long id; |
| 36 | + |
| 37 | + @NotEmpty |
| 38 | + @Column(name = "name") |
| 39 | + private String name; |
| 40 | + |
| 41 | + @NotEmpty |
| 42 | + @Column(name = "description") |
| 43 | + private String description; |
| 44 | + |
| 45 | + @NotNull |
| 46 | + @Column(name = "price") |
| 47 | + private BigDecimal price; |
| 48 | + |
| 49 | + public Long getId() { |
| 50 | + return id; |
| 51 | + } |
| 52 | + |
| 53 | + public void setId(Long id) { |
| 54 | + this.id = id; |
| 55 | + } |
| 56 | + |
| 57 | + public String getName() { |
| 58 | + return name; |
| 59 | + } |
| 60 | + |
| 61 | + public void setName(String name) { |
| 62 | + this.name = name; |
| 63 | + } |
| 64 | + |
| 65 | + public String getDescription() { |
| 66 | + return description; |
| 67 | + } |
| 68 | + |
| 69 | + public void setDescription(String description) { |
| 70 | + this.description = description; |
| 71 | + } |
| 72 | + |
| 73 | + public BigDecimal getPrice() { |
| 74 | + return price; |
| 75 | + } |
| 76 | + |
| 77 | + public void setPrice(BigDecimal price) { |
| 78 | + this.price = price; |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## 2️⃣ Repository Layer |
| 84 | + |
| 85 | +Layer Repository `ProductRepository`. |
| 86 | + |
| 87 | +```java |
| 88 | +import org.springframework.data.jpa.repository.JpaRepository; |
| 89 | + |
| 90 | +import com.timposulabs.model.Product; |
| 91 | + |
| 92 | +public interface ProductRepository extends JpaRepository<Product, Long> { |
| 93 | + |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +## 3️⃣ Service Layer |
| 98 | + |
| 99 | +* `ProductService.java` |
| 100 | + |
| 101 | +```java |
| 102 | +import java.util.List; |
| 103 | + |
| 104 | +import com.timposulabs.model.Product; |
| 105 | + |
| 106 | +public interface ProductService { |
| 107 | + |
| 108 | + List<Product> findAll(); |
| 109 | + |
| 110 | + Product findById(Long id); |
| 111 | + |
| 112 | + Product save(Product product); |
| 113 | + |
| 114 | + void delete(Long id); |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +* Implementasi Service `ProductServiceImpl.java` |
| 119 | + |
| 120 | +```java |
| 121 | +import java.util.List; |
| 122 | + |
| 123 | +import org.springframework.stereotype.Service; |
| 124 | + |
| 125 | +import com.timposulabs.model.Product; |
| 126 | +import com.timposulabs.repository.ProductRepository; |
| 127 | + |
| 128 | +@Service |
| 129 | +public class ProductServiceImpl implements ProductService { |
| 130 | + |
| 131 | + private ProductRepository repository; |
| 132 | + |
| 133 | + public ProductServiceImpl(ProductRepository repository) { |
| 134 | + this.repository = repository; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public List<Product> findAll() { |
| 139 | + return repository.findAll(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public Product findById(Long id) { |
| 144 | + return repository.findById(id) |
| 145 | + .orElseThrow(() -> new RuntimeException("ID Not Found")); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public Product save(Product product) { |
| 150 | + return repository.save(product); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void delete(Long id) { |
| 155 | + repository.deleteById(id); |
| 156 | + } |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +## 4️⃣ Controller |
| 161 | + |
| 162 | +* `ProductController.java` |
| 163 | + |
| 164 | +```java |
| 165 | +import java.util.List; |
| 166 | + |
| 167 | +import org.springframework.stereotype.Controller; |
| 168 | +import org.springframework.ui.Model; |
| 169 | +import org.springframework.web.bind.annotation.GetMapping; |
| 170 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 171 | + |
| 172 | +import com.timposulabs.model.Product; |
| 173 | +import com.timposulabs.service.ProductService; |
| 174 | + |
| 175 | +@Controller |
| 176 | +@RequestMapping("/product") |
| 177 | +public class ProductController { |
| 178 | + |
| 179 | + private ProductService productService; |
| 180 | + |
| 181 | + public ProductController(ProductService productService) { |
| 182 | + this.productService = productService; |
| 183 | + } |
| 184 | + |
| 185 | + @GetMapping("/list") |
| 186 | + public String listProduct(Model model) { |
| 187 | + List<Product> productList = productService.findAll(); |
| 188 | + model.addAttribute("products", productList); |
| 189 | + return "product-list"; |
| 190 | + } |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +## 5️⃣ Membuat View |
| 195 | + |
| 196 | +* Membuat view untuk Product List Page `product-list.html` di direktori dimana di sini saya menambahkan Bootstrap untuk css nya. |
| 197 | + |
| 198 | +```html |
| 199 | +<!DOCTYPE html> |
| 200 | +<html xmlns:th="http://www.thymeleaf.org"> |
| 201 | + <head> |
| 202 | + <title>Belajar Spring CRUD</title> |
| 203 | + <meta charset="utf-8"> |
| 204 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 205 | + <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous"> |
| 206 | + </head> |
| 207 | + <body> |
| 208 | + <div class="container"> |
| 209 | + <h3>Product List</h3> |
| 210 | + <hr /> |
| 211 | + <table class="table table-bordered table-striped"> |
| 212 | + <thead> |
| 213 | + <tr> |
| 214 | + <th scope="col">ID</th> |
| 215 | + <th scope="col">Product Name</th> |
| 216 | + <th scope="col">Description</th> |
| 217 | + <th scope="col">Price</th> |
| 218 | + </tr> |
| 219 | + </thead> |
| 220 | + <tbody> |
| 221 | + <tr th:each="product : ${products}"> |
| 222 | + <th scope="row" th:text="${product.id}"></th> |
| 223 | + <td th:text="${product.name}"></td> |
| 224 | + <td th:text="${product.description}"></td> |
| 225 | + <td th:text="${product.price}"></td> |
| 226 | + </tr> |
| 227 | + </tbody> |
| 228 | + </table> |
| 229 | + </div> |
| 230 | + <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js" integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI" crossorigin="anonymous"></script> |
| 231 | + </body> |
| 232 | +</html> |
| 233 | +``` |
| 234 | + |
| 235 | +## 6️⃣ Menjalankan Aplikasi |
| 236 | + |
| 237 | +Ketika pertama kali dijalankan maka Hibernate akan men-generate entity `Product` ke tabel database MySQL, tapi disini datanya masih kosong. Disini kita akan menambahkan data ke dalam table `product` yang ada di dalam database secara manual. |
| 238 | + |
| 239 | +```sql |
| 240 | +insert into product (name, description, price) values |
| 241 | +('Iphone 16', 'The New Generation Iphone', 15000000), |
| 242 | +('Samsung Galaxy S25', 'Next Generation Android Smartphone', 16000000), |
| 243 | +('Asus ROG Laptop', 'Gaming Laptop', 18000000); |
| 244 | +``` |
| 245 | + |
| 246 | +Melihat data: |
| 247 | + |
| 248 | +```sql |
| 249 | +mysql> select * from product; |
| 250 | ++----+------------------------------------+--------------------+-------------+ |
| 251 | +| id | description | name | price | |
| 252 | ++----+------------------------------------+--------------------+-------------+ |
| 253 | +| 1 | The New Generation Iphone | Iphone 16 | 15000000.00 | |
| 254 | +| 2 | Next Generation Android Smartphone | Samsung Galaxy S25 | 16000000.00 | |
| 255 | +| 3 | Gaming Laptop | Asus ROG Laptop | 18000000.00 | |
| 256 | ++----+------------------------------------+--------------------+-------------+ |
| 257 | +``` |
| 258 | + |
| 259 | +Menjalankan aplikasi Spring Boot: |
| 260 | + |
| 261 | +* Buka terminal di root project. |
| 262 | +* Jalankan dengan perintah Maven: `mvn spring-boot:run`. |
| 263 | +* Buka browser dan akses http://localhost:8080/product/list. |
| 264 | + |
| 265 | + |
| 266 | + |
| 267 | +* Untuk menampilkan data `price` sesuai format mata uang, maka kita dapat memperbaharui `application.properties` dengan menambahkan konfigurasi locale Indonesia: |
| 268 | + |
| 269 | +```properties |
| 270 | +spring.datasource.url=jdbc:mysql://localhost:3306/belajar_spring |
| 271 | +spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver |
| 272 | +spring.datasource.username=root |
| 273 | +spring.datasource.password=rahasia |
| 274 | +spring.jpa.show-sql=true |
| 275 | +spring.jpa.hibernate.ddl-auto=update |
| 276 | + |
| 277 | +# Mengatur locale default ke Indonesia |
| 278 | +spring.web.locale=in_ID |
| 279 | +spring.web.locale-resolver=fixed |
| 280 | +``` |
| 281 | + |
| 282 | +* Update kolom table `price`. |
| 283 | + |
| 284 | +```html |
| 285 | +<td th:text="${#numbers.formatCurrency(product.price)}"></td> |
| 286 | +``` |
| 287 | + |
| 288 | + |
| 289 | + |
| 290 | +:::info |
| 291 | +Source Code: https://github.com/TopekoX/belajar-springboot-mvc-crud/tree/main/01-spring-mvc-crud-list |
| 292 | +::: |
0 commit comments