{"id":22307,"date":"2020-11-16T12:55:07","date_gmt":"2020-11-16T12:55:07","guid":{"rendered":"https:\/\/cnsfly.com\/vytcdc\/?p=22307"},"modified":"2024-09-26T11:30:56","modified_gmt":"2024-09-26T11:30:56","slug":"spring-data-jpa","status":"publish","type":"post","link":"https:\/\/cnsfly.com\/vytcdc\/spring-data-jpa\/","title":{"rendered":"Spring Data JPA"},"content":{"rendered":"<p>This Blog describes how you can create crud application using JPA repositories without writing any boilerplate code.<\/p>\n<p>Using Springboot, Spring Data JPA, and Hibernate.<\/p>\n<p>This spring data JPA is like magic which we don\u2019t want to write any code in doing crud operation because it has some inbuilt method just we need to call it.<\/p>\n<p>Findall()<br \/>\nFindbyid()<br \/>\nSave()<br \/>\nDeletebyid()<br \/>\nUpdate<br \/>\nEtc..<\/p>\n<p>Let see the example for creating curd application with spring boot and spring data JPA.<\/p>\n<figure id=\"attachment_23723\" aria-describedby=\"caption-attachment-23723\" style=\"width: 2048px\" class=\"wp-caption alignnone\"><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-23723 size-full\" src=\"https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/11\/blog.jpg\" alt=\"For saving data\" width=\"2048\" height=\"1024\" srcset=\"https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/11\/blog.jpg 2048w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/11\/blog-300x150.jpg 300w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/11\/blog-1024x512.jpg 1024w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/11\/blog-768x384.jpg 768w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/11\/blog-1536x768.jpg 1536w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/11\/blog-600x300.jpg 600w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><figcaption id=\"caption-attachment-23723\" class=\"wp-caption-text\">For saving data<\/figcaption><\/figure>\n<h3><\/h3>\n<h3>Html form<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;%@ page language=\"java\" contentType=\"text\/html; charset=ISO-8859-1\"\r\npageEncoding=\"ISO-8859-1\"%&gt;\r\n   &lt;!DOCTYPE html&gt;\r\n    &lt;html&gt;\r\n    &lt;head&gt;\r\n        &lt;meta charset=\u201cISO-8859-1\u201d&gt;\r\n        &lt;title&gt;Insert title here&lt;\/title&gt;\r\n        &lt;link rel=\u201cstylesheet\u201d href=\u201chttps:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/4.4.1\/css\/bootstrap.min.css\u201d&gt;\r\n        &lt;script src=\u201chttps:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.5.1\/jquery.min.js\u201d&gt;&lt;\/script&gt;\r\n        &lt;script src=\u201chttps:\/\/cdnjs.cloudflare.com\/ajax\/libs\/popper.js\/1.16.0\/umd\/popper.min.js\u201d&gt;&lt;\/script&gt;\r\n        &lt;script src=\u201chttps:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/4.4.1\/js\/bootstrap.min.js\u201d&gt;&lt;\/script&gt;\r\n        &lt;style&gt;\r\n            .bg {\r\n                background: url(\u201cyellow-bokeh-photo-949587.jpg\u201d) no-repeat;\r\n                width: 100%;\r\n                height: 100vh;\r\n            }\r\n            .main-css {\r\n                width: 400px;\r\n                background: #fff;\r\n                margin: auto;\r\n                border-radius: 10px;\r\n                box-shadow: 0px 0px 10px 0px;\r\n                padding: 30px;\r\n                margin-top: 15px;\r\n            }\r\n        &lt;\/style\r\n    &lt;\/head&gt;\r\n    &lt;body class=\u201cbg\u201d&gt;\r\n        &lt;div class=\u201ccontainer \u201c&gt;\r\n            &lt;form action=\u201csavePatient\u201d method=\u201cpost\u201d class=\u201cmain-css\u201d&gt;\r\n                &lt;div class=\u201cform-group\u201d&gt;\r\n                    &lt;label for=\u201cnum\u201d&gt;Patient Name&lt;\/label&gt;\r\n                    &lt;input name=\u201cname\u201d id=\u201cnum\u201d class=\u201cform-control\u201d&gt;\r\n                &lt;\/div&gt;\r\n                &lt;div class=\u201cform-group\u201d&gt;\r\n                    &lt;label for=\u201cnum\u201d&gt;Patient Username Name&lt;\/label&gt;\r\n                    &lt;input name=\u201cusername\u201d id=\u201cnum\u201d class=\u201cform-control\u201d&gt;\r\n                &lt;\/div&gt;\r\n                &lt;div class=\u201cform-group\u201d&gt;\r\n                    &lt;label for=\u201cnum\u201d&gt;Password&lt;\/label&gt;\r\n                    &lt;input name=\u201cpassword\u201d id=\u201cnum\u201d class=\u201cform-control\u201d&gt;\r\n                &lt;\/div&gt;\r\n                &lt;div class=\u201cform-group\u201d&gt;\r\n                    &lt;label for=\u201cac\u201d&gt;Age&lt;\/label&gt;\r\n                    &lt;input name=\u201cage\u201d id=\u201cac\u201d class=\u201cform-control\u201d&gt;\r\n                &lt;\/div&gt;\r\n                &lt;div class=\u201cform-group\u201d&gt;\r\n                    &lt;label for=\u201cdd\u201d&gt;Gender&lt;\/label&gt;\r\n                    &lt;select class=\u201cform-control\u201d name=\u201cgender\u201d&gt;\r\n                        &lt;option&gt;select&lt;\/option&gt;\r\n                        &lt;option value=\u201cmale\u201d&gt;male&lt;\/option&gt;\r\n                        &lt;option value=\u201cfemale\u201d&gt;female&lt;\/option&gt;\r\n                    &lt;\/select&gt;\r\n                &lt;\/div&gt;\r\n                &lt;div class=\u201cform-group\u201d&gt;\r\n                    &lt;label for=\u201coa\u201d&gt;Address&lt;\/label&gt;\r\n                    &lt;input name=\u201caddress\u201d id=\u201coa\u201d class=\u201cform-control\u201d&gt;\r\n                &lt;\/div&gt;\r\n                &lt;div class=\u201cform-group\u201d&gt;\r\n                    &lt;label for=\u201coa\u201d&gt;Mobile Number&lt;\/label&gt;\r\n                    &lt;input name=\u201cmobile\u201d id=\u201coa\u201d class=\u201cform-control\u201d&gt;\r\n                &lt;\/div&gt;\r\n                &lt;div class=\u201cform-group\u201d&gt;\r\n                    &lt;input type=\u201csubmit\u201d name=\u201cpwd\u201d value=\u201cSave\u201d class=\u201cbtn btn-primary btn-block\u201d&gt;\r\n                &lt;\/div&gt;\r\n            &lt;\/form&gt;\r\n        &lt;\/div&gt;\r\n    &lt;\/body&gt;\r\n    &lt;\/html&gt;<\/pre>\n<p><strong>Controller Code<\/strong><br \/>\n@Autowired<br \/>\nPatientrepo ps;<\/p>\n<p>@GetMapping(\u201c\/userRegister\u201d)<br \/>\n<strong>public\u00a0<\/strong>String userRegister() {<br \/>\n<strong>return<\/strong>\u201cregister\u201d;<br \/>\n}<\/p>\n<p>\/\/Saving data<\/p>\n<p>@PostMapping(\u201c\/savePatient\u201d)<br \/>\n<strong>public\u00a0\u00a0<\/strong>String savePatient(Patient patient) {<br \/>\nSystem.<strong><em>out<\/em><\/strong>.println(patient);<br \/>\nps.save(patient);<br \/>\n<strong>return\u00a0<\/strong>\u201clogin\u201d;<br \/>\n}<\/p>\n<p><strong>\/\/Fetching data<\/strong><br \/>\n@GetMapping(\u201c\/dept\u201d)<br \/>\n<strong>public\u00a0<\/strong>String Department(@RequestParam(\u201cid\u201d)\u00a0<strong>int\u00a0<\/strong>theid,Model model) {<br \/>\nSystem.<strong><em>out<\/em><\/strong>.println(theid);<br \/>\nList&lt;Doctors&gt; dlist = dr.findByFlag(theid);<br \/>\nmodel.addAttribute(\u201cdoctorlist\u201d, dlist);<br \/>\n<strong>return<\/strong>\u201cdoctorlist\u201d;<br \/>\n}<\/p>\n<p><strong>Entity Creation<\/strong><br \/>\n@Entity<br \/>\n@Table(name=\u201dpatient\u201d)<br \/>\n<strong>public\u00a0<\/strong><strong>class\u00a0<\/strong>Patient {<\/p>\n<p>@Id<br \/>\n@GeneratedValue(strategy=GenerationType.<br \/>\n<strong><em>AUTO<\/em><\/strong><\/p>\n<p><strong>private<\/strong><\/p>\n<p><strong>int\u00a0<\/strong>id;<br \/>\n<strong>private\u00a0<\/strong>String name;<br \/>\n<strong>private\u00a0<\/strong>String gender;<br \/>\n<strong>private\u00a0<\/strong>String age;<br \/>\n<strong>private\u00a0<\/strong>String address;<br \/>\n<strong>private\u00a0<\/strong>String mobile;<br \/>\n<strong>private\u00a0<\/strong>String username;<br \/>\n<strong>private\u00a0<\/strong>String password;<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026\u2026.<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026.<br \/>\n<strong>Repository Creation<\/strong><\/p>\n<p><strong>import\u00a0<\/strong>org.springframework.data.jpa.repository.JpaRepository;<\/p>\n<p><strong>import\u00a0<\/strong>com.abd.hospitalmgt.entity.Patient;<\/p>\n<p><strong>public\u00a0<\/strong><strong>interface\u00a0<\/strong>Patientrepo\u00a0<strong>extends\u00a0<\/strong>&lt; JpaRepository&lt;Patient, Integer&gt;{<\/p>\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Data JPA This Blog describes how you can create crud application using JPA repositories without writing any boilerplate code. Using Springboot,Spring Data JPA, and Hibernate. This spring data JPA<\/p>\n","protected":false},"author":1,"featured_media":23725,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[77],"tags":[25,26],"class_list":["post-22307","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-design","tag-development"],"_links":{"self":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts\/22307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/comments?post=22307"}],"version-history":[{"count":1,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts\/22307\/revisions"}],"predecessor-version":[{"id":33855,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts\/22307\/revisions\/33855"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/media\/23725"}],"wp:attachment":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/media?parent=22307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/categories?post=22307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/tags?post=22307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}