我是spring-boot的新手,我想创建一个将值插入数据库的应用程序
我已经创建了服务、存储库、配置和控制器
但是我收到这个错误
Parameter 0 of method addProduct in com.oshabz.springboot.repositories.ProductRepository required a bean of type 'java.lang.String' that could not be found.
Action:
Consider defining a bean of type 'java.lang.String' in your configuration.
我的代码
@Repository
public class ProductRepository {
@Autowired
JdbcTemplate jdbcTemplate;
@Bean
public void addProduct(String name) {
String sql = "INSERT INTO product VALUES (NULL, ?)";
jdbcTemplate.update(sql, name);
}
}
@Service
public class ProductService {
@Autowired
ProductRepository productRepository;
public void addProduct(String name) {
productRepository.addProduct(name);
}
}
@RestController
@RequestMapping(path="product")
public class ProductController {
@Autowired
ProductService productService;
@PostMapping(path="/add/{name}")
public void addProduct(@PathVariable String name) {
productService.addProduct(name);
}
}
从addProduct
方法中删除@Bean
注释。该注释应该在方法上的配置类中使用,这些配置类创建bean实例。