我似乎无法从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', '[email protected]'),
    (2, 'John', 'Die', '[email protected]'),
    (3, 'Ajay', 'Rao', '[email protected]'),
    (4, 'Mary', 'Publiidc', '[email protected]'),
    (5, 'Maxwell', 'Dixon', '[email protected]');

最佳答案

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

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

关于java - CRUDRepository findAll查找为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43564402/

10-10 08:54