我按照本指南将MySql添加到已经存在的SpringBoot项目中,该项目的依赖关系管理处于graddle状态。就在我添加教程中使用的这三个类时,如下所示
主/ JAVA /净/代码/模型/ Users.Java
$(document).ready(function () {
$("#datepicker").kendoDatePicker();
$(".delay").click(function () {
//Container Div
$("#delayActionPanel").toggle("slide");
//Target input-trying to perform a click using jq
$('.k-select').trigger("click");
});
});
和用户存储库为 主/ JAVA /净/代码/回购/ UserRepository.Java package net.code.repo;
package net.code.controller;
import net.code.model.User;
import net.code.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController // 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 // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private UserRepository userRepository;
@GetMapping(path="/add") // Map ONLY GET Requests
public @ResponseBody String addNewUser (@RequestParam String name
, @RequestParam String email) {
// @ResponseBody means the returned String is the response, not a view name
// @RequestParam means it is a parameter from the GET or POST request
User n = new User();
n.setName(name);
n.setEmail(email);
userRepository.save(n);
return "Saved";
}
@GetMapping(path="/all")
public @ResponseBody Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
}
使用webservice控制器 主/ JAVA /净/代码/控制器/ MainController.Java
import net.code.model.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
}
我的班级@SpringBoot 主/ JAVA /净/代码/ App.Java
package net.code.controller;
import net.code.model.User;
import net.code.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController // 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 // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private UserRepository userRepository;
@GetMapping(path="/add") // Map ONLY GET Requests
public @ResponseBody String addNewUser (@RequestParam String name
, @RequestParam String email) {
// @ResponseBody means the returned String is the response, not a view name
// @RequestParam means it is a parameter from the GET or POST request
User n = new User();
n.setName(name);
n.setEmail(email);
userRepository.save(n);
return "Saved";
}
@GetMapping(path="/all")
public @ResponseBody Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
}
但是每次运行应用程序时,我都会收到以下消息
package net.code;
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.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
//@CrossOrigin(origins = "http://127.0.0.1:8080")
@SpringBootApplication
@ComponentScan("net.code")
//@ComponentScan(basePackages = { "net.code","net.code.repo"})
//@RestController
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class App extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
我已经搜索过这样的相关问题 Spring Boot not autowiring @Repository,@RestController in other package doesn't work但无法修复,因为这些链接的建议对我不起作用 我也想在这里尝试接受的解决方案 Consider defining a bean of type 'package' in your configuration [Spring-Boot]但我发现@EnableJpaRepositories
没有这样的包请帮我解决这个问题,因为我一直试图解决这个问题
答案 0 :(得分:0)
此代码的完美工作版本为here on github。您可以查看http://start.spring.io以获取spring-data-jpa的相应gradle版本。