SPRINGBOOT-SPRINGDATA-JPA Integration

Source: Internet
Author: User
Tags findone

SPRINGBOOT-SPRINGDATA-JPA Integration

https://zhuanlan.zhihu.com/p/35067219

Https://www.cnblogs.com/chenpi/p/6357527.html

53240946

Https://www.cnblogs.com/homeword/p/7442116.html

First, pom.xml configuration

    <!--springdata JPA Integration--    <dependency>            <groupid>org.springframework.boot</groupid >            <artifactId>spring-boot-starter-data-jpa</artifactId>        </dependency>                <!-- MySQL-        <dependency>            <groupId>mysql</groupId>            <artifactId> mysql-connector-java</artifactid>        </dependency>                < using Cache---< in!--springboot         dependency>            <groupId>org.springframework.boot</groupId>            <artifactId> Spring-boot-starter-cache</artifactid>        </dependency>    

Second, create interface inheritance Jparepository

//defining interface Determination types <User,Long> Public InterfaceUserjparepository extends Jparepository<user, long> {    //[1] Custom HQL query database (named query) single table operation@Query (value ="from User u where u.name=:name")     PublicList<user> Findbyname (@Param ("name") String name); //[2] Custom native SQL query database@Query (value ="Select u.* from user u where u.name=?1", Nativequery =true) List<User>findbynamelist (String name); //[3] Custom native SQL query database, return array@Query (value ="Select u.* from user u where u.name=?1", Nativequery =true) object[] Findbynamearr (String name); }

Third, create the service class interface Iuserservice

Package com.tdtk.service;import Java.util.list;import com.tdtk.model.User; Public Interfaceiuserservice{ PublicList<user>FindAll ();  PublicUser FindOne (Long ID);  Public voidsaveuser (user user);  Public voidDeleteUser (Long ID);  PublicList<user>findbyname (String name);  Publicobject[] Findbynamearr (String name); }

IV. Create the implementation class Userserviceimpl, note the annotation @Service @Transactional, inject the userjparepository interface into the implementation class, call the default FindAll method

Package Com.tdtk.service.impl;import Java.util.list;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.service;import Org.springframework.transaction.annotation.transactional;import Com.tdtk.model.user;import Com.tdtk.service.iuserservice;import com.tdtk.service.UserJpaRepository; @Service @transactional Public classUserserviceimpl implements iuserservice{@AutowiredPrivateuserjparepository userjparepository; //[1] calling JPA Default SQL methods (FindAll, GetOne, save, FindOne, delete, Deleteinbatch)     PublicList<user>FindAll () {returnUserjparepository.findall (); } @Override PublicUser FindOne (Long id) {returnUserjparepository.findone (ID); } @Override Public voidsaveuser (user user) {userjparepository.save (user); } @Override Public voidDeleteUser (Long id) {userjparepository.delete (ID); }        //[2] Custom SQL method (Findbyname) returns list@Override PublicList<user>findbyname (String name) {//return Userjparepository.findbyname (name);        returnuserjparepository.findbynamelist (name); }        //[3] Custom SQL Method (Findbynamearr) returns an array@Override Publicobject[] Findbynamearr (String name) {returnUserjparepository.findbynamearr (name); }    }

V. Creation of Usercontroller

Package Com.tdtk.controller;import Java.util.list;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.web.bind.annotation.getmapping;import Org.springframework.web.bind.annotation.RequestParam; Import Org.springframework.web.bind.annotation.restcontroller;import Com.tdtk.model.order;import Com.tdtk.model.user;import Com.tdtk.service.iorderservice;import Com.tdtk.service.IUserService; @RestController  Public classUsercontroller {@AutowiredPrivateIuserservice UserService; @AutowiredPrivateIOrderService OrderService; //[1] Query all@GetMapping (value ="/users/findall")     PublicList<user>FindAll () {returnUserservice.findall (); }        //[2] querying an object//Access Address:http://localhost: 8050/users/findone?id=2@GetMapping (value ="/users/findone")     PublicUser FindOne (@RequestParam Long id) {returnUserservice.findone (ID); }        //[3] Saving an object//Access Address:http://localhost: 8050/users/saveuser?id=2&name=mayun&address= Hangzhou@GetMapping (value ="/users/saveuser")     Publicstring Saveuser (@RequestParam Long ID, @RequestParam string name, @RequestParam string address) {User User =NewUser ();        User.setid (ID);        User.setname (name);        User.setaddress (address);        Userservice.saveuser (user); return "Save Success"; }        //[4] Deleting an object//Access Address:http://localhost: 8050/users/deleteuser?id=2@GetMapping (value ="/users/deleteuser")     PublicString deleteuser (@RequestParam Long id) {userservice.deleteuser (ID); return "Delete Success"; }               //=================================sql Query =================================//Single-table hql query returns list<user>//Access Address:http://localhost: 8050/users/findbyname?name=mayun@GetMapping (value ="/users/findbyname")     PublicList<user>findbyname (@RequestParam String name) {returnuserservice.findbyname (name); }    //Single-table HQL query returns object[] array//Access Address:http://localhost: 8050/users/findbynamearr?name=mayun@GetMapping (value ="/users/findbynamearr")     Publicobject[] Findbynamearr (@RequestParam String name) {returnUserservice.findbynamearr (name); }}

Multi-Table Association query:

1. Create Order Object

@IdPrivateLong ID; @Column (Name="UID")    PrivateLong uid; @Column (Name="pname")    PrivateString pname; @Column (Name="Count")    PrivateLong Count; @Column (Name="desc")    PrivateString desc; 

2, Orderjparepository

 Public Interface Orderjparepository extends Jparepository<order, long> {    //[3] Custom native SQL query database    "select o.* from Orders o,user u where u.id=o.uid and o.pname=?1"true) C12/>list<Order> findbypname (String pname);}

3, create IOrderService, Orderserviceimpl

    // [3] multi-table Association query     @Override    public  list<order> findbypname (String pname) {          return  orderjparepository.findbypname (pname);    }

Online API:

https://docs.spring.io/spring-data/jpa/docs/2.0.6.RELEASE/reference/html/

SPRINGBOOT-SPRINGDATA-JPA Integration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.