spring.datasource.url=jdbc:mysql://localhost:3306/shop
spring.datasource.username=root
spring.datasource.password=123456
@Entity
@Table(name = "product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
private BigDecimal price;
}
@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
List<Product> findByName(String name);
List<Product> findByPriceLessThan(BigDecimal price);
}
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public List<Product> findProductsByName(String name) {
return productRepository.findByName(name);
}
public List<Product> findProductsByPriceLessThan(BigDecimal price) {
return productRepository.findByPriceLessThan(price);
}
}
@Configuration
@EnableJpaRepositories(basePackages = "com.example.repository")
@EntityScan(basePackages = "com.example.entity")
public class AppConfig {
}