首先,我会说这个应用程序运行时,它在我想要测试的内容中被破坏了。我基本上想通过hibernate显示来自MySql的所有已保存实体,并将其打印在带有百里香叶的Web浏览器中。我无法弄清楚我做错了什么。没有例外,但DAO.count显示为0.感谢您的帮助。
DAO:
Authorization
控制器:
@Repository("CustomerDAO")
@Transactional
public interface CustomerDAO extends CrudRepository<Customer, Integer> {
public List<Customer> findAll();
public long count();
}
实体:
@Controller
public class CustomerController {
private CustomerDAO customerDAO;
@Autowired
public void setCustomerDAO(CustomerDAO customerDAO) {
this.customerDAO = customerDAO;
}
private SessionFactory sessionFactory;
@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";
}
}
HTML / thymeleaf:
@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;
}
}
Application.properties:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h3 th:text="${answer}"></h3>
<div th:each="customer: ${customers}">
<h3 th:text="${customer.firstName}">This didnt work</h3>
</div>
</body>
</html>
打印跟踪:
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=create-drop
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