无法在Spring Boot中创建存储库bean

时间:2018-09-21 18:10:42

标签: spring-boot jpa spring-data-jpa

您好,我尝试了几种方法,阅读了有关该主题的几乎所有问题,但无法解决我的问题。我是春天的新手。

尝试使用Spring Boot应用程序调用数据库

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.abc</groupId>
    <artifactId>abc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

模型

package com.abc.model;

import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;

@Entity
@Table(name="phone")
public class phone {

    @Id
    @Column(name = "phonenumber", unique = true, nullable = false)
    String phonenumber;

    @Column(name = "code")
    int code;

    public String getPhonenumber() {
        return phonenumber;
    }

    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }

    public int getPortfolioCode() {
        return portfolioCode;
    }

    public void setCode(int code) {
        this.code = code;
    }

}

PhoneRepository

package com.abc.repository;

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PhoneRepository extends CrudRepository<Phone,Integer> {
    List<Phone> findByCode(Integer Code);
}

PhoneService

package com.abc.service;

import java.util.List;

public interface PhoneService {

    List<String> getAllCodes(Integer code);

}

phoneserviceimpl

package com.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PhoneServiceImpl implements PhoneService{

    @Autowired
    private  PhoneRepository phoneRepository;

    @Override
    public List<String> getAllPhones(Integer Code) {
        List<String> result = new ArrayList<String>();
        List<Phone> phones= phoneRepository.findByCode(code);
        for(Phone phone:phones){
            result.add(phone.getPhonenumbers());
        }
        return result;
    }
}

应用

package com.abc

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsoleApp implements CommandLineRunner {

    @Autowired
    public PositionService positionService;

    public static  void main( String[] args){
        SpringApplication app = new SpringApplication(ConsoleApp.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }

    @Override
    public void run(String... strings) throws Exception {
        if (strings.length > 0) {
        System.out.println(strings[0]);
        } else {
            System.out.println("Something went wrong!");

            try{
                System.out.println("going to read fropm db");
                List<String> nums= phoneService.getAllPhonenumbers(155275);

                for(String c: nums){
                    System.out.println(c+"  ------> bye bye");
                }

            }
            catch(Exception ex){
                System.out.println("db failed");
            }
        }
    }
}
  

com.abc中的field phoneRepository需要一个Bean

我尝试在应用程序类之前连接所有EnableJpaRepositories之类的注释,但对我来说效果不佳

0 个答案:

没有答案