我是Springboot的新手,我通过Sping的“https://spring.io/guides”教程和指南学习。
现在我尝试理解并重做教程:"Accessing data with MySQL"
不幸的是,我在Mavenbuild期间遇到了错误:
启动ApplicationContext时出错。要显示自动配置报告,请在启用“debug”的情况下重新运行应用程序。 2017-12-07 10:26:42.708 ERROR 8100 --- [主要] o.s.b.d.LoggingFailureAnalysisReporter:
申请失败
说明
xxx.controller.GoodController中的字段goodRepository需要一个无法找到的类型为“xxx.repositories.GoodRepository”的bean。
动作:
考虑在配置中定义类型为“xxx.repositories.GoodRepository”的bean。
使用退出代码1完成处理
这是我的代码:
Good.java:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
@Entity
@Data
public class Good {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
public Good() {
}
public void setName(String name) {
this.name = name;
}
}
GoodRepository:
import org.springframework.data.repository.CrudRepository;
import xxx.model.Good;
public interface GoodRepository extends CrudRepository<Good, Long>
{
}
GoodController:
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 xxx.model.Good;
import xxx.repositories.GoodRepository;
@Controller
@RequestMapping(path="/goods")
public class GoodController {
@Autowired
private GoodRepository goodRepository;
@GetMapping(path="/add")
public @ResponseBody String addNewGod( @RequestParam String name)
{
Good god= new Good();
god.setName(name);
goodRepository.save(god);
return "Saved";
}
@GetMapping(path="/all")
public @ResponseBody Iterable<Good> getAllGods()
{
return goodRepository.findAll();
}
}
我的application.properties:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/gccontest
spring.datasource.username=xxx
spring.datasource.password=xxx
此端口上存在mysql-db ...
我错在哪里?
答案 0 :(得分:0)
您需要使用@Repository注释GoodRepository。 请参阅以下答案:Spring Boot Inject CrudRepository into Service
@Repository
public interface GoodRepository extends CrudRepository<Good, Long>{
}
答案 1 :(得分:0)
将您的GoodRepository实现类标记为@ service / @组件并尝试
答案 2 :(得分:0)
我发现了它! ; - )
我必须编辑我的Application.java:
package xxx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,WebMvcAutoConfiguration.class })
@ComponentScan({ "xxx.repositories"})
public class Application
{
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
}
我还没有检查我的功能,但它编译并运行!非常感谢您的提示!!!