我正在使用弹簧数据jpa处理弹簧启动应用程序以进行休息服务。我跟随教师并阅读了很多答案,但我无法修复我的休息服务。
这是application.class
package tr.kasim.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EnableJpaRepositories("tr.kasim.Dao")
@EntityScan("tr.kasim.Model")
@ComponentScan({"tr.kasim.Service", "tr.kasim.Application" })
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
这是`restcontroller.class
package tr.kasim.Controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import tr.kasim.Service.PersonelService;
import tr.kasim.Model.Personel;
@RestController
public class STRestController {
@Autowired
public PersonelService personelService;
@RequestMapping(value = "/api/personels", method = RequestMethod.GET)
public ResponseEntity<List<Personel>> getPersonels(){
List<Personel> personels = personelService.findAll();
return ResponseEntity.ok().body(personels);
}
}
`
这是Service.class`
package tr.kasim.Service;
import java.util.List;
import tr.kasim.Model.Personel;
public interface PersonelService {
List<Personel> findAll();
}
`
这是ServiceImplemantion.class
package tr.kasim.Service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tr.kasim.Dao.PersonelDao;
import tr.kasim.Model.Personel;
@Service
public class PersonelServiceImpl implements PersonelService {
@Autowired
private PersonelDao personelDao;
@Override
@Transactional
public List<Personel> findAll() {
return personelDao.findAll();
}
}
这是Dao.class
package tr.kasim.Dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import tr.kasim.Model.Personel;
@Repository
public interface PersonelDao extends JpaRepository<Personel, Long> {
List<Personel> findAll();
}
最后这是我的application.properties
#MySql Connection
spring.datasource.url=jdbc:mysql://localhost:3306/exampleproject?verifyServerCertificate=false&useSSL=true
spring.datasource.username=root
spring.datasource.password=*******
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#Jpa/Hibernate
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
#Logging
logging.file=staffTracking.log
logging.level.org.springframework.web=debug
我不确定componentScan。当我读到答案时,我发现有人提到了它,但我尝试了,但我什么都没有。请告诉我失败的地方。最好的祝福。
我更新了Application.class,现在我可以部署项目但是休息服务还没有工作。
你是怎么尝试ComponentScan
的?这里的问题似乎是你有这样的包结构:
tr.kasim.Application
- Application.java
tr.kasim.Service
- PersonelService.java
- PersonelServiceImpl.java
tr.kasim.Dao
- PersonelDao.java
现在,mainClass
在tr.kasim.Application
中,它将扫描该包内的bean定义(或tr.kasim.Application
中的子包)。所以,
mainClass
移动到像tr.kasim
这样的父包,要么@ComponentScan({ "tr.kasim.Dao", "tr.kasim.Service", "tr.kasim.Application" })
等。- 更新 -
根据到目前为止的讨论,我建议采用第一个选项,因为这减少了手动启用实体,存储库等扫描的工作量。