2
respostas

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

package br.ufrj.ch.cnesprofissional.repository;

import org.springframework.data.repository.CrudRepository;

import br.ufrj.ch.cnesprofissional.modelo.Profissional;

public interface ProfissionalRepository extends CrudRepository<Profissional, Long> {

}
package br.ufrj.ch.cnesprofissional.configuracao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import br.ufrj.ch.cnesprofissional.modelo.Profissional;
import br.ufrj.ch.cnesprofissional.repository.ProfissionalRepository;

@Controller
public class CnesController {

    @Autowired
    private ProfissionalRepository repository;

    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @RequestMapping("listaprofissionais")
    public String listaProfissionais(Model model) {
        Iterable<Profissional> profissionais = repository.findAll();
        model.addAttribute("profissionais",profissionais);
        return"listaprofissionais";
    }
}
package br.ufrj.ch.cnesprofissional.modelo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;


@Entity
public class Profissional {

    @Id
    @GeneratedValue
    private Long id;
    private String nome;
    private String telefone;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getTelefone() {
        return telefone;
    }
    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }
}
package br.ufrj.ch.cnesprofissional.configuracao;

import javax.sql.DataSource;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@SpringBootApplication
public class Configuracao {

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

    @Bean
    public DataSource dataSouce(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgresql://localhost:5432/cnes");
        dataSource.setUsername("postgres");
        dataSource.setPassword("123456");
        return dataSource;
    }


}
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>br.ufrj.ch</groupId>
    <artifactId>cnesprofissional</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <spring-boot-version>2.2.6.RELEASE</spring-boot-version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>${spring-boot-version}</version>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901-1.jdbc4</version>
        </dependency>

    </dependencies>

</project>

tive que add no pom a dependencia javax.xml.bind já baixei os projeto já pronto daqui e vem com outros erros.

2 respostas

boa tarde, pelo que vi, falta vc criar a sua INTERFACE e dar um extend no JPARepositoty e depois injetar a classe no seu controller @Autowired nomeDaClasseRepository nomedaclasseRepository; e usar o nomedaclasseRepository.findAll( ); ou outros

eu add a inferface do código, eu não tinha colocado aqui