Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaSpring一次初始化hibernate存储库_Java_Spring_Hibernate - Fatal编程技术网

JavaSpring一次初始化hibernate存储库

JavaSpring一次初始化hibernate存储库,java,spring,hibernate,Java,Spring,Hibernate,我遵循官方规定,宣布我的目标如下: 存储库: @Repository public interface PropertyRepository extends CrudRepository<Property, Long> { } 申请书: @SpringBootApplication @ComponentScan({"org.demo*"}) @EnableAutoConfiguration public class Application { public static

我遵循官方规定,宣布我的目标如下:

存储库:

@Repository
public interface PropertyRepository extends CrudRepository<Property, Long> {
}
申请书:

@SpringBootApplication
@ComponentScan({"org.demo*"})

@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

    }
还有一个很短的控制器:

@Controller
@RequestMapping(path = "/demo")
public class PropertyController {
    @Autowired
    private PropertyDao propDao;
    public PropertyController(PropertyRepository repo) {
        propDao = PropertyDao.setRepository(repo);
    }
    @GetMapping(path = "/map")
    public @ResponseBody
    HashMap<String,String> get() {
        return propDao.getPropertiesMap();
    }
@Controller
@RequestMapping(path = "/demo")
public class PropertyController {
    @Autowired
    private MyService myService;

    public PropertyController(MyService myService) {
        this.myService = myService;
    }

    @GetMapping(path = "/map")
    public @ResponseBody
    HashMap<String,String> get() {
        return myService.getPropertiesMap();
    }
}
@控制器
@请求映射(路径=“/demo”)
公共类属性控制器{
@自动连线
私有财产道;
公共财产控制人(财产存储回购){
propDao=PropertyDao.setRepository(repo);
}
@GetMapping(path=“/map”)
公共@ResponseBody
HashMap get(){
返回propDao.getPropertiesMap();
}
PropertyDao在构造函数中设置他的存储库,然后将属性转换为HashMap

我从日志中看到,每次我在控制器上执行请求时,都会调用Hibernate并执行对mysql的查询,就像每次请求时都会创建PropertyController一样

然而,这个属性对象只包含初始值配置,在每次请求时调用db是一个巨大的开销

你有什么解决办法吗

编辑:添加了PropertyDao

@Component
public class PropertyDao {
    public PropertyDao setRepository(PropertyRepository repository) {...}
    public HashMap<String, String> getPropertiesMap(){...}
}
@组件
公营物业道{
公共属性DAO setRepository(属性存储库){…}
公共HashMap getPropertiesMap(){…}
}
创建一个服务(服务是单例的,只创建一次)并将逻辑移到那里。然后从控制器调用服务方法

大概是这样的:

@Service
@Transactional
public class MyService {

    @Autowired
    private PropertyDao propDao;

    public MyService(PropertyRepository repo) {
        propDao = PropertyDao.setRepository(repo);
    }


    HashMap<String,String> getPropertiesMap() {
        return propDao.getPropertiesMap();
    }
}
@服务
@交易的
公共类MyService{
@自动连线
私有财产道;
公共MyService(房地产回购){
propDao=PropertyDao.setRepository(repo);
}
HashMap getPropertiesMap(){
返回propDao.getPropertiesMap();
}
}
在控制器中:

@Controller
@RequestMapping(path = "/demo")
public class PropertyController {
    @Autowired
    private PropertyDao propDao;
    public PropertyController(PropertyRepository repo) {
        propDao = PropertyDao.setRepository(repo);
    }
    @GetMapping(path = "/map")
    public @ResponseBody
    HashMap<String,String> get() {
        return propDao.getPropertiesMap();
    }
@Controller
@RequestMapping(path = "/demo")
public class PropertyController {
    @Autowired
    private MyService myService;

    public PropertyController(MyService myService) {
        this.myService = myService;
    }

    @GetMapping(path = "/map")
    public @ResponseBody
    HashMap<String,String> get() {
        return myService.getPropertiesMap();
    }
}
@控制器
@请求映射(路径=“/demo”)
公共类属性控制器{
@自动连线
私人MyService-MyService;
公共属性控制器(MyService MyService){
this.myService=myService;
}
@GetMapping(path=“/map”)
公共@ResponseBody
HashMap get(){
返回myService.getPropertiesMap();
}
}

注意,您可能更喜欢(使用
@Autowired
)将存储库直接注入dao中,这部分不应由控制器负责。谢谢,它工作得很好!我不知道注解@Service,我将研究它