Skip to content

Commit

Permalink
排序接口
Browse files Browse the repository at this point in the history
  • Loading branch information
linkoutBin committed Dec 12, 2018
1 parent 38adad1 commit ca86c2f
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 29 deletions.
24 changes: 18 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!--<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</exclusions>-->
</dependency>
<dependency>
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -68,11 +68,16 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--<dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependency><!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
Expand Down Expand Up @@ -112,13 +117,20 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
Expand Down
10 changes: 0 additions & 10 deletions src/main/docker/Dockerfile

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/java/com/bin/demo/DemoApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/bin/demo/config/JdbcDataSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.bin.demo.config;

/**
* @Author: xingshulin
* @Date: 2018/12/10 下午5:56
* @Description: 数据源配置
* @Version: 1.0
**/
public class JdbcDataSource {
}
22 changes: 19 additions & 3 deletions src/main/java/com/bin/demo/controller/GreetingController.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
package com.bin.demo.controller;

import com.bin.demo.domain.Greeting;
import com.bin.demo.domain.Person;
import com.bin.demo.service.PersonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

@RestController
@Slf4j
public class GreetingController {

private static final String template = "Hello,%s!";
private static final String template = "Hello,%s!,%s";

private final AtomicInteger counter = new AtomicInteger();

@Autowired
private PersonService personService;

@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "world") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
public Greeting greeting(@RequestParam(value = "age", defaultValue = "10") Integer age) {
//List<Person> persons = personService.getAll();
/*List<Long> ids = persons.stream().map(person -> person.getId()).collect(Collectors.toList());
List<Person> personList = personService.getByIds(ids);*/
Person person = personService.getByAge(age);
/*log.info("日志级别:{}", persons.toString());
log.debug("日志: {}", persons.toString());*/
String result = String.format(template, person != null ? person.getName() : "NULL", person != null ? person.toString() : "NULL");
return new Greeting(counter.incrementAndGet(), result);
}
}
125 changes: 121 additions & 4 deletions src/main/java/com/bin/demo/controller/TestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

import com.bin.demo.vo.Param;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@Slf4j
@RequestMapping("test")
@RestController
public class TestController {

private static CountDownLatch countDownLatch;
private static List<Integer> SORT_VALUE = Arrays.asList(1, 5, 2, 4, 7, 3, 9, 6);

@RequestMapping(method = RequestMethod.GET)
public String show(String num) {
for (int i = 0; i < Integer.valueOf(num); i++) {
Expand All @@ -37,4 +48,110 @@ public Object showName(@RequestBody Param param) {

return result;
}

@RequestMapping("/concurrent/{concurrency}")
public void testDb(@PathVariable Integer concurrency) {
int num = concurrency.intValue();
countDownLatch = new CountDownLatch(num);

while (num-- > 0) {
log.info("处理请求号:{}", num);
new Thread(() -> {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
String url = "http://localhost:8080/springbootdemo/greeting";
HttpGet httpGet = new HttpGet();
httpGet.setURI(URI.create(url));
httpGet.setHeader("Accept", "application/json");
httpGet.setHeader("Content-Type", "application/json;charset=utf-8");
countDownLatch.countDown();
try {
countDownLatch.await();
} catch (InterruptedException e) {
log.error("计数等待失败:{}", e);
}
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
byte[] resp = new byte[1024];
InputStream inputStream = httpEntity.getContent();
inputStream.read(resp);

log.info("接收到的返回结果:{}", new String(resp).trim());

} catch (IOException ioe) {
log.error("请求接口异常!{}", ioe);
}
}).start();
}
log.info("主线程执行结束");
}

/**
* 冒泡排序
*
* @return
*/
@RequestMapping("/collection/bubble")
@ResponseBody
public List<Integer> testBubble() {
log.info("原始顺序:{}", SORT_VALUE.toString());
for (int i = 0; i < SORT_VALUE.size(); i++) {
for (int j = 0; j < SORT_VALUE.size() - (i + 1); j++) {
if (SORT_VALUE.get(j) > SORT_VALUE.get(j + 1)) {
Integer temp = SORT_VALUE.get(j);
SORT_VALUE.set(j, SORT_VALUE.get(j + 1));
SORT_VALUE.set(j + 1, temp);
}
}
}
//Collections.reverse(greetings);
return SORT_VALUE;
}

/**
* 选择排序
*
* @return
*/
@RequestMapping("/collection/select")
public List<Integer> testSelect() {
Integer min = 0;
log.info("原始顺序:{}", SORT_VALUE.toString());
for (int i = 0; i < SORT_VALUE.size() - 1; i++) {
min = i;
for (int j = i; j < SORT_VALUE.size(); j++) {
if (SORT_VALUE.get(min) > SORT_VALUE.get(j)) {
min = j;
}
}
if (i != min) {
Integer temp = SORT_VALUE.get(i);
SORT_VALUE.set(i, SORT_VALUE.get(min));
SORT_VALUE.set(min, temp);
}
}
return SORT_VALUE;
}

/**
* 插入排序
*
* @return
*/
@RequestMapping("/collection/insert")
public List<Integer> testInsert() {
log.info("原始顺序:{}", SORT_VALUE.toString());
for (int i = 1; i < SORT_VALUE.size(); i++) {
int j = i;
Integer target = SORT_VALUE.get(j);
//如果a[j]<a[j-1],则将a[j-1]向后移,循环此操作直至前面元素没有比当前target小的
while (j > 0 && target < SORT_VALUE.get(j - 1)) {
SORT_VALUE.set(j, SORT_VALUE.get(j - 1));
j--;
}
//将target赋值给最后移动的元素的原始位置
SORT_VALUE.set(j, target);
}
return SORT_VALUE;
}

}
21 changes: 21 additions & 0 deletions src/main/java/com/bin/demo/dao/PersonDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.bin.demo.dao;


import com.bin.demo.domain.Person;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface PersonDao extends JpaRepository<Person, Integer> {

@Override
List<Person> findAll(Sort sort);

List<Person> findByIdInOrderByAgeDesc(List<Long> ids);

@Query("FROM Person where age = :age")
Person getByAge(@Param("age") Integer a);
}
26 changes: 26 additions & 0 deletions src/main/java/com/bin/demo/domain/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.bin.demo.domain;

import lombok.Data;

import javax.persistence.*;

/**
* @Author: xingshulin
* @Date: 2018/12/10 下午7:13
* @Description: TODO
* @Version: 1.0
**/
@Entity
@Table(name = "t_person")
@Data
public class Person {
@Id
@GeneratedValue
private Long id;

@Column(nullable = false)
private String name;

@Column
private Integer age;
}
15 changes: 15 additions & 0 deletions src/main/java/com/bin/demo/service/PersonService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.bin.demo.service;

import com.bin.demo.domain.Person;

import java.util.List;

public interface PersonService {
Person getById();

List<Person> getAll();

List<Person> getByIds(List<Long> ids);

Person getByAge(Integer age);
}
43 changes: 43 additions & 0 deletions src/main/java/com/bin/demo/service/impl/PersonServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.bin.demo.service.impl;

import com.bin.demo.dao.PersonDao;
import com.bin.demo.domain.Person;
import com.bin.demo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @Author: xingshulin
* @Date: 2018/12/10 下午11:23
* @Description: TODO
* @Version: 1.0
**/
@Service
public class PersonServiceImpl implements PersonService {

@Autowired
private PersonDao personDao;

@Override
public Person getById() {
return null;
}

@Override
public List<Person> getAll() {
return personDao.findAll(Sort.by(Sort.Order.asc("age"), Sort.Order.desc("id")));
}

@Override
public List<Person> getByIds(List<Long> ids) {
return personDao.findByIdInOrderByAgeDesc(ids);
}

@Override
public Person getByAge(Integer age) {
return personDao.getByAge(age);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/bin/demo/utils/Receiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Slf4j
public class Receiver {

@RabbitListener(queues = "${rabbitmq.queueName}")
//@RabbitListener(queues = "${rabbitmq.queueName}")
public void precess(Greeting msg) {
log.info("receive:{}", msg);
}
Expand Down
Loading

0 comments on commit ca86c2f

Please sign in to comment.