From d872b4830c8460852903fdd1dbc7ee39c110b979 Mon Sep 17 00:00:00 2001 From: luoxiang <2806718453@qq.com> Date: Thu, 3 Jan 2019 23:06:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20spring-boot-data-jpa=20?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springboot/bean/Programmer.java | 3 +- .../springboot/dao/ProgRepository.java | 12 +-- .../src/main/resources/application.yml | 12 +-- .../heibaiying/springboot/DataJPATests.java | 95 +++++++++++++++++++ .../SpringBootDataJpaApplicationTests.java | 57 ----------- 5 files changed, 108 insertions(+), 71 deletions(-) create mode 100644 spring-boot/spring-boot-data-jpa/src/test/java/com/heibaiying/springboot/DataJPATests.java delete mode 100644 spring-boot/spring-boot-data-jpa/src/test/java/com/heibaiying/springboot/SpringBootDataJpaApplicationTests.java diff --git a/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/bean/Programmer.java b/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/bean/Programmer.java index c4b7fd7..3fc4511 100644 --- a/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/bean/Programmer.java +++ b/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/bean/Programmer.java @@ -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; diff --git a/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/dao/ProgRepository.java b/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/dao/ProgRepository.java index f599bf8..9938017 100644 --- a/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/dao/ProgRepository.java +++ b/spring-boot/spring-boot-data-jpa/src/main/java/com/heibaiying/springboot/dao/ProgRepository.java @@ -12,27 +12,27 @@ import java.util.List; /** * @author : heibaiying - * @description : 查询接口 继承自CrudRepository CrudRepository 默认实现了部分增删改查方法 + * @description : 查询接口继承自CrudRepository,CrudRepository 默认定义了部分增删改查方法 */ public interface ProgRepository extends CrudRepository { /* - *方法名遵循命名规范的查询 更多命名规范可以参考官方文档所列出的这张表格 + * 方法名遵循命名规范的查询 更多命名规范可以参考官方文档所列出的这张表格 * https://docs.spring.io/spring-data/jpa/docs/2.1.3.RELEASE/reference/html/#jpa.query-methods.query-creation */ List findAllByName(String name); /* - *分页查询 + *分页排序查询 */ - List findByAndSort(String name, Sort sort); + Page findAll(Pageable pageable); /* * 占位符查询 */ @Query(value = "select u from Programmer u where u.name = ?1 or u.salary = ?2") - List findByCondition(String name, float salary); + List findByConditionAndOrder(String name, float salary, Sort.Order order); /* @@ -40,5 +40,5 @@ public interface ProgRepository extends CrudRepository { */ @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); } diff --git a/spring-boot/spring-boot-data-jpa/src/main/resources/application.yml b/spring-boot/spring-boot-data-jpa/src/main/resources/application.yml index 131e815..504d2a7 100644 --- a/spring-boot/spring-boot-data-jpa/src/main/resources/application.yml +++ b/spring-boot/spring-boot-data-jpa/src/main/resources/application.yml @@ -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 \ No newline at end of file diff --git a/spring-boot/spring-boot-data-jpa/src/test/java/com/heibaiying/springboot/DataJPATests.java b/spring-boot/spring-boot-data-jpa/src/test/java/com/heibaiying/springboot/DataJPATests.java new file mode 100644 index 0000000..eb6c0b2 --- /dev/null +++ b/spring-boot/spring-boot-data-jpa/src/test/java/com/heibaiying/springboot/DataJPATests.java @@ -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 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 programmers = repository.findAllByName("pro01"); + programmers.forEach(System.out::println); + + // 传入参数名称 + Programmer param = repository.findByParam("pro02", 22); + System.out.println("findByParam:" + param); + + // 占位符查询 + List byCondition = repository.findByConditionAndOrder("pro03", 3321.34f, Sort.Order.asc("salary")); + System.out.println("byCondition:" + byCondition); + + //条件与分页查询 需要注意的是这里的页数是从第0页开始计算的 + Page 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 = repository.findById(1); + Assert.assertEquals(programmer.get().getName(), "updatePro01"); + } + + + /** + * 删除数据测试 + */ + @Test + public void delete() { + Optional programmer = repository.findById(2); + if (programmer.isPresent()) { + repository.deleteById(2); + } + Assert.assertFalse(programmer.isPresent()); + } + + +} + diff --git a/spring-boot/spring-boot-data-jpa/src/test/java/com/heibaiying/springboot/SpringBootDataJpaApplicationTests.java b/spring-boot/spring-boot-data-jpa/src/test/java/com/heibaiying/springboot/SpringBootDataJpaApplicationTests.java deleted file mode 100644 index 1098526..0000000 --- a/spring-boot/spring-boot-data-jpa/src/test/java/com/heibaiying/springboot/SpringBootDataJpaApplicationTests.java +++ /dev/null @@ -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 programmers = repository.findAllByName("xiaominng"); - programmers.forEach(System.out::println); - // 传入参数名称 - Programmer param = repository.findByParam("xiaolan", 21); - System.out.println("findByParam:" + param); - // 占位符查询 - List byCondition = repository.findByCondition("xiaolan", 347.34f); - System.out.println("byCondition:" + byCondition); - //条件与分页查询 - List programmerList = repository.findByAndSort("xiaominng", Sort.by("salary")); - programmerList.forEach(System.out::println); - } - - @Test - public void delete() { - Optional programmer = repository.findById(4); - if (programmer.isPresent()) { - repository.deleteById(4); - } - } -} -