Day26 springboot基础4 JPA查询

查询方法

Spring Data Jpa通过解析方法名创建查询,框架在进行方法名解析时,会先把方法名多余的前缀截取掉,然后对剩下部分进行解析,第一个By会被用作分隔符来指示实际查询条件的开始。 我们可以在实体属性上定义条件,并将它们与And和Or连接起来,从而创建大量查询:
(链接:https://www.jianshu.com/p/c23c82a8fcfc)
支持的关键字、示例及JPQL片段如下表所示:
在这里插入图片描述

例子:

ProductRepository

package com.ll.repository;

import java.util.List;
import org.springframework.data.repository.CrudRepository;
import com.ll.entity.Product2;

public interface Product2Repository extends CrudRepository<Product2,String> {
    
    //主键类型
	//根据名字查找
	public List<Product2> findByProductName(String name);
	//模糊查询
	public List<Product2> findByProductNameLike(String name);
	//根据名字和价格进行查询
	public List<Product2> findByProductNameAndProductPrice(String name,String price);
}

ProductService

package com.ll.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ll.entity.OrderMaster;
import com.ll.entity.Product2;
import com.ll.repository.OrderMasterRepository;
import com.ll.repository.Product2Repository;

@Service
public class Product2Service {
    
    
	@Resource
	Product2Repository product2Repository;
	//查询所有
	public List<Product2> getAll() {
    
    
		return (List<Product2>) product2Repository.findAll();
	}
	//根据名字查询
	public List<Product2> getByName(String name) {
    
    
		return  product2Repository.findByProductName(name);
	}
	//模糊查询
	public List<Product2> getByLikeName(String name) {
    
    
		return  product2Repository.findByProductNameLike(name);
	}
	//根据名字和价格查询
	public List<Product2> getByNameandPriceame(String name,String price) {
    
    
		return  product2Repository.findByProductNameAndProductPrice(name, price);
	}
}

ProductController

package com.ll.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ll.entity.OrderMaster;
import com.ll.entity.Product2;
import com.ll.service.OrderService;
import com.ll.service.Product2Service;
import com.ll.util.KeyUtil;

@RestController
@RequestMapping("product2")
public class Product2Controller {
    
    
	@Resource
	Product2Service product2Service;
	
	
	@RequestMapping("all")
	public List<Product2> showAll() {
    
    
		
		return product2Service.getAll();
	}
	//  http://localhost:9081/b/product2/all
	
	@RequestMapping("byname")
	public List<Product2> showByName(HttpServletRequest request) {
    
    
		String name=request.getParameter("name");
		
		return product2Service.getByName(name);
	}
	//  http://localhost:9081/b/product2/byname?name=苹果
	
	@RequestMapping("bynamelike")
	public List<Product2> showByNameLike(HttpServletRequest request) {
    
    
		String name=request.getParameter("name");
		
		return product2Service.getByLikeName("%"+name+"%");
		//return product2Service.getByLikeName(name+"%");
	}
	//  http://localhost:9081/b/product2/bynamelike?name=西瓜
	
	@RequestMapping("bynameandprice")
	public List<Product2> getByNameandPriceame(HttpServletRequest request) {
    
    
		String name=request.getParameter("name");
		String price=request.getParameter("p");
		
		return product2Service.getByNameandPrice(name, price);
	}
	//  http://localhost:9081/b/product2/bynameandprice?name=西瓜&p=9
}

猜你喜欢

转载自blog.csdn.net/fggsgnhz/article/details/98937670