Provinces.java
package com.spring.inter.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
// @Entity 对实体注释。任何Hibernate映射对象都要有这个注释
// @Id 声明此属性为主键。该属性值可以通过应该自身创建
//实体类
@Entity
@Table(name = "provinces")
public class Provinces {
@Id
private Integer id;
@Column(name = "provinceid")
private Integer provinceid;
@Column(name="province")
private String province;
//需要声明无参数的构造函数
public Provinces(){}
public Provinces(Integer id, Integer provinceid, String province) {
this.id = id;
this.provinceid = provinceid;
this.province = province;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getProvinceid() {
return provinceid;
}
public void setProvinceid(Integer provinceid) {
this.provinceid = provinceid;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
}
package com.spring.inter.controller;
import com.spring.inter.bean.Provinces;
import com.spring.inter.repository.ProvincesRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class ProvincesController {
@Autowired(required = false)
private ProvincesRepository provincesRepository;
// @RequestMapping("queryAll")
@RequestMapping("/provinces")
public List<Provinces> queryAll(){
List<Provinces> list = new ArrayList<Provinces>();
list = provincesRepository.findAll();
return list;
}
}
package com.spring.inter.repository;
import com.spring.inter.bean.Provinces;
import com.spring.inter.bean.LoginBean;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
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 ProvincesRepository extends JpaRepository<Provinces, Integer>{
/\*
\* 查询全部 \* \*/ @Query("select t from Provinces t")
List<Provinces> findAll();
}
package com.spring.inter.starter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/\*\*
\* Spring boot 入口启动程序,sprint boot内置了tomcat \* @author Administrator
\*/
@ComponentScan(basePackages\= {"com.spring.inter.controller"})//扫描组件
@SpringBootApplication
public class Application {
public static void main(String\[\] args) {
SpringApplication.run(Application.class,args);
}
}
代码github地址
https://github.com/wohuifude1...
很明显它告诉你了,错误在
ProvincesController
的第24行,而24行的代码是provincesRepository.findAll()
。因此,错误一定是
provincesRepository
是null,那就看看provincesRepository
为什么是null.那就看看
provincesRepository
是怎么创建的,发现你是在上面autowired
的,那就去看看provincesRepository
这个类的定义。原来这个类是一个
repository
,但是为什么这个repository
没有annotation呢?因此,你需要把
@Repository
给加上,像下面的代码,那么provincesRepository
就不会为null了。