项目GitHub地址
https://github.com/1913045515/JPA
代码
package com.lzq.jpa.entity.repository;
import com.lzq.jpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/**
* Created by qiang on 2018/1/22.
*/
public interface UserRepository extends JpaRepository<User,String>{
/**
* 相当于 select *from user where name=?
* @param name
* @return
*/
public List<User> findByName(String name);
/**
* 相当于select *from user where name like ?
* 但是有一点需要注意的是,%需要我们自己来写
* @param name
* @return
*/
public List<User> findByNameLike(String name);
/**
* 相当于select *from user where name not like ?
* 但是有一点需要注意的是,%需要我们自己来写
* @param name
* @return
*/
public List<User> findByNameNotLike(String name);
/**
* 相当于 select *from user where name <> ?
* @param name
* @return
*/
public List<User> findByNameNot(String name);
/**
* 相当于 select *from user where id in (?)
* @param ids
* @return
*/
public List<User> findByIdIn(List<String> ids);
/**
* 相当于 select *from user where id not in ()
* @param ids
* @return
*/
public List<User> findByIdNotIn(List<String> ids);
/**
* 相当于 select *from user where name=? order by height desc
* @param name
* @return
*/
public List<User> findByNameOrderByHeightDesc(String name);
/**
* 相当于 select *from user where name=? order by height asc
* @param name
* @return
*/
public List<User> findByNameOrderByHeightAsc(String name);
/**
* 相当于 select *from user where name is null
* @return
*/
public List<User> findByNameIsNull();
/**
* 相当于 select *from user where name is not null
* @return
*/
public List<User> findByNameIsNotNull();
/**
* 相当于 select *from user where name =? and height=?
* @param name
* @param height
* @return
*/
public List<User> findByNameAndHeight(String name,int height);
/**
* 相当于 select *from user where name =? or height=?
* @param name
* @param height
* @return
*/
public List<User> findByNameOrHeight(String name,int height);
/**
* 相当于 select *from user where height between ? and ?
* 需要注意的是mysql是有包含两个端点值的
* @param start
* @param end
* @return
*/
public List<User> findByHeightBetween(int start,int end);
/**
* 相当于 select *from user where height < ?
* 需要注意的是mysql是没有包含端点值的
* @param less
* @return
*/
public List<User> findByHeightLessThan(int less);
/**
* 相当于 select *from user where height > ?
* 需要注意的是mysql是没有包含端点值的
* @param greater
* @return
*/
public List<User> findByHeightGreaterThan(int greater);
}
具体语法规则和对应的sql都在代码中给出来了,这边需要和大家说的是UserRepository接口的特点。我们通过继承JpaRepository《对应的实体类,主键属性值》来编写findBy等相关的函数来查询数据库。继承JpaRepository的接口在使用的时候,通过@Autowired会自动创建接口的实现类,不需要怎么去实现这个接口,这也是jpa最方便的地方。
5.通过控制类调用dao接口
package com.lzq.jpa.controller;
import com.lzq.jpa.entity.User;
import com.lzq.jpa.entity.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
/**
* Created by qiang on 2018/1/22.
*/
@Controller
@RequestMapping(value = "jpa")
public class UserController {
@Autowired
private UserRepository userRepository;
@RequestMapping(value = "findByName",method = RequestMethod.GET)
@ResponseBody
public List<User> findByName(String name){
return userRepository.findByName(name);
}
@RequestMapping(value = "findByHeightBetween",method = RequestMethod.GET)
@ResponseBody
public List<User> findByHeightBetween(int start,int end){
return userRepository.findByHeightBetween(start,end);
}
@RequestMapping(value = "findByHeightLessThan",method = RequestMethod.GET)
@ResponseBody
public List<User> findByHeightLessThan(int less){
return userRepository.findByHeightLessThan(less);
}
@RequestMapping(value = "findByHeightGreaterThan",method = RequestMethod.GET)
@ResponseBody
public List<User> findByHeightGreaterThan(int greater){
return userRepository.findByHeightGreaterThan(greater);
}
@RequestMapping(value = "findByNameAndHeight",method = RequestMethod.GET)
@ResponseBody
public List<User> findByNameAndHeight(String name,int height){
return userRepository.findByNameAndHeight(name,height);
}
@RequestMapping(value = "findByNameOrHeight",method = RequestMethod.GET)
@ResponseBody
public List<User> findByNameOrHeight(String name,int height){
return userRepository.findByNameOrHeight(name,height);
}
@RequestMapping(value = "findByNameIsNull",method = RequestMethod.GET)
@ResponseBody
public List<User> findByNameIsNull(){
return userRepository.findByNameIsNull();
}
@RequestMapping(value = "findByNameIsNotNull",method = RequestMethod.GET)
@ResponseBody
public List<User> findByNameIsNotNull(){
return userRepository.findByNameIsNotNull();
}
@RequestMapping(value = "findByNameOrderByHeightDesc",method = RequestMethod.GET)
@ResponseBody
public List<User> findByNameOrderByHeightDesc(String name){
return userRepository.findByNameOrderByHeightDesc(name);
}
@RequestMapping(value = "findByNameOrderByHeightAsc",method = RequestMethod.GET)
@ResponseBody
public List<User> findByNameOrderByHeightAsc(String name){
return userRepository.findByNameOrderByHeightAsc(name);
}
@RequestMapping(value = "findByNameLike",method = RequestMethod.GET)
@ResponseBody
public List<User> findByNameLike(String name){
return userRepository.findByNameLike("%"+name+"%");
}
@RequestMapping(value = "findByNameNotLike",method = RequestMethod.GET)
@ResponseBody
public List<User> findByNameNotLike(String name){
return userRepository.findByNameNotLike("%"+name+"%");
}
@RequestMapping(value = "findByNameNot",method = RequestMethod.GET)
@ResponseBody
public List<User> findByNameNot(String name){
return userRepository.findByNameNot(name);
}
@RequestMapping(value = "findByIdIn",method = RequestMethod.GET)
@ResponseBody
public List<User> findByIdIn(String ids){
List<String>list=new ArrayList<>();
String[] idsStr=ids.split(",");
for(String id:idsStr){
list.add(id);
}
return userRepository.findByIdIn(list);
}
@RequestMapping(value = "findByIdNotIn",method = RequestMethod.GET)
@ResponseBody
public List<User> findByIdNotIn(String ids){
List<String>list=new ArrayList<>();
String[] idsStr=ids.split(",");
for(String id:idsStr){
list.add(id);
}
return userRepository.findByIdNotIn(list);
}
}
转载地址
https://blog.csdn.net/linzhiqiang0316/article/details/79177077