基于springscurity基础架构, 自己创建两张表,一张person ,字段有id,iphone,address. 一张account,字段名id,name,balance,personId实现两张表联合增删改查. 实现的方法有, 查询所有, 添加数据, 修改数据, 删除数据, 根据主键查询
时间: 2023-08-05 07:07:30 浏览: 102
首先,需要定义Person和Account实体类,以及对应的DAO接口和Service接口。
Person实体类:
```java
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String iphone;
private String address;
// getter and setter
}
```
Account实体类:
```java
@Entity
@Table(name = "account")
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private double balance;
@ManyToOne
@JoinColumn(name = "person_id")
private Person person;
// getter and setter
}
```
PersonDAO接口:
```java
public interface PersonDAO extends JpaRepository<Person, Long> {
}
```
AccountDAO接口:
```java
public interface AccountDAO extends JpaRepository<Account, Long> {
List<Account> findByPersonId(Long personId);
}
```
PersonService接口:
```java
public interface PersonService {
List<Person> getAllPerson();
Person savePerson(Person person);
Person updatePerson(Person person);
void deletePersonById(Long id);
Person getPersonById(Long id);
}
```
PersonServiceImpl实现:
```java
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonDAO personDAO;
@Override
public List<Person> getAllPerson() {
return personDAO.findAll();
}
@Override
public Person savePerson(Person person) {
return personDAO.save(person);
}
@Override
public Person updatePerson(Person person) {
Person existingPerson = personDAO.findById(person.getId()).orElse(null);
if (existingPerson != null) {
existingPerson.setIphone(person.getIphone());
existingPerson.setAddress(person.getAddress());
return personDAO.save(existingPerson);
}
return null;
}
@Override
public void deletePersonById(Long id) {
personDAO.deleteById(id);
}
@Override
public Person getPersonById(Long id) {
return personDAO.findById(id).orElse(null);
}
}
```
AccountService接口:
```java
public interface AccountService {
List<Account> getAllAccounts();
Account saveAccount(Account account);
Account updateAccount(Account account);
void deleteAccountById(Long id);
List<Account> getAccountsByPersonId(Long personId);
Account getAccountById(Long id);
}
```
AccountServiceImpl实现:
```java
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDAO accountDAO;
@Override
public List<Account> getAllAccounts() {
return accountDAO.findAll();
}
@Override
public Account saveAccount(Account account) {
return accountDAO.save(account);
}
@Override
public Account updateAccount(Account account) {
Account existingAccount = accountDAO.findById(account.getId()).orElse(null);
if (existingAccount != null) {
existingAccount.setName(account.getName());
existingAccount.setBalance(account.getBalance());
existingAccount.setPerson(account.getPerson());
return accountDAO.save(existingAccount);
}
return null;
}
@Override
public void deleteAccountById(Long id) {
accountDAO.deleteById(id);
}
@Override
public List<Account> getAccountsByPersonId(Long personId) {
return accountDAO.findByPersonId(personId);
}
@Override
public Account getAccountById(Long id) {
return accountDAO.findById(id).orElse(null);
}
}
```
在Controller中,可以根据需要调用各个Service接口的方法来实现增删改查操作。例如:
```java
@RestController
@RequestMapping("/api")
public class MyController {
@Autowired
private PersonService personService;
@Autowired
private AccountService accountService;
// 查询所有Person
@GetMapping("/persons")
public List<Person> getAllPersons() {
return personService.getAllPerson();
}
// 添加Person
@PostMapping("/persons")
public Person addPerson(@RequestBody Person person) {
return personService.savePerson(person);
}
// 修改Person
@PutMapping("/persons")
public Person updatePerson(@RequestBody Person person) {
return personService.updatePerson(person);
}
// 删除Person
@DeleteMapping("/persons/{id}")
public void deletePersonById(@PathVariable Long id) {
personService.deletePersonById(id);
}
// 根据Person的id查询Person
@GetMapping("/persons/{id}")
public Person getPersonById(@PathVariable Long id) {
return personService.getPersonById(id);
}
// 查询所有Account
@GetMapping("/accounts")
public List<Account> getAllAccounts() {
return accountService.getAllAccounts();
}
// 添加Account
@PostMapping("/accounts")
public Account addAccount(@RequestBody Account account) {
return accountService.saveAccount(account);
}
// 修改Account
@PutMapping("/accounts")
public Account updateAccount(@RequestBody Account account) {
return accountService.updateAccount(account);
}
// 删除Account
@DeleteMapping("/accounts/{id}")
public void deleteAccountById(@PathVariable Long id) {
accountService.deleteAccountById(id);
}
// 根据Account的id查询Account
@GetMapping("/accounts/{id}")
public Account getAccountById(@PathVariable Long id) {
return accountService.getAccountById(id);
}
// 根据Person的id查询对应的Account
@GetMapping("/accounts/person/{personId}")
public List<Account> getAccountsByPersonId(@PathVariable Long personId) {
return accountService.getAccountsByPersonId(personId);
}
}
```
以上就是基于springscurity基础架构,自己创建两张表,一张person ,字段有id,iphone,address. 一张account,字段名id,name,balance,personId实现两张表联合增删改查的方法。
阅读全文