我已经从start.spring.io
准备了一个应用程序名称为SpringBootWebManyToManyApplication(Artifact Id)
组ID是com,com.controller,repository,model,service
我收到以下错误:
Invalid property 'persons' of bean class [com.model.Person]: Bean property 'persons' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
//////////////////////////////主要应用/////////////// /////////////////
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootWebManyToManyApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebManyToManyApplication.class, args);
}
}
////////////////////////////////的IndexController ////////////// /////////
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
@RequestMapping(value="/")
String index(){
return "index";
}
}
/////////////////////////////// PersonController /////////////// /////////
package com.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.model.Person;
//import com.model.PersonDetail;
import com.service.PersonService;
@Controller
public class PersonController {
Person person = new Person();
private PersonService personService;
@Autowired
public void setPersonService(PersonService personService) {
this.personService = personService;
}
@RequestMapping(value = "/persons", method = RequestMethod.GET)
public String list(Model model){
model.addAttribute("persons", personService.listAllPersons());
System.out.println("Returning Persons");
return "persons";
}
@RequestMapping("person/{id}")
public String showPerson(@PathVariable Integer id, Model model){
model.addAttribute("person", personService.getPersonById(id));
return "personshow";
}
@RequestMapping("person/edit/{id}")
public String edit(@PathVariable Integer id, Model model){
model.addAttribute("person", personService.getPersonById(id));
return "personform";
}
@RequestMapping("person/new")
public String newPerson(Model model){
model.addAttribute("person", new Person());
return "personform";
}
@RequestMapping("person")
public String savePerson(Person person){
personService.savePerson(person);
//return "personform";
return "redirect:/person/" + person.getId();
}
@RequestMapping("person/delete/{id}")
public String delete(@PathVariable Integer id){
personService.deletePerson(id);
return "redirect:/persons/";
//return "persons";
/*return "persons";*/
}
}
/////////////////////////////// Person.java///////////// ///////////////
package com.model;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "person")
@Access(AccessType.FIELD)
public class Person implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id",unique=true,nullable=false)
private Integer id;
@Column(name = "name", unique = true, nullable = false, length = 10)
private String name;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "person_person_Detail", joinColumns = @JoinColumn(name = "person_id",
referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "personDetails_id",
referencedColumnName = "id"))
private Set<PersonDetail> personDetails;
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
/**
* @return the personDetails
*/
/**
* @return the personDetails
*/
public Set<PersonDetail> getPersonDetails() {
return personDetails;
}
/**
* @param personDetails the personDetails to set
*/
public void setPersonDetails(Set<PersonDetail> personDetails) {
this.personDetails = personDetails;
}
}
/////////////////////////// PersonDetail /////////////////// ///
package com.model;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="personDetails")
public class PersonDetail implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private Integer id;
@Column(name = "address", nullable = false, length = 20)
private String address;
@Column(name = "age", nullable = false,length=10)
private Integer age;
@ManyToMany(mappedBy = "personDetails")
private Set<Person> persons;
/**
* @return the persons
*/
public Set<Person> getPersons() {
return persons;
}
/**
* @param persons the persons to set
*/
public void setPersons(Set<Person> persons) {
this.persons = persons;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
public Set<Person> getPerson() {
return persons;
}
public void setPerson(Set<Person> persons) {
this.persons = persons;
}
}
//////////////////////////// PersonRepository ////////////////// ///////
package com.repository;
import javax.transaction.Transactional;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.model.PersonDetail;
@Repository
@Transactional
public interface PersonDetailRepository extends CrudRepository<PersonDetail, Integer>{
}
///////////////////////////////// PersonRepository ///////////// /////////
package com.repository;
import javax.transaction.Transactional;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.model.Person;
@Repository
@Transactional
public interface PersonRepository extends CrudRepository<Person, Integer>{
}
///////////////////////// PersonService ///////////////////// ////////////
package com.service;
import com.model.Person;
public interface PersonService {
Iterable<Person> listAllPersons();
Person getPersonById(Integer id);
Person savePerson(Person person);
void deletePerson(Integer id);
}
/////////////////////////////// PersonServiceImpl ///////////////
package com.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.model.Person;
//import com.model.PersonDetail;
import com.repository.PersonRepository;
@Service
public class PersonServiceImpl implements PersonService{
@Autowired
private PersonRepository personRepository;
public void setPersonRepository(PersonRepository personRepository) {
this.personRepository = personRepository;
}
@Override
public Iterable<Person> listAllPersons() {
// TODO Auto-generated method stub
return personRepository.findAll();
}
@Override
public Person getPersonById(Integer id) {
// TODO Auto-generated method stub
return personRepository.findOne(id);
}
@Override
public Person savePerson(Person person) {
// TODO Auto-generated method stub
return personRepository.save(person);
}
@Override
public void deletePerson(Integer id) {
personRepository.delete(id);
}
}
/////////////////////////////模板头/////////////// //////////
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<!-- <link href="../static/css/bootstrap.min.css"
th:href="@{/css/bootstrap.min.css}"
rel="stylesheet" media="screen"/>
<link href="../../static/css/style.css"
th:href="@{css/style.css}" rel="stylesheet" media="screen"/> -->
</head>
<body>
<!-- <div class="container">
<div th:fragment="header">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#" th:href="@{/}">Home</a>
<ul class="nav navbar-nav">
<li><a href="#" th:href="@{/products}">Products</a></li>
<li><a href="#" th:href="@{/product/new}">Create Product</a></li>
</ul>
</div>
</div> -->
<!-- </nav> -->
<!-- <div class="jumbotron">
<div class="row text-center">
<div class="">
<h2>Spring Boot Exemple</h2>
<h3>Bienvenue</h3>
</div>
</div> -->
<!-- <div class="row text-center">
<img src="../../static/images/boot.png" width="200"
th:src="@{/images/boot.png}"/>
</div> -->
<!-- </div>
</div>
</div> -->
</body>
</html>
///////////////////////////// headerinc.html /////////////// //////////
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en" th:fragment="head">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<!-- <link href="../static/css/bootstrap.min.css"
th:href="@{/css/bootstrap.min.css}"
rel="stylesheet" media="screen" />
<link href="../static/css/style.css"
th:href="@{/css/style.css}" rel="stylesheet" media="screen"/>
--></head>
<body>
</body>
</html>
///////////////////////////////的index.html ///////////// ////////////
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<title>Spring Boot Example</title>
<!-- <th:block th:include="fragments/headerinc :: head"></th:block> -->
</head>
<body>
<div class="container">
<div th:fragment="header">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<!-- <a class="navbar-brand" href="#" th:href="@{/}">Home</a> -->
<ul class="nav navbar-nav">
<li><a href="#" th:href="@{/persons}">Persons</a></li>
<li><a th:href="@{/person/new}">Add Person</a> </li>
</ul>
</div>
</div></nav></div>
<!-- <div class="container"> -->
<!-- <th:block th:include="fragments/header :: header"></th:block> -->
</div>
</body>
</html>
///////////////////////////// personform.html /////////////// //////
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<title>Spring Boot Example</title>
<th:block th:include="fragments/headerinc :: head"></th:block>
</head>
<body>
<div class="container">
<th:block th:include="fragments/header :: header"></th:block>
<h2>Person Details</h2>
<div>
<!-- th:object="${person}" -->
<form class="form-horizontal" th:object="${person}" th:action="@{/person}" method="post">
<input type="hidden" th:field="*{id}"/>
<!-- <div class="form-group">
<label class="col-sm-2 control-label">Person Id:</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{id}"/>
</div>
</div> -->
<div class="form-group">
<label class="col-sm-2 control-label">Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{name}"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Address:</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{persons.personDetails.address}"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Age:</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:field="*{persons.personDetail.age}"/>
</div>
</div>
<div class="row">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
<a href="#" th:href="@{/}">Back</a>
</div>
</div>
</body>
</html>
//////////////////////////////// persons.html //////////// /////////////
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<title>Persons</title>
<th:block th:include="fragments/headerinc :: head"></th:block>
</head>
<body>
<div class="container">
<!-- <th:block th:include="fragments/header :: header"></th:block> /*/ -->
<div th:if="${not #lists.isEmpty(persons)}">
<b><font size="4">Person List</font></b>
<table class="table table-striped" border="1">
<tr>
<th>Id</th>
<th>Person Id</th>
<th>Name</th>
<th>Address</th>
<th>Age</th>
<th>Operations</th>
</tr>
<tr th:each="person : ${persons}">
<td th:text="${person.id}"><a href="/person/${person.id}">Id</a></td>
<td th:text="${person.id}">Person Id</td>
<td th:text="${person.name}">Name</td>
<td th:text="${person.personDetails.address}">Address</td>
<td th:text="${person.personDetails.age}">Age</td>
<td><a th:href="${ '/person/' + person.id}">View</a> <a th:href="${'/person/edit/' + person.id}">Update</a> <a th:href="${'/person/delete/' + person.id}">Delete</a></td>
</tr>
</table>
<a href="#" th:href="@{/}">Back</a> <br/>
<a th:href="@{/person/new}">Add Person</a>
</div>
</div>
</body>
</html>
////////////////////////////// personshow.html ////////////// ////////
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<title>Person Details</title>
<th:block th:include="fragments/headerinc :: head"></th:block>
</head>
<body>
<div class="container">
<!--/*/ <th:block th:include="fragments/header :: header"></th:block> /*/-->
<h2>Person Details</h2>
<div>
<form class="form-horizontal" th:action="@{/person}" th:object="${person}" method="get">
<label class="col-sm-2 control-label"><b>Person Id:</b></label>
<p class="form-control-static" th:text="${id}">Person Id</p>
<label class="col-sm-2 control-label"><b>Name:</b></label>
<p class="form-control-static" th:text="${person.name}">name</p>
<label class="col-sm-2 control-label"><b>Address:</b></label>
<p class="form-control-static" th:text="${person.personDetails.address}">address</p>
<label class="col-sm-2 control-label"><b>Age</b></label>
<p class="form-control-static" th:text="${person.personDetails.age}">age</p>
<a href="#" th:href="@{/persons}">Person List</a>
</form>
</div>
</div>
</body>
</html>
///////////////////////////// application.properties/////////////// /////
#logging.level.org.h2.server: DEBUG
# Database
spring.datasource.url= jdbc:mysql://localhost:3306/myschema
spring.datasource.username=root
spring.datasource.password=admin123
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect