Merge branch 'master' of github.com:heibaiying/Spring-All-In-One
This commit is contained in:
commit
8dcfd49cb9
@ -19,7 +19,7 @@ import java.util.Date;
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Entity // 表示该类是一个数据库表映射实体
|
||||
@Table(name = "programmer") // 指明对应的数据库表的名称 不指定的默认就是类名
|
||||
@Table(name = "programmer") // 指明对应的数据库表的名称 不指定的默认就是类名
|
||||
public class Programmer {
|
||||
|
||||
/* AUTO: 默认值,主键由程序控制
|
||||
@ -41,6 +41,7 @@ public class Programmer {
|
||||
|
||||
private Date birthday;
|
||||
|
||||
|
||||
public Programmer(String name, int age, float salary, Date birthday) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
|
@ -12,27 +12,27 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 查询接口 继承自CrudRepository CrudRepository 默认实现了部分增删改查方法
|
||||
* @description : 查询接口继承自CrudRepository,CrudRepository 默认定义了部分增删改查方法
|
||||
*/
|
||||
public interface ProgRepository extends CrudRepository<Programmer, Integer> {
|
||||
|
||||
/*
|
||||
*方法名遵循命名规范的查询 更多命名规范可以参考官方文档所列出的这张表格
|
||||
* 方法名遵循命名规范的查询 更多命名规范可以参考官方文档所列出的这张表格
|
||||
* https://docs.spring.io/spring-data/jpa/docs/2.1.3.RELEASE/reference/html/#jpa.query-methods.query-creation
|
||||
*/
|
||||
List<Programmer> findAllByName(String name);
|
||||
|
||||
/*
|
||||
*分页查询
|
||||
*分页排序查询
|
||||
*/
|
||||
List<Programmer> findByAndSort(String name, Sort sort);
|
||||
Page<Programmer> findAll(Pageable pageable);
|
||||
|
||||
|
||||
/*
|
||||
* 占位符查询
|
||||
*/
|
||||
@Query(value = "select u from Programmer u where u.name = ?1 or u.salary = ?2")
|
||||
List<Programmer> findByCondition(String name, float salary);
|
||||
List<Programmer> findByConditionAndOrder(String name, float salary, Sort.Order order);
|
||||
|
||||
|
||||
/*
|
||||
@ -40,5 +40,5 @@ public interface ProgRepository extends CrudRepository<Programmer, Integer> {
|
||||
*/
|
||||
@Query("select u from Programmer u where u.name = :name or u.age = :age")
|
||||
Programmer findByParam(@Param("name") String name,
|
||||
@Param("age") int age);
|
||||
@Param("age") int age);
|
||||
}
|
||||
|
@ -5,10 +5,8 @@ spring:
|
||||
password: root
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
jpa:
|
||||
hibernate:
|
||||
# 表与实体不一样时候,会相应的更新数据库中的表结构,但是存在表中原先的值不会删除
|
||||
ddl-auto: update
|
||||
# Hibernate默认创建的表是myisam引擎,可以用以下方式指定为默认使用innodb创建表
|
||||
database-platform: org.hibernate.dialect.MySQL57Dialect
|
||||
show-sql: true
|
||||
|
||||
hibernate:
|
||||
ddl-auto: update
|
||||
#Hibernate默认创建的表是myisam引擎,可以用以下方式指定为使用innodb创建表
|
||||
database-platform: org.hibernate.dialect.MySQL57Dialect
|
||||
show-sql: true
|
@ -0,0 +1,95 @@
|
||||
package com.heibaiying.springboot;
|
||||
|
||||
import com.heibaiying.springboot.bean.Programmer;
|
||||
import com.heibaiying.springboot.dao.ProgRepository;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class DataJPATests {
|
||||
|
||||
@Autowired
|
||||
private ProgRepository repository;
|
||||
|
||||
|
||||
/**
|
||||
* 保存数据测试
|
||||
*/
|
||||
@Test
|
||||
public void save() {
|
||||
// 保存单条数据
|
||||
repository.save(new Programmer("pro01", 12, 2121.34f, new Date()));
|
||||
// 保存多条数据
|
||||
List<Programmer> programmers = new ArrayList<>();
|
||||
programmers.add(new Programmer("pro02", 22, 3221.34f, new Date()));
|
||||
programmers.add(new Programmer("pro03", 32, 3321.34f, new Date()));
|
||||
programmers.add(new Programmer("pro04", 44, 4561.34f, new Date()));
|
||||
programmers.add(new Programmer("pro01", 44, 4561.34f, new Date()));
|
||||
repository.saveAll(programmers);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询数据测试
|
||||
*/
|
||||
@Test
|
||||
public void get() {
|
||||
|
||||
// 遵循命名规范的查询
|
||||
List<Programmer> programmers = repository.findAllByName("pro01");
|
||||
programmers.forEach(System.out::println);
|
||||
|
||||
// 传入参数名称
|
||||
Programmer param = repository.findByParam("pro02", 22);
|
||||
System.out.println("findByParam:" + param);
|
||||
|
||||
// 占位符查询
|
||||
List<Programmer> byCondition = repository.findByConditionAndOrder("pro03", 3321.34f, Sort.Order.asc("salary"));
|
||||
System.out.println("byCondition:" + byCondition);
|
||||
|
||||
//条件与分页查询 需要注意的是这里的页数是从第0页开始计算的
|
||||
Page<Programmer> page = repository.findAll(PageRequest.of(0, 10, Sort.Direction.DESC, "salary"));
|
||||
page.get().forEach(System.out::println);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新数据测试
|
||||
*/
|
||||
@Test
|
||||
public void update() {
|
||||
// 保存主键相同的数据就认为是更新操作
|
||||
repository.save(new Programmer(1, "updatePro01", 12, 2121.34f, new Date()));
|
||||
Optional<Programmer> programmer = repository.findById(1);
|
||||
Assert.assertEquals(programmer.get().getName(), "updatePro01");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除数据测试
|
||||
*/
|
||||
@Test
|
||||
public void delete() {
|
||||
Optional<Programmer> programmer = repository.findById(2);
|
||||
if (programmer.isPresent()) {
|
||||
repository.deleteById(2);
|
||||
}
|
||||
Assert.assertFalse(programmer.isPresent());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,57 +0,0 @@
|
||||
package com.heibaiying.springboot;
|
||||
|
||||
import com.heibaiying.springboot.bean.Programmer;
|
||||
import com.heibaiying.springboot.dao.ProgRepository;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.JpaSort;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringBootDataJpaApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private ProgRepository repository;
|
||||
|
||||
@Test
|
||||
public void save() {
|
||||
repository.save(new Programmer("xiaoxiao", 12, 2121.34f, new Date()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
// 遵循命名规范的查询
|
||||
List<Programmer> programmers = repository.findAllByName("xiaominng");
|
||||
programmers.forEach(System.out::println);
|
||||
// 传入参数名称
|
||||
Programmer param = repository.findByParam("xiaolan", 21);
|
||||
System.out.println("findByParam:" + param);
|
||||
// 占位符查询
|
||||
List<Programmer> byCondition = repository.findByCondition("xiaolan", 347.34f);
|
||||
System.out.println("byCondition:" + byCondition);
|
||||
//条件与分页查询
|
||||
List<Programmer> programmerList = repository.findByAndSort("xiaominng", Sort.by("salary"));
|
||||
programmerList.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete() {
|
||||
Optional<Programmer> programmer = repository.findById(4);
|
||||
if (programmer.isPresent()) {
|
||||
repository.deleteById(4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user