Skip to content

Commit fc7863d

Browse files
committed
add junit test to data-fast
1 parent 07136c6 commit fc7863d

File tree

7 files changed

+109
-1
lines changed

7 files changed

+109
-1
lines changed

springboot-starter-data-fast/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@
3333
<artifactId>spring-boot-starter-data-jpa</artifactId>
3434
</dependency>
3535

36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>com.h2database</groupId>
44+
<artifactId>h2</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
3648
</dependencies>
3749

3850
</project>

springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/executor/JpaExecutor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import javax.persistence.EntityManager;
99
import java.util.Collection;
10+
import java.util.List;
1011

1112
@AllArgsConstructor
1213
public class JpaExecutor {
@@ -18,7 +19,8 @@ public Object execute(String hql, String countHql, Object[] args, Class<?> retur
1819
JpaQuery query = new JpaQuery(hql, countHql, args, entityManager);
1920

2021
if (returnType.equals(SingleResponse.class)) {
21-
return SingleResponse.of(query.getSingleResult());
22+
List list = (List) query.getResultList();
23+
return list==null&&list.size()>0?SingleResponse.of(list.get(0)):SingleResponse.empty();
2224
}
2325

2426
if (returnType.equals(MultiResponse.class)) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.codingapi.springboot.fast;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class DataFastApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(DataFastApplication.class,args);
11+
}
12+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.codingapi.springboot.fast;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.http.MediaType;
8+
import org.springframework.test.web.servlet.MockMvc;
9+
10+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
11+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
12+
13+
@AutoConfigureMockMvc
14+
@SpringBootTest
15+
public class DataFastApplicationTest {
16+
17+
@Autowired
18+
private MockMvc mockMvc;
19+
20+
@Test
21+
void findAll() throws Exception {
22+
mockMvc.perform(get("/demo/findAll").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
23+
}
24+
25+
@Test
26+
void findById() throws Exception {
27+
mockMvc.perform(get("/demo/getById").param("id","1").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
28+
}
29+
30+
@Test
31+
void findPage() throws Exception {
32+
mockMvc.perform(get("/demo/findPage").param("pageSize","20").param("current","1").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
33+
}
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.codingapi.springboot.fast.entity;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
import javax.persistence.*;
7+
8+
@Setter
9+
@Getter
10+
@Entity
11+
@Table(name = "t_demo")
12+
public class Demo {
13+
@Id
14+
@GeneratedValue(strategy = GenerationType.IDENTITY)
15+
private Integer id;
16+
17+
private String name;
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.codingapi.springboot.fast.query;
2+
3+
import com.codingapi.springboot.fast.annotation.FastController;
4+
import com.codingapi.springboot.fast.annotation.FastMapping;
5+
import com.codingapi.springboot.fast.entity.Demo;
6+
import com.codingapi.springboot.framework.dto.request.PageRequest;
7+
import com.codingapi.springboot.framework.dto.response.MultiResponse;
8+
import com.codingapi.springboot.framework.dto.response.SingleResponse;
9+
import org.springframework.web.bind.annotation.RequestParam;
10+
11+
@FastController
12+
public interface FastDemoQuery {
13+
14+
@FastMapping(value = "select d from Demo d",mapping = "/demo/findAll")
15+
MultiResponse<Demo> findAll();
16+
17+
@FastMapping(value = "select d from Demo d where d.id = ?1",mapping = "/demo/getById")
18+
SingleResponse<Demo> getById(@RequestParam("id") int id);
19+
20+
@FastMapping(value = "select d from Demo d",countQuery = "select count(d) from Demo d",mapping = "/demo/findPage")
21+
MultiResponse<Demo> findPage(PageRequest pageRequest);
22+
23+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
server.port=8088
2+
3+
spring.datasource.driver-class-name=org.h2.Driver
4+
spring.datasource.url=jdbc:h2:file:./test.db
5+
6+
spring.jpa.hibernate.ddl-auto=create-drop
7+
spring.jpa.show-sql=true

0 commit comments

Comments
 (0)