从数据库中以随机顺序输出对象,而无需重播

时间:2019-06-08 17:54:08

标签: java spring spring-boot spring-data-jpa thymeleaf

有必要使用spring投票实现Web应用程序。考生通过特殊形式加载到数据库中。当您开始投票时,将显示几个候选人,依此类推,直到所有参与者都结束为止。它们不应重复,并且必须以随机顺序出现在每个用户身上。最后,所有用户的投票结果显示在顶部。目前,候选人不断重复,您无法下载少于或超过10位参与者,我几天都无法解决问题,因此我决定在这里联系。

我在5个不同的页面上显示候选人。有必要推论一种(用一种方法),然后重复的问题应该消失,但是我不知道该怎么做。

投票控制者

@Controller
@RequestMapping("/votePage")
public class VoteController {

@Autowired
CatRepo catRepo;

@Autowired
ListService service;

@RequestMapping("/vote1")
public String votePageOne(Model model) {
    model.addAttribute("cat1", service.listAll().get(0));
    model.addAttribute("cat2", service.listAll().get(1));
    return "vote1";
}

@RequestMapping("/voteFor1")
public String voteForOne(@RequestParam Long id) {
    Cat cat = catRepo.findById((long) id);
    cat.setCount(cat.getCount() + 1);
    catRepo.save(cat);
    return "redirect:/votePage/vote2";
}

@RequestMapping("/vote2")
public String votePageTwo(Model model) {
    model.addAttribute("cat3", service.listAll().get(2));
    model.addAttribute("cat4", service.listAll().get(3));
    return "vote2";
}

@RequestMapping("/voteFor2")
public String voteForTwo(@RequestParam Long id) {
    Cat cat = catRepo.findById((long) id);
    cat.setCount(cat.getCount() + 1);
    catRepo.save(cat);
    return "redirect:/votePage/vote3";
}

@RequestMapping("/vote3")
public String votePageThree(Model model) {
    model.addAttribute("cat5", service.listAll().get(4));
    model.addAttribute("cat6", service.listAll().get(5));
    return "vote3";
}

@RequestMapping("/voteFor3")
public String voteForThree(@RequestParam Long id) {
    Cat cat = catRepo.findById((long) id);
    cat.setCount(cat.getCount() + 1);
    catRepo.save(cat);
    return "redirect:/votePage/vote4";
}

@RequestMapping("/vote4")
public String votePageFour(Model model) {
    model.addAttribute("cat7", service.listAll().get(6));
    model.addAttribute("cat8", service.listAll().get(7));
    return "vote4";
}

@RequestMapping("/voteFor4")
public String voteForFour(@RequestParam Long id) {
    Cat cat = catRepo.findById((long) id);
    cat.setCount(cat.getCount() + 1);
    catRepo.save(cat);
    return "redirect:/votePage/vote5";
}

@RequestMapping("/vote5")
public String votePageFive(Model model) {
    model.addAttribute("cat9", service.listAll().get(8));
    model.addAttribute("cat10", service.listAll().get(9));
    return "vote5";
}

@RequestMapping("/voteFor5")
public String voteForFive(@RequestParam Long id) {
    Cat cat = catRepo.findById((long) id);
    cat.setCount(cat.getCount() + 1);
    catRepo.save(cat);
    return "redirect:/votePage/result";
}

@RequestMapping("/result")
public String result(Model model) {
    List<Integer> numbers = new ArrayList<>();
    for (int i = 0; i < numbers.size(); i++) {
        numbers.add(i);
    }
    model.addAttribute("cats", service.sortList());
    model.addAttribute("numbers", numbers);

    return "result";
}
}

型号

@Entity
@Data
@Table(name = "cats")
public class Cat implements Serializable, Comparable<Cat> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private Integer count;

private String filename;

public Cat() { }

public Cat(String name) {
    this.name = name;
}

@Override
public int compareTo(Cat that) {
    return Long.compare(this.count, that.count);
}
}

服务

@Service
public class ListService {

@Autowired
CatRepo catRepo;

public List<Cat> sortList() {
    return StreamSupport
            .stream(
                    Spliterators.spliteratorUnknownSize(catRepo.findAll().iterator(), Spliterator.NONNULL),
                    false)
            .sorted(Comparator.reverseOrder())
            .collect(Collectors.toList());
}

public List<Cat> listAll() {
    List<Cat> cats = new ArrayList<>();
    catRepo.findAll().forEach(cats::add);
    Collections.shuffle(cats);
    return cats;
}
}

添加控制器

@Controller
@RequestMapping("/add")
public class AddController {

@Autowired
CatRepo catRepo;

@Value("${upload.path}")
private String uploadPath;

@GetMapping("/addCandidate")
public String getAdd() {
    return "upload";
}

@PostMapping("/addCandidate")
public String add(
        @RequestParam String name,
        @RequestParam("file") MultipartFile file,
        Model model
) throws IOException {
    Cat cat = new Cat(name);
    if (file != null && !file.getOriginalFilename().isEmpty()) {
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }
        String uuidFile = UUID.randomUUID().toString();
        String resultFilename = uuidFile + "." + file.getOriginalFilename();
        file.transferTo(new File(uploadPath + "/" + resultFilename));
        cat.setFilename(resultFilename);
    }
    catRepo.save(cat);
    return "upload";
}
}

0 个答案:

没有答案