spring 构造函数的参数0需要类型为“javax.persistence.EntityManager”的Bean,但找不到该Bean

nle07wnf  于 2023-02-07  发布在  Spring
关注(0)|答案(6)|浏览(146)

你好,我的工作Spring Boot ,我已经尝试了很多方法,我不断得到这个错误,但它没有发生,一些代码显示红色,你能帮助enter image description hereenter image description hereenter image description here

h79rfbju

h79rfbju1#

你需要用用户名,密码和主机配置实体管理器. hib需要实体管理器来连接数据库.你可以像这样添加配置

spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
spring.datasource.password=root
spring.datasource.username=root

你还需要

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId> // your db connector
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
twh00eeo

twh00eeo2#

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>6.1.6.Final</version>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
    </dependency>
</dependencies>
hmmo2u0o

hmmo2u0o3#

public City(int id, String name, String countryCode, String district, int population) {
    super();
    this.id = id;
    this.name = name;
    this.countryCode = countryCode;
    this.district = district;
    this.population = population;
}
public City() {}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getCountryCode() {
    return countryCode;
}
public void setCountryCode(String countryCode) {
    this.countryCode = countryCode;
}
public String getDistrict() {
    return district;
}
public void setDistrict(String district) {
    this.district = district;
}
public int getPopulation() {
    return population;
}
public void setPopulation(int population) {
    this.population = population;
}

}

ygya80vv

ygya80vv4#

@Repository

公共类HibernateCityDal实现ICityDal {

private EntityManager entityManager;

@Autowired
public HibernateCityDal(EntityManager entityManager) {
    this.entityManager = entityManager;
}

@Override
@Transactional
public List<City> getAll() {
    Session session = entityManager.unwrap(Session.class);
    List<City> cities = session.createQuery("from City",City.class).getResultList();
    return cities;
}

@Override
public void add(City city) {
    // TODO Auto-generated method stub
    
}

@Override
public void update(City city) {
    // TODO Auto-generated method stub
    
}

@Override
public void delete(City city) {
    // TODO Auto-generated method stub
    
}

}

jtoj6r0c

jtoj6r0c5#

@服务公共类CityManager实现ICityService {

private ICityDal cityDal;

@Autowired
public CityManager(ICityDal cityDal) {
    this.cityDal = cityDal;
}

@Override
@Transactional
public List<City> getAll() {
    return this.cityDal.getAll();
}

@Override
@Transactional
public void add(City city) {

    
}

@Override
@Transactional
public void update(City city) {

    
}

@Override
@Transactional
public void delete(City city) {

    
}

}

2fjabf4q

2fjabf4q6#

@RestController

@请求Map(“/API”)公共类城市控制器{

private ICityService cityService;

@Autowired
public CityController(ICityService cityService) {
    this.cityService = cityService;
}
@GetMapping("/cities")
public List<City> get(){
    return cityService.getAll();
}

}

相关问题