上回说到了神奇的一个类建一个表,今天说说怎么写入和查找数据。
如果说写一个类就可以生成一张表是神奇,那么写入数据就是“懒的出奇”。为什么说懒呢?请听下回分解。
玩笑玩笑,不要当真。想要用JPA操作数据就要用到上回说到的JpaRepository接口了。用的时候那可真是“懒”一个字了得。看代码!
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository<Student,Long> {
}
只建了一个接口类继承了JpaRepository,这里面什么都没有,然后基本操作就可以进行了。
测试
还是使用的之前的Student类,重写了一下构造函数和toString()方法
@Override
public String toString(){
return this.getName() + "-" + this.getSex();
}
public Student(String name,String sex){
this.name = name;
this.sex = sex;
}
测试上面直接调用save方法,数据就保存了。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
public class StudentTest {
@Autowired
StudentRepository repository;
@Test
public void addStudent(){
Student s = new Student("张三","男");
repository.save(s);
}
@Test
public void addMoreStudents(){
List<Student> list = new ArrayList<Student>();
Student s = new Student("李四","女");
Student s1 = new Student("钱三一","男");
Student s2 = new Student("林妙妙","女");
Student s3 = new Student("江天昊","男");
Student s4 = new Student("邓小琪","女");
list.add(s);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
repository.saveAll(list);
list.forEach(obj->System.out.println(obj.toString()));
}
}
这里用了《少年派》四位的大名,不侵权吧?如若侵权,劳烦说一声,我改我改。
就问你这是不是“懒”得出奇。 基本啥也没干数就上去了。
取出来呢还是遇到点波折。
先是提示我没有默认的构造函数
No default constructor for entity: : cc.lyw.qrtest.model.entity.Student
这个sql语句都出来了,然后说我没有构造函数,其实是有一个的,但是由于重写了构造函数,Java默认的空的构造函数就会失效,导致了这种报错。所以需要加上默认的构造函数。
public Student()
{
}
然后就没有然后了。
个人对JPA的感觉就是神奇,我就觉得我什么都没干,一切就好了,脸上写着一个大写的懵字。
这还没完,离谱的还有
PageAndSortingRepository
这个连分页和排序也准备好了。
排序代码示例
@Test
public void sortStudent(){
List<Sort.Order> orders = new ArrayList<>();
orders.add(new Sort.Order(Sort.Direction.DESC,"name"));
List<Student> list = repository.findAll(Sort.by(orders));
list.forEach(obj->System.out.println(obj.toString()));
}
分页代码示例
@Test
public void pageStudent(){
Pageable pageable = PageRequest.of(0,3);
Page<Student> list = repository.findAll(pageable);
list.forEach(obj->System.out.println(obj.toString()));
}
真是一个接口实现万物。
这还不算完,还有花式查询
需要在定义的StudentRepository里面写几个方法名
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface StudentRepository extends JpaRepository<Student,Long> {
List<Student> findByName(String name);
List<Student> findByNameEndingWith(String name);
List<Student> findByIdAndName(Long id,String name);
}
调用实现
@Test
public void findByName(){
List<Student> list = repository.findByName("钱三一");
list.forEach(obj->System.out.println(obj.toString()));
}
@Test
public void findByNameLike(){
List<Student> list = repository.findByNameEndingWith("三一");
list.forEach(obj->System.out.println(obj.toString()));
}
@Test
public void findByIdAndName(){
List<Student> list = repository.findByIdAndName(1L,"张三");
list.forEach(obj->System.out.println(obj.toString()));
}
效果就不截图了
命名规则表
and | findByItem1AndItem2(type item1,type item2) | |
or | findByItem1OrItem2(type item1,type item2) | |
= | findByItem1(type item1) findByItem1Is(type item1) | |
Between | findByItem1Between(type item1,type item2) | |
< | findByItemGreaterThan() | |
<= | findByItemLessThanEqual() | |
> | findByItemGreaterThan() | |
>= | findByItemGreaterThanEqual() | |
is null | findByItemIsNull() | |
is not null | findByItemIsNotNull() | |
like | findByItemLike() | |
not like | findByItemNotLike() | |
like'%xxx' | findByItemBegin() | |
like 'xxx%' | findByItemEndingWithing() | |
order by | findByItemOrderByItemDesc() | |
<> | findByItemNot() | |
in() | findByItemIn(Collection<type> item) | |
not in() | findByItemNotIn(Collection<type> item) | |
true | findByItemTrue() | |
false | findByItemFalse() |
写好的查询不爽,自己写SQL,没有问题
@Query(value="select * from student",nativeQuery = true)
List<Student> findAllNative();
@Test
public void findNative(){
List<Student> list = repository.findAllNative();
list.forEach(obj->System.out.println(obj.toString()));
}
使用@Query注解,实现sql语言查询。
还有好多查询的办法,我觉得这个阶段我是用不上。等需要再看吧,哈哈!