Advertisement
sipamski

CRUD

May 21st, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 KB | None | 0 0
  1. #1 HomeController
  2.  
  3. package com.xsis.batch197.controller;
  4.  
  5. import java.util.List;
  6.  
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.ui.Model;
  10. import org.springframework.web.bind.annotation.ModelAttribute;
  11. import org.springframework.web.bind.annotation.PathVariable;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13.  
  14. import com.xsis.batch197.model.BiodataModel;
  15. import com.xsis.batch197.repository.BiodataRepo;
  16.  
  17. @Controller
  18. public class HomeController {
  19. @Autowired
  20. private BiodataRepo repo;
  21.  
  22. @RequestMapping(value="/home/index")
  23. public String index() {
  24. return "/home/index";
  25. }
  26.  
  27. @RequestMapping(value="/home/save")
  28. public String save(@ModelAttribute BiodataModel biodata) {
  29. repo.save(biodata);
  30. return "redirect:/home/list";
  31. }
  32. @RequestMapping(value="/home/list")
  33. public String list(Model kirim) {
  34. List<BiodataModel> biodataList = repo.findAll();
  35. kirim.addAttribute("biodataList", biodataList);
  36. return "home/list";
  37. }
  38.  
  39. @RequestMapping(value="/home/edit/{id}")
  40. public String edit(Model kirim, @PathVariable(name = "id") Integer id) {
  41. BiodataModel biodataEdit=repo.findById(id).orElse(null);
  42. kirim.addAttribute("biodataEdit", biodataEdit);
  43. return "home/edit";
  44. }
  45. @RequestMapping(value="/home/hapus/{id}")
  46. public String hapus(@PathVariable(name = "id") Integer id) {
  47. BiodataModel biodataEdit=repo.findById(id).orElse(null);
  48. repo.delete(biodataEdit);
  49. return "redirect:/home/list";
  50. }
  51.  
  52. }
  53.  
  54. #2 BiodataModel
  55.  
  56. package com.xsis.batch197.model;
  57.  
  58. import java.util.Date;
  59.  
  60. import javax.persistence.Column;
  61. import javax.persistence.Entity;
  62. import javax.persistence.GeneratedValue;
  63. import javax.persistence.GenerationType;
  64. import javax.persistence.Id;
  65. import javax.persistence.Table;
  66. import javax.persistence.TableGenerator;
  67. import javax.persistence.Temporal;
  68. import javax.persistence.TemporalType;
  69.  
  70. import org.springframework.format.annotation.DateTimeFormat;
  71.  
  72. @Entity
  73. @Table(name = "biodata")
  74. public class BiodataModel {
  75. @Id
  76. @GeneratedValue(strategy = GenerationType.TABLE, generator = "biodata_seq")
  77. @TableGenerator(name = "biodata_seq", table = "tbl_sequences", pkColumnName = "seq_id", valueColumnName = "seq_value", initialValue = 0, allocationSize = 1)
  78. @Column(name = "id")
  79. private int id;
  80.  
  81. @Column(name = "nama", length = 100)
  82. private String nama;
  83.  
  84. @Column(name = "alamat", length = 225)
  85. private String alamat;
  86.  
  87. @Column(name = "tpt_lahir", length = 50)
  88. private String tptLahir;
  89.  
  90. @Column(name = "tgl_lahir")
  91. @Temporal(TemporalType.DATE)
  92. @DateTimeFormat(pattern = "yyyy-MM-dd")
  93. private Date tglLahir;
  94.  
  95. @Column(name = "jk", length = 10)
  96. private String jk;
  97.  
  98. @Column(name = "agama", length = 10)
  99. private String agama;
  100.  
  101. @Column(name = "pekerjaan", length = 100)
  102. private String pekerjaan;
  103.  
  104. @Column(name = "gol_darah", length = 2)
  105. private String golDarah;
  106.  
  107. public int getId() { //MembacaID
  108. return id; //Mengakses id dari get & set
  109. }
  110.  
  111. public void setId(int id) { //MenuliskanID
  112. this.id = id;
  113. }
  114.  
  115. public String getNama() {
  116. return nama;
  117. }
  118.  
  119. public void setNama(String nama) {
  120. this.nama = nama;
  121. }
  122.  
  123. public String getAlamat() {
  124. return alamat;
  125. }
  126.  
  127. public void setAlamat(String alamat) {
  128. this.alamat = alamat;
  129. }
  130.  
  131. public String getTptLahir() {
  132. return tptLahir;
  133. }
  134.  
  135. public void setTptLahir(String tptLahir) {
  136. this.tptLahir = tptLahir;
  137. }
  138.  
  139. public Date getTglLahir() {
  140. return tglLahir;
  141. }
  142.  
  143. public void setTglLahir(Date tglLahir) {
  144. this.tglLahir = tglLahir;
  145. }
  146.  
  147. public String getJk() {
  148. return jk;
  149. }
  150.  
  151. public void setJk(String jk) {
  152. this.jk = jk;
  153. }
  154.  
  155. public String getAgama() {
  156. return agama;
  157. }
  158.  
  159. public void setAgama(String agama) {
  160. this.agama = agama;
  161. }
  162.  
  163. public String getPekerjaan() {
  164. return pekerjaan;
  165. }
  166.  
  167. public void setPekerjaan(String pekerjaan) {
  168. this.pekerjaan = pekerjaan;
  169. }
  170.  
  171. public String getGolDarah() {
  172. return golDarah;
  173. }
  174.  
  175. public void setGolDarah(String golDarah) {
  176. this.golDarah = golDarah;
  177. }
  178.  
  179. }
  180.  
  181. #3 BiodataRepo
  182.  
  183. package com.xsis.batch197.repository;
  184.  
  185. import org.springframework.data.jpa.repository.JpaRepository;
  186. import org.springframework.stereotype.Repository;
  187.  
  188. import com.xsis.batch197.model.BiodataModel;
  189.  
  190. @Repository
  191. public interface BiodataRepo extends JpaRepository<BiodataModel, Integer> {
  192.  
  193. }
  194.  
  195. #4 index.html
  196.  
  197. <html>
  198. <head>
  199. <title>Index</title>
  200. </head>
  201. <body>
  202. <style>
  203. table {
  204. border: 1px solid black;
  205. padding: 20px
  206. }
  207. </style>
  208. <form th:action="@{/home/save}" method="post">
  209. <table>
  210. <caption><h2>Form Biodata</h2></caption>
  211. <tr>
  212. <td>Nama</td>
  213. <td>:</td>
  214. <td><input type="text" name="nama"></td>
  215. </tr>
  216. <tr>
  217. <td>Alamat</td>
  218. <td>:</td>
  219. <td><textarea name="alamat" rows="3" cols="22"></textarea></td>
  220. </tr>
  221. <tr>
  222. <td>Tempat Lahir</td>
  223. <td>:</td>
  224. <td><input type="text" name="tptLahir"></td>
  225. </tr>
  226. <tr>
  227. <td>Tanggal Lahir</td>
  228. <td>:</td>
  229. <td><input type="date" name="tglLahir" max="2019-12-31"></td>
  230. </tr>
  231. <tr>
  232. <td>Jenis Kelamin</td>
  233. <td>:</td>
  234. <td><input type="radio" name="jk" value="Pria">Pria
  235. <input type="radio" name="jk" value="Wanita">Wanita</td>
  236. </tr>
  237. <tr>
  238. <td>Agama</td>
  239. <td>:</td>
  240. <td>
  241. <select name="agama">
  242. <option value="Islam">Islam</option>
  243. <option value="Kristen">Kristen Protestan</option>
  244. <option value="Katolik">Katolik</option>
  245. <option value="Hindu">Hindu</option>
  246. <option value="Budha">Buddha</option>
  247. <option value="Konghucu">Konghucu</option>
  248. </select>
  249. </td>
  250. </tr>
  251. <tr>
  252. <td>Golongan Darah</td>
  253. <td>:</td>
  254. <td><input type="radio" name="golDarah" value="A">A
  255. <input type="radio" name="golDarah" value="B">B
  256. <input type="radio" name="golDarah" value="AB">AB
  257. <input type="radio" name="golDarah" value="O">O</td>
  258. </tr>
  259. <tr>
  260. <td></td>
  261. <td></td>
  262. <td><input type="submit" value="Submit">
  263. <input type="reset" value="Reset"></td>
  264. </tr>
  265. </table>
  266. </form>
  267. </body>
  268. </html>
  269.  
  270. #5 list.html
  271.  
  272. <html>
  273. <body>
  274. <table>
  275. <thead>
  276. <tr>
  277. <th>Nama</th>
  278. <th>Alamat</th>
  279. <th>Tempat Lahir</th>
  280. <th>action</th>
  281. <th>hapus</th>
  282. </tr>
  283. </thead>
  284. <tbody>
  285. <tr th:each="item:${biodataList}">
  286. <td><span th:text="${item.nama}"></span></td>
  287. <td><span th:text="${item.alamat}"></span></td>
  288. <td><span th:text="${item.tptLahir}"></span></td>
  289. <td><a th:href = "@{edit/{id}(id=${item.id})}">edit</a></td>
  290. <td><a th:href = "@{hapus/{id}(id=${item.id})}">hapus</a></td>
  291. </tr>
  292. </tbody>
  293. </table>
  294. </body>
  295. </html>
  296.  
  297. #6 save.html
  298.  
  299. <html>
  300. <body>
  301. <h1>Simpan Berhasil</h1>
  302. </body>
  303. </html>
  304.  
  305. #7 edit.html
  306.  
  307. <html>
  308. <head>
  309. <title>Index</title>
  310. </head>
  311. <body>
  312. <style>
  313. table {
  314. border: 1px solid black;
  315. padding: 20px
  316. }
  317. </style>
  318. <form th:action="@{/home/save}" method="post">
  319. <table>
  320. <caption><h2>Form Biodata</h2></caption>
  321. <input type="hidden" name="id" th:value="${biodataEdit.id}">
  322. <tr>
  323. <td>Nama</td>
  324. <td>:</td>
  325. <td><input type="text" name="nama" th:value="${biodataEdit.nama}"></td>
  326. </tr>
  327. <tr>
  328. <td>Alamat</td>
  329. <td>:</td>
  330. <td><textarea name="alamat" rows="3" cols="22" th:text="${biodataEdit.alamat}"></textarea></td>
  331. </tr>
  332. <tr>
  333. <td>Tempat Lahir</td>
  334. <td>:</td>
  335. <td><input type="text" name="tptLahir"></td>
  336. </tr>
  337. <tr>
  338. <td>Tanggal Lahir</td>
  339. <td>:</td>
  340. <td><input type="date" name="tglLahir" max="2019-12-31"></td>
  341. </tr>
  342. <tr>
  343. <td>Jenis Kelamin</td>
  344. <td>:</td>
  345. <td><input type="radio" name="jk" value="Pria">Pria
  346. <input type="radio" name="jk" value="Wanita">Wanita</td>
  347. </tr>
  348. <tr>
  349. <td>Agama</td>
  350. <td>:</td>
  351. <td>
  352. <select name="agama">
  353. <option value="Islam">Islam</option>
  354. <option value="Kristen">Kristen Protestan</option>
  355. <option value="Katolik">Katolik</option>
  356. <option value="Hindu">Hindu</option>
  357. <option value="Budha">Buddha</option>
  358. <option value="Konghucu">Konghucu</option>
  359. </select>
  360. </td>
  361. </tr>
  362. <tr>
  363. <td>Golongan Darah</td>
  364. <td>:</td>
  365. <td><input type="radio" name="golDarah" value="A">A
  366. <input type="radio" name="golDarah" value="B">B
  367. <input type="radio" name="golDarah" value="AB">AB
  368. <input type="radio" name="golDarah" value="O">O</td>
  369. </tr>
  370. <tr>
  371. <td></td>
  372. <td></td>
  373. <td><input type="submit" value="Submit">
  374. <input type="reset" value="Reset"></td>
  375. </tr>
  376. </table>
  377. </form>
  378. </body>
  379. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement