使用Spring Data JPA初始化存储库

时间:2018-07-18 09:09:03

标签: java spring hibernate spring-data-jpa

image attached after answer from Lorelorelore

基本上,我尝试在项目中使用休眠模式,但是它变得非常混乱,因此我决定再次对其进行测试,因此创建了一个新的Spring项目。我有一个POJO-汽车类和CarRepository。据我了解,它应该使用CRUD存储库中的基本方法-但是,当我想保存一个对象(或使用任何其他方法)时,它根本不起作用(它表明我应该初始化变量“ carRepository”) 。你能帮我吗?预先感谢

//CARTESTER CLASS

public class CarTester {
    public static void main(String[] args) {

        @Autowired
        CarRepository carRepository;
//I want to use a method from crudrepository here
        carRepository.save(new Car(1, "AAA", "BBB", 1111));
        carRepository.findAll();
    }
}

//CAR CLASS
@Table(name = "CARS")
@Entity
    public class Car {

        @Id
        private Integer id;
        private String brand;
        private String model;
        private int manufactureYear;
//constructors, getters, setters, toString()

//CARREPOSITORY CLASS
@Repository
public interface CarRepository extends CrudRepository<Car, Integer> {


}

1 个答案:

答案 0 :(得分:0)

您的测试类应如下所示:

import scrapy
from scrapy.http.request import Request
from indicators.ESGIndicators import ESGIndicators
from scrapy.spiders import CrawlSpider,Rule
from scrapy.linkextractors import LinkExtractor
from lxml import html

class mySpider(scrapy.Spider):
    name = "YALE"
    allowed_domains = ["epi.envirocenter.yale.edu"]
    start_urls = (
        'https://epi.envirocenter.yale.edu/epi-indicator-report/WWT',
        )

    def parse(self, response):
        return Request(
            url='https://epi.envirocenter.yale.edu/epi-indicator-report/WWT',
            callback=self.parse_table
        )

    def parse_table(self,response):
        for tr in response.xpath('//*[@id="block-system-main"]/div/div/div/div[3]/table[2]/tr'):
            item = ESGIndicators()
            item['country'] = tr.xpath('td[1]/a/text()').extract_first()
            item['data1'] = tr.xpath('td[2]/text()').extract()
            item['data2'] = tr.xpath('td[3]/text()').extract()
            item['data3'] = tr.xpath('td[4]/text()').extract()
            item['data4'] = tr.xpath('td[5]/text()').extract()
            print(item)
            yield item