CRUDRepository findAll找到空

时间:2017-04-22 20:44:18

标签: java mysql hibernate spring-boot

我似乎无法从hiber中获取mySQL中的任何信息。我到处都读过,我觉得我这样做是正确的,显然我不是,但是我的大脑已经试图理解我搞砸了什么。

主:

@SpringBootApplication
@ComponentScan("com.luv2code")
@EntityScan("com.luv2code.entity")
@EnableJpaRepositories("com.luv2code.dao")
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

控制器:

@Controller
public class CustomerController {


    private CustomerDAO customerDAO;

    @Autowired
    public void setCustomerDAO(CustomerDAO customerDAO) {
        this.customerDAO = customerDAO;
    }

    @RequestMapping("/")
    public String listCustomers(Model model){

        long test = customerDAO.count();
        model.addAttribute("answer", test);

        List<Customer> customers = customerDAO.findAll();
        model.addAttribute("customers", customers);
        return "home";
    }

}

application.properties:

spring.datasource.url = jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false
spring.datasource.username=root
spring.datasource.password=metalgear3
spring.datasouce.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect

spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
entitymanager.packagesToScan = com.luv2code.entity.Customer

实体:

@Entity
@Table(name = "customer")
public class Customer implements Serializable {

    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private int id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "email")
    private String email;

    public Customer(){}

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                '}';
    }

    public Customer(String firstName, String lastName, String email){
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

DAO:

@Repository("CustomerDAO")
@Transactional
public interface CustomerDAO extends CrudRepository<Customer, Integer> {

    public List<Customer> findAll();
    public long count();
}

MySQL的:

CREATE DATABASE IF NOT EXISTS `web_customer_tracker`;
USE `web_customer_tracker`;

DROP TABLE IF EXISTS `customer`;
CREATE TABLE `customer`(
`id` INT(11) NOT NULL auto_increment,
`first_name` VARCHAR(45) DEFAULT NULL,
`last_name` VARCHAR(45)DEFAULT NULL,
`email` VARCHAR(45) DEFAULT NULL,
PRIMARY KEY(`id`)
) ENGINE=INNODB AUTO_INCREMENT=1 CHARSET=latin1;


INSERT INTO `customer` VALUES
    (1, 'David', 'Adams', 'david@luv2code.com'),
    (2, 'John', 'Die', 'john@luv2code.com'),
    (3, 'Ajay', 'Rao', 'ajay@luv2code.com'),
    (4, 'Mary', 'Publiidc', 'mary@luv2code.com'),
    (5, 'Maxwell', 'Dixon', 'maxwell@luv2code.com');

1 个答案:

答案 0 :(得分:0)

我知道现在答复有点晚了,但是这个问题对Google友好。

您不需要在findAll()内声明CustomerDAO。接口CrudRepository已经做到了。最重要的是,我建议改为实现JpaRepository存储库(它扩展了CrudRepository本身,并为您提供了更多免费的便捷方法。