如何更正sql语句以给出正确的结果

时间:2018-11-18 13:41:54

标签: mysql spring-boot

我正在端口3306上运行具有模式“ sys”的本地MySQL Server,该模式具有表“ users” Table in sys schema

现在,我有一个小型的Spring Boot应用程序来查询该表中的所有条目。 该表的模型是:

package com.example.databaseneu.model;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Users {

@Id
// @Column(name = "id")
private int id;
// @Column(name = "name")
private String name;
// @Column(name = "salary")
private int salary;
// @Column(name = "team_name")
private String team_name;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getSalary() {
    return salary;
}

public void setSalary(int salary) {
    this.salary = salary;
}

public String getTeam_name() {
    return team_name;
}

public void setTeam_name(String team_name) {
    this.team_name = team_name;
}}

连接正常,但是当我得到Whitelabel错误页面时,查询似乎未提供正确的结果。 查询以获取表中的所有元素(由存储库自动生成)

Hibernate: 
select
    users0_.id as id1_0_,
    users0_.name as name2_0_,
    users0_.salary as salary3_0_,
    users0_.team_name as team_nam4_0_ 
from
    users users0_

所以我不确定我是否定义了实体错误或其他所有东西 @Column Tag不能解决问题。

---编辑---

package com.example.databaseneu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.databaseneu.model.Users;
import com.example.databaseneu.repository.UserRepository;

@Controller // This means that this class is a Controller
@RequestMapping(path = "/demo") // This means URL's start with /demo (after Application path)
public class MainController {

@Autowired
private UserRepository userRepository;

@GetMapping(path = "/add")
public @ResponseBody String addNewUser(@RequestParam String name, @RequestParam int salary,
        @RequestParam String team_name) {
    Users n = new Users();
    n.setName(name);
    n.setSalary(salary);
    n.setTeam_name(team_name);
    userRepository.save(n);
    return "Saved";
}

@GetMapping(path = "/all")
public Iterable<Users> getAllUsers() {
    return userRepository.findAll();
}}

所以id导航到localhost:8080 / demo / all

1 个答案:

答案 0 :(得分:2)

您已经为一件事写了所有正确的期望。用$itemList = Item::orderBy('id','desc')->take(100)->get(); foreach($itemList as &$item){ // pass by reference since you are modifying within foreach //increment to items $item->increment('views'); //increment to items_daily_views $dailyViews = ItemDailyViews::firstOrCreate( ['item_id'=>$item->id, 'date'=>Carbon::now()->format('Y-m-d')] ); $dailyViews->increment('views'); } return view('home',['itemList',$itemList]); 注释标记您的返回类型,类似于您的@ResponseBody方法。

addNewUser

希望这应该可行。如果仍然遇到问题,请在此处发布。