Java Framework FAQs and Solutions
1. How to choose the right Java framework?
2. How to solve Spring Bean injection problem?
3. How to handle Hibernate lazy loading exception?
4. How to solve Struts 2 interceptor problem?
5. How to improve the efficiency of JUnit unit testing?
Practical case: Building a CRUD (create, read, update, delete) application using Spring MVC and MySQL
@SpringBootApplication public class CrudApp { public static void main(String[] args) { SpringApplication.run(CrudApp.class, args); } } @Entity class Person { @Id @GeneratedValue private Long id; private String name; private int age; } @Repository interface PersonRepository extends CrudRepository<Person, Long> {} @RestController class PersonController { @Autowired private PersonRepository personRepository; @GetMapping("/person") public List<Person> getAll() { return personRepository.findAll(); } @PostMapping("/person") public Person create(@RequestBody Person person) { return personRepository.save(person); } @GetMapping("/person/{id}") public Person getById(@PathVariable Long id) { return personRepository.findById(id).orElse(null); } @PutMapping("/person/{id}") public Person update(@PathVariable Long id, @RequestBody Person person) { Person existing = personRepository.findById(id).orElse(null); if (existing != null) { existing.setName(person.getName()); existing.setAge(person.getAge()); return personRepository.save(existing); } return null; } @DeleteMapping("/person/{id}") public void delete(@PathVariable Long id) { personRepository.deleteById(id); } }
The above is the detailed content of FAQs and Solutions for Java Frameworks. For more information, please follow other related articles on the PHP Chinese website!