源代码
package com.ch.ch6_2.controller;
import com.ch.ch6_2.entity.IdCard;
import com.ch.ch6_2.entity.Person;
import com.ch.ch6_2.service.PersonAndIdCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class TestOneToOneController {
@Autowired
private PersonAndIdCardService personAndIdCardService;
@RequestMapping("/save")
public String save() {
personAndIdCardService.saveAll();
return "人员和身份保存成功!";
}
@RequestMapping("/findAllPerson")
public List<Person> findAllPerson() {
return personAndIdCardService.findAllPerson();
}
@RequestMapping("/findAllIdCard")
public List<IdCard> findAllIdCard() {
return personAndIdCardService.findAllIdCard();
}
/**
* 根据人员ID查询身份信息(级联查询)
*/
@RequestMapping("/findByPerson_id")
public IdCard findByPerson_id(Integer id) {
return personAndIdCardService.findByPerson_id(id);
}
@RequestMapping("/findByAddressAndCode")
public List<IdCard> findByAddressAndCode(String address, String code) {
return personAndIdCardService.findByAddressAndCode(address, code);
}
/**
* 根据身份ID查询人员信息(级联查询)
*/
@RequestMapping("/findByIdCard_id")
public Person findByIdCard_id(Integer id) {
return personAndIdCardService.findByIdCard_id(id);
}
@RequestMapping("/findByPnameAndPsex")
public List<Person> findByPnameAndPsex(String pname, String psex) {
return personAndIdCardService.findByPnameAndPsex(pname, psex);
}
@RequestMapping("/getOneIdCard")
public IdCard getOneIdCard(Integer id) {
return personAndIdCardService.getOneIdCard(id);
}
@RequestMapping("/getOnePerson")
public Person getOnePerson(Integer id) {
return personAndIdCardService.getOnePerson(id);
}
}
JPA会自动将数据表建立好
就是将实体类的Java对象持久化保存到数据库中
对于多条件联合查询的URL的写法
要调用TestOneToOneController
中的findByPnameAndPsex
方法,您需要构建一个带有适当参数的URL。由于该方法处理HTTP GET请求,您将在URL中将参数作为查询参数传递。
假设您在本地主机上运行Spring Boot应用程序,并使用默认端口(例如,8080),调用findByPnameAndPsex
方法的URL如下所示:
http://localhost:8080/findByPnameAndPsex?pname=<pname_value>&psex=<psex_value>
请将<pname_value>
和<psex_value>
替换为您要用于pname
和psex
参数的实际值。当您向此URL发出GET请求时,Spring Boot控制器将从查询字符串中提取pname
和psex
参数,并将它们传递给findByPnameAndPsex
方法,该方法将执行搜索并返回与指定的姓名和性别匹配的Person
实体列表。
实例说明
http://localhost:8080/ch6_2/findByPnameAndPsex?pname=%E9%99%88%E6%81%921&psex=%E7%94%B7