1. 程式人生 > >springboot查詢資料顯示結果為[]

springboot查詢資料顯示結果為[]

使用springboot進行條件查詢是,查不到資料,程式碼如下:

service:

public List<User> findByUserName(String userName) {
		return resitory.findByUserName(userName);
	}

controller:

@RequestMapping("/findByUserName")
	public List<User> findByUserName(String UserName) {
		return crudService.findByUserName(UserName);
	}

 repository:

public interface UserResitory extends JpaRepository<User, Integer>{

	List<User> findByUserName(String userName);

}

App:

@SpringBootApplication 
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

Domain:

@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String loginName;
private String userName;
private String password;
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getLoginName() {
	return loginName;
}
public void setLoginName(String loginName) {
	this.loginName = loginName;
}
public String getUserName() {
	return userName;
}
public void setUserName(String userName) {
	this.userName = userName;
}
public String getPassword() {
	return password;
}
public void setPassword(String password) {
	this.password = password;
}
@Override
public String toString() {
	return "User [id=" + id + ", loginName=" + loginName + ", userName=" + userName + ", password=" + password + "]";
}
	
	
}

進行頁面訪問:

表:

查詢結果:

 後臺顯示:

Hibernate: select user0_.id as id1_0_, user0_.login_name as login_na2_0_, user0_.password as password3_0_, user0_.user_name as user_nam4_0_ from user user0_ where user0_.user_name is null

說明我的springboot是正常啟動了的,只是查不到資料

解決方法:檢查訪問路徑是否正確

發現是路徑寫錯了

before:

after:

結果:

小結:在訪問路徑中,?後面的引數名是與controller層的變數名一致,而不是與service中的名字一致!