diff --git a/ANSWER.md b/ANSWER.md index 985fca4..929ad1d 100644 --- a/ANSWER.md +++ b/ANSWER.md @@ -1,62 +1,18 @@ -# Spring Boot基础 +# Spring Data Jpa -## 练习描述 -- 使用Spring boot来完成一个关于Employee的API接口 -- 至少需要完成对所有Employee的一个Get请求,返回一个包含所有Employee的JSON,接口路径范例:`http://localhost:8080/employee` -- 有余力的同学可以尝试完成对Employee的`CURD(增删改查)`全部接口 +### 如何下载 +- 答案详情请点击上方 作业答案下载,来下载 answer.zip 压缩包文件 -## 环境描述 -- java8 -- spring-boot -- Intellij-IDEA +### 如何启动 +- 解压 `answer.zip`,用 IntelliJ IDE 打开 +- 首先本地启动`MySQL server` +- 项目根目录下运行 `./gradlew test`,所有测试均可通过 -## 如何开始 -- 使用如下方式建立Spring boot项目 - - 从`http://start.spring.io/`生成一个项目包,并下载下来,解压,开始编码 -- 使用`./gradlew bootRun`来启动服务器 +### 注意事项 -## 输出规范 -- 仔细阅读上面的练习描述,完成作业 -- 运行项目,启动服务器 -- 使用`Postman`来向目标URL发送请求,获取Response,Response JSON格式如下: -```json -[ - { - "id": 0, - "name": "小明", - "age": 20, - "gender": "男" - }, - { - "id": 1, - "name": "小红", - "age": 19, - "gender": "女" - }, - { - "id": 2, - "name": "小智", - "age": 15, - "gender": "男" - }, - { - "id": 3, - "name": "小刚", - "age": 16, - "gender": "男" - }, - { - "id": 4, - "name": "小霞", - "age": 15, - "gender": "女" - } -] -``` -- 截图,并将图片存放在项目根目录下,截图命名为`result.png`,图片中Response中的Employee必须包括你自己的名字,例如: -![](https://raw.githubusercontent.com/tws-online-quiz/spring-boot-quiz/master/example.png) - -## 题目要求 -- 至少完成`Get`请求 -- 代码通过小步提交,并且每次提交的描述都要有意义 -- 使用快捷键编码 +- 请不要修改项目中mysql的用户名密码,否则测试不通过 + +### 重点讲解 +- 建议按照教程首先完成`entity`的创建 +- 按照 `TDD` 方式开发 +- 实现在 `repository` 中,并配有部分讲解 \ No newline at end of file diff --git a/spring-data-jpa-quiz.iml b/Spring-Data-JPA-Quiz.iml similarity index 84% rename from spring-data-jpa-quiz.iml rename to Spring-Data-JPA-Quiz.iml index 041e1ba..e63b2ed 100644 --- a/spring-data-jpa-quiz.iml +++ b/Spring-Data-JPA-Quiz.iml @@ -1,10 +1,11 @@ - + + diff --git a/Spring-Data-JPA-Quiz_main.iml b/Spring-Data-JPA-Quiz_main.iml new file mode 100644 index 0000000..914e0ec --- /dev/null +++ b/Spring-Data-JPA-Quiz_main.iml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Spring-Data-JPA-Quiz_test.iml b/Spring-Data-JPA-Quiz_test.iml new file mode 100644 index 0000000..1926a7d --- /dev/null +++ b/Spring-Data-JPA-Quiz_test.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index f57f635..1f7ec1a 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ buildscript { springBootVersion = '2.0.0.RELEASE' } repositories { - mavenCentral() + mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") @@ -17,7 +17,7 @@ apply plugin: 'io.spring.dependency-management' apply plugin: 'war' repositories { - mavenCentral() + mavenCentral() } configurations { diff --git a/database.sql b/database.sql new file mode 100644 index 0000000..8a45d94 --- /dev/null +++ b/database.sql @@ -0,0 +1 @@ +create database employee_db; \ No newline at end of file diff --git a/script.sh b/script.sh new file mode 100644 index 0000000..2310c2a --- /dev/null +++ b/script.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +service mysql start +mysql -uroot -proot -e "source ./database.sql" +./gradlew test \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..55cf3c1 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ + +rootProject.name = 'backend' diff --git a/spring-data-jpa-quiz.ipr b/spring-data-jpa-quiz.ipr index d5c70b0..4cc1114 100644 --- a/spring-data-jpa-quiz.ipr +++ b/spring-data-jpa-quiz.ipr @@ -1,8 +1,8 @@ - - $PROJECT_DIR$/build/libs/exploded/spring-data-jpa-quiz.war + + $PROJECT_DIR$/build/libs/exploded/Spring-Data-JPA-Quiz.war TWFuaWZlc3QtVmVyc2lvbjogMS4wDQoNCg== @@ -10,84 +10,84 @@ - + - + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + $PROJECT_DIR$/build/libs - - + + @@ -107,7 +107,9 @@ + + @@ -624,8 +626,8 @@ - - + + @@ -637,699 +639,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/spring-data-jpa-quiz_main.iml b/spring-data-jpa-quiz_main.iml deleted file mode 100644 index 37f70ed..0000000 --- a/spring-data-jpa-quiz_main.iml +++ /dev/null @@ -1,80 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-data-jpa-quiz_test.iml b/spring-data-jpa-quiz_test.iml deleted file mode 100644 index d4a6d1d..0000000 --- a/spring-data-jpa-quiz_test.iml +++ /dev/null @@ -1,97 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/main/java/com/example/employee/entity/Company.java b/src/main/java/com/example/employee/entity/Company.java index cbe26fd..b6847cd 100644 --- a/src/main/java/com/example/employee/entity/Company.java +++ b/src/main/java/com/example/employee/entity/Company.java @@ -2,7 +2,6 @@ import javax.persistence.*; import javax.validation.constraints.NotNull; -import java.util.List; @Entity public class Company { diff --git a/src/main/java/com/example/employee/repository/CompanyRepository.java b/src/main/java/com/example/employee/repository/CompanyRepository.java deleted file mode 100644 index c2339b6..0000000 --- a/src/main/java/com/example/employee/repository/CompanyRepository.java +++ /dev/null @@ -1,14 +0,0 @@ -//package com.example.employee.repository; -// -//import com.example.employee.entity.Company; -//import org.springframework.data.jpa.repository.JpaRepository; -//import org.springframework.data.jpa.repository.Modifying; -//import org.springframework.data.jpa.repository.Query; -//import org.springframework.stereotype.Repository; -//import org.springframework.transaction.annotation.Transactional; -// -//import java.util.List; -// -//@Repository -//public interface CompanyRepository extends JpaRepository { -//} diff --git a/src/main/java/com/example/employee/repository/EmployeeRepository.java b/src/main/java/com/example/employee/repository/EmployeeRepository.java index cc5ce1e..fbcfdf1 100644 --- a/src/main/java/com/example/employee/repository/EmployeeRepository.java +++ b/src/main/java/com/example/employee/repository/EmployeeRepository.java @@ -16,14 +16,14 @@ public interface EmployeeRepository extends JpaRepository { //1.查询名字是*的第一个employee Employee findFirstByName(String name); - //2.找出Employee表中姓名包含`*`字符并且薪资大于*的雇员所有个人信息 + //2.找出Employee表中第一个姓名包含`*`字符并且薪资大于*的雇员个人信息 Employee findFirstByNameLikeAndSalaryIsGreaterThan(String character,Integer salary); //3.找出一个薪资最高且公司ID是*的雇员以及该雇员的姓名 @Query("select e.name from Employee e where e.salary=(select max(t.salary) from Employee t where t.companyId=?1)") String findEmployee(Integer companyId); - //4.实现对Employee的分页查询,每页一个数据 + //4.实现对Employee的分页查询,每页两条数据 Page findAll(Pageable pageable); //5.查找**的所在的公司的公司名称 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 82cdce0..f72844b 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,8 +4,8 @@ spring: datasource: url: jdbc:mysql://localhost:3306/employee_db username: root - password: 123456 - driveClassName: com.mysql.jdbc.Driver + password: root + driver-class-name: com.mysql.jdbc.Driver jpa: show-sql: true hibernate: diff --git a/src/test/java/com/example/employee/CompanyJPATest.java b/src/test/java/com/example/employee/CompanyJPATest.java deleted file mode 100644 index 856c11d..0000000 --- a/src/test/java/com/example/employee/CompanyJPATest.java +++ /dev/null @@ -1,43 +0,0 @@ -//package com.example.employee; -// -//import com.example.employee.entity.Employee; -//import com.example.employee.repository.CompanyRepository; -//import com.example.employee.repository.EmployeeRepository; -//import org.flywaydb.core.Flyway; -//import org.junit.Before; -//import org.junit.Test; -//import org.junit.runner.RunWith; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; -//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -//import org.springframework.test.context.junit4.SpringRunner; -// -//import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -//import static org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace.NONE; -// -//@RunWith(SpringRunner.class) -//@DataJpaTest -//@AutoConfigureTestDatabase(replace = NONE) -//public class CompanyJPATest { -// @Autowired -// private CompanyRepository companyRepository; -// -// @Autowired -// private EmployeeRepository employeeRepository; -// -// @Before -// public void setUp() throws Exception { -// Flyway flyway = new Flyway(); -// flyway.setDataSource("jdbc:mysql://localhost:3306/employee_db","root","123456"); -// flyway.clean(); -// flyway.migrate(); -// } -// -// @Test -// public void should_deleted_employees_in_company_when_given_company_name() throws Exception { -// //1.删除tengxun公司以及其下的所有employee -// companyRepository.deleteByName("tengxun"); -// Employee actualEmployee = employeeRepository.findFirstByName("xiaozhi"); -// assertThat(actualEmployee).isNull(); -// } -//} diff --git a/src/test/java/com/example/employee/EmployeeJPATest.java b/src/test/java/com/example/employee/EmployeeJPATest.java index 5e69806..b826ddb 100644 --- a/src/test/java/com/example/employee/EmployeeJPATest.java +++ b/src/test/java/com/example/employee/EmployeeJPATest.java @@ -30,7 +30,7 @@ public class EmployeeJPATest { @Before public void setUp() throws Exception { Flyway flyway = new Flyway(); - flyway.setDataSource("jdbc:mysql://localhost:3306/employee_db","root","123456"); + flyway.setDataSource("jdbc:mysql://localhost:3306/employee_db","root","root"); flyway.clean(); flyway.migrate(); } @@ -92,13 +92,4 @@ public void should_deleted_employee_when_given_employee_name() throws Exception Employee actualEmployee = employeeRepository.findFirstByName("xiaohong"); assertThat(actualEmployee).isNull(); } - - @Test - public void should_insert_employee_when_given_new_employee() throws Exception { - //7.删除姓名是xiaohong的employee - Employee expectedEmployee = new Employee("xiaohong",19,"female",7000,1, 1); - employeeRepository.deleteByName("xiaohong"); - Employee actualEmployee = employeeRepository.findFirstByName("xiaohong"); - assertThat(actualEmployee).isNull(); - } }