我已经制作了一个名为费用跟踪器的项目的基本结构,但我遇到了错误。尽管我已经尝试解决它很长时间了,但它仍然存在。
错误:
应用程序无法启动
描述:
com.app.expensetracker.service.CategoryServiceImpl 中的字段categoryRepository 需要类型为“com.app.expensetracker.dao.CategoryRepository”的 bean,但无法找到。
注入点有以下注释: - @org.springframework.beans.factory.annotation.Autowired(必需= true)
行动:
考虑在配置中定义“com.app.expensetracker.dao.CategoryRepository”类型的 bean。
类别控制器:
package com.app.expensetracker.controller;
import com.app.expensetracker.dto.CategoryCreateRequest;
import com.app.expensetracker.entity.Category;
import com.app.expensetracker.service.CategoryService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@RestController
@RequestMapping("/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
@PostMapping("/create")
public Category createCategory(@RequestBody @Valid CategoryCreateRequest categoryCreateRequest){
return categoryService.createCategory(categoryCreateRequest);
}
@PutMapping("/update/{catId}")
public Category updateCategory(@PathVariable("catId") String catId, @RequestBody @Valid CategoryCreateRequest categoryCreateRequest){
return categoryService.updateCategory(catId, categoryCreateRequest);
}
@GetMapping("/get/{catId}")
public ResponseEntity<Optional<Category>> getCategoryById(@PathVariable String catId) {
return categoryService.getCategoryById(catId);
}
@PostMapping("/delete/{catId}")
public ResponseEntity<Optional<Category>> deleteCategoryById(@PathVariable String catId) {
return categoryService.deleteCategoryById(catId);
}
}
类别存储库
package com.app.expensetracker.dao;
import com.app.expensetracker.entity.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CategoryRepository extends JpaRepository<Category, String> {
}
类别创建请求
package com.app.expensetracker.dto;
import com.app.expensetracker.entity.Category;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.*;
import java.util.List;
import java.util.UUID;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
public class CategoryCreateRequest {
@NotBlank
private String categoryName;
@Size(min=2,message="Enter 2 users only")
private List<String> users;
public Category to(){
return Category.builder()
.cat_id(UUID.randomUUID().toString())
.categoryName(this.categoryName)
.users(this.users)
.build();
}
}
类别
package com.app.expensetracker.entity;
import jakarta.persistence.*;
import lombok.*;
import java.util.List;
@Entity
@Table(name = "category")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@ToString
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="cat_id",nullable = false)
private String cat_id;
@Column(name="categoryName")
private String categoryName;
@Column(name="users")
private List<String> users;
// @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
// @JoinColumn(name = "user_id")
// private User user;
}
类别服务
package com.app.expensetracker.service;
import com.app.expensetracker.dto.CategoryCreateRequest;
import com.app.expensetracker.entity.Category;
import org.springframework.http.ResponseEntity;
import java.util.Optional;
public interface CategoryService {
Category createCategory(CategoryCreateRequest categoryCreateRequest);
ResponseEntity<Optional<Category>> getCategoryById(String category_id);
Category updateCategory(String categoryId, CategoryCreateRequest categoryCreateRequest);
ResponseEntity<Optional<Category>> deleteCategoryById(String category_id);
}
类别ServiceImpl
package com.app.expensetracker.service;
import com.app.expensetracker.dao.CategoryRepository;
import com.app.expensetracker.dto.CategoryCreateRequest;
import com.app.expensetracker.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
@Service
public class CategoryServiceImpl implements CategoryService{
@Autowired
private CategoryRepository categoryRepository;
@Override
@Transactional
public Category createCategory(CategoryCreateRequest categoryCreateRequest){
Category category = categoryCreateRequest.to();
return categoryRepository.save(category);
}
@Override
@Transactional
public ResponseEntity<Optional<Category>> getCategoryById(String category_id){
Optional<Category> category = categoryRepository.findById(category_id);
if(category == null){
return ResponseEntity.notFound().build();
}
else{
return ResponseEntity.ok(category);
}
}
@Override
@Transactional
public Category updateCategory(String categoryId, CategoryCreateRequest categoryCreateRequest){
Category category = categoryCreateRequest.to();
return categoryRepository.save(category);
}
@Override
@Transactional
public ResponseEntity<Optional<Category>> deleteCategoryById(String category_id){
Optional<Category> category = categoryRepository.findById(category_id);
if(category == null){
return ResponseEntity.notFound().build();
}
else{
categoryRepository.deleteById(category_id);
return ResponseEntity.ok(category);
}
}
}
费用追踪应用程序
package com.app.expensetracker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication //(exclude = {DataSourceAutoConfiguration.class })
//@ComponentScan(basePackages = "com.app.*")
//@EntityScan("com.app.*")
@EnableJpaRepositories(basePackages = "com.app.expensetracker.repository")
public class ExpenseTrackerApplication {
public static void main(String[] args) {
SpringApplication.run(ExpenseTrackerApplication.class, args);
}
}
我想问题来自于
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="cat_id",nullable = false)
private String cat_id;
因为你正在使用
@GeneratedValue(strategy = GenerationType.AUTO)
在
String
字段上,它是错误的,因为 strategy = GenerationType.AUTO
:标识列、序列或表,具体取决于底层数据库,通常是 long
、short
或 int
,而不是 String
。
要修复它,您必须对其进行类似的设置:
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "cat_id", nullable = false)
private String cat_id;