码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot-SpringData-JPA集成

时间:2018-07-09 01:11:12      阅读:398      评论:0      收藏:0      [点我收藏+]

标签:link   map   服务类   rest   lan   code   sql查询   spring   override   

SpringBoot-SpringData-JPA集成

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

 

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

 

https://blog.csdn.net/u012373815/article/details/53240946

 

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

 

技术分享图片

 

 技术分享图片

 

一、pom.xml配置

    <!-- springdata jpa整合 -->
    <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>
        
        <!-- Springboot中使用缓存 -->
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>    

二、创建接口继承JpaRepository

//定义接口 确定类型<User,Long>
public interface UserJpaRepository extends JpaRepository<User, Long> {
    //[1]自定义HQL查询数据库(命名查询)单表操作
    @Query(value = "from User u where u.name=:name")
    public List<User> findByName(@Param("name") String name);
    
    //[2]自定义原生sql查询数据库
    @Query(value = "select u.* from user u where u.name=?1", nativeQuery = true)
    List<User> findByNameList(String name);
    
    //[3]自定义原生sql查询数据库,返回数组
    @Query(value = "select u.* from user u where u.name=?1", nativeQuery = true)
    Object[] findByNameArr(String name);
    
}

 

三、创建服务类接口 IUserService

package com.tdtk.service;

import java.util.List;
import com.tdtk.model.User;

public interface IUserService{
    
    public List<User> findAll();

    public User findOne(Long id);
    
    public void saveUser(User user);
   
    public void deleteUser(Long id);

    public List<User> findByName(String name);
   
    public Object[] findByNameArr(String name);
  
}

 

四、创建实现类 UserServiceImpl ,注意注解@Service @Transactional,将UserJpaRepository 接口注入到实现类,调用默认的findAll方法

 

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 class UserServiceImpl implements IUserService{
    @Autowired
    private UserJpaRepository userJpaRepository;
    //[1]调用jpa默认sql方法(findAll、getOne、save、findOne、delete、deleteInBatch)
    public List<User> findAll(){
        return userJpaRepository.findAll();
    }
    
    @Override
    public User findOne(Long id) {
        return userJpaRepository.findOne(id);
    }

    @Override
    public void saveUser(User user) {
        userJpaRepository.save(user);
    }

    @Override
    public void deleteUser(Long id) {
        userJpaRepository.delete(id);
        
    }
    
    //[2]自定义sql方法(findByName)返回list
    @Override
    public List<User> findByName(String name) {
        //return userJpaRepository.findByName(name);
        return userJpaRepository.findByNameList(name);
    }
    
    //[3]自定义sql方法(findByNameArr)返回数组
    @Override
    public Object[] findByNameArr(String name) {
        return userJpaRepository.findByNameArr(name);
    }
    
}

 

五、创建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 class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IOrderService orderService;
    
    //[1]查询所有
    @GetMapping(value = "/users/findAll")
    public List<User> findAll(){
       return userService.findAll();
    }
    
    //[2]查询一个对象
    //访问地址:http://localhost:8050/users/findOne?id=2
    @GetMapping(value = "/users/findOne")
    public User findone(@RequestParam Long id){
        return userService.findOne(id);
    }
    
    //[3]保存一个对象
    //访问地址:http://localhost:8050/users/saveUser?id=2&name=mayun&address=杭州
    @GetMapping(value = "/users/saveUser")
    public String saveUser(@RequestParam Long id,@RequestParam String name,@RequestParam String address){
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setAddress(address);
        userService.saveUser(user);
        return "save success";
    }
    
    //[4]删除一个对象
    //访问地址:http://localhost:8050/users/deleteUser?id=2
    @GetMapping(value = "/users/deleteUser")
    public String deleteUser(@RequestParam Long id){
        userService.deleteUser(id);
        return "delete success";
    }
    
    
    
   // =================================sql查询=================================
    
    //单表HQL查询 返回list<User>
    //访问地址:http://localhost:8050/users/findByName?name=mayun
    @GetMapping(value = "/users/findByName")
    public  List<User> findByName(@RequestParam String name){
        return userService.findByName(name);
    }
    //单表HQL查询 返回Object[]数组
    //访问地址:http://localhost:8050/users/findByNameArr?name=mayun
    @GetMapping(value = "/users/findByNameArr")
    public  Object[] findByNameArr(@RequestParam String name){
        return userService.findByNameArr(name);
    }


}

 

 

多表关联查询:

1、创建 Order对象

    @Id
    private Long id;
    
    @Column(name = "uid")
    private Long uid;
    
    @Column(name = "pname")
    private String pname;
    
    @Column(name = "count")
    private Long count;
    
    @Column(name = "desc")
    private String desc;
    

 

2、OrderJpaRepository

public interface OrderJpaRepository extends JpaRepository<Order, Long> {

    //[3]自定义原生sql查询数据库
    @Query(value = "select o.* from orders o,user u where u.id=o.uid and o.pname=?1", nativeQuery = true)
    List<Order> findByPname(String pname);
}

 

3、创建 IOrderService、OrderServiceImpl

    //[3]多表关联查询
    @Override
    public  List<Order> findByPname(String pname) {
        return orderJpaRepository.findByPname(pname);
    }

 

 

在线api:

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

 

SpringBoot-SpringData-JPA集成

标签:link   map   服务类   rest   lan   code   sql查询   spring   override   

原文地址:https://www.cnblogs.com/gaogaoyanjiu/p/8784373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!