Reputation: 187
I just started learning the Spring MVC framework for Java, and I want to learn how to handle CRUD operations as well as handling form data.
So far many tutorials on the net are mostly about Spring with Maven combination which I find confusing as a self-studying beginner.
I would appreciate some clarifications on what I would need exactly to handle CRUD operations in a typical Spring MVC project with a preference of MySQL. Also any relevant tutorial suggestions to get the same would be highly appreciated.
Upvotes: 4
Views: 7252
Reputation: 12684
You can perform CRUD operations in Spring very easily with Spring Data JPA.
Suppose you have your entity class YourClass
:
@Entity
public class YourClass {
@Id
private Long id;
private String name;
// constructors, getters, setters
}
Just extend your repository interface from org.springframework.data.repository.CrudRepository
to have it working.
Note: You can find that org.springframework.data.jpa.repository.JpaRepository
is more convenient, feel free to use it because it is also a CrudRepository
by inheritance.
public interface YourClassRepository extends JpaRepository<YourClass, Long> {
// you already have all the CRUD methods
// you can also create own queries just introducing their signatures like below:
List<YourClass> findByName(String name);
}
And you are ready to perform all CRUD operations as save
, findAll
, findOne
, delete
(and some other methods).
If in a typical Java application you have to write a class that implements YourClassRepository
here comes the main power of Spring Data JPA – you don’t have to write any implementation of the repository interface. Spring Data JPA creates an implementation on the fly when you run your application.
Spring Data JPA also allows you to define other query methods by simply declaring their method signature. In this example it is shown with a findByName()
method. That's because you have the name
field in YourClass
, you can create such queries with any other fields.
Then you can simply inject your repository and use it in your controller or service class:
@Controller
public class YourClassController {
@Autowired
private YourClassRepository repository;
@RequestMapping(value = "entities", method = RequestMethod.GET)
public List<YourClass> list() {
return repository.findAll();
}
@RequestMapping(value = "entities", method = RequestMethod.POST)
public YourClass create(@RequestBody YourClass yourClass) {
return repository.saveAndFlush(yourClass);
}
@RequestMapping(value = "entities/{id}", method = RequestMethod.GET)
public YourClass get(@PathVariable Long id) {
return repository.findOne(id);
}
@RequestMapping(value = "entities/{id}", method = RequestMethod.PUT)
public YourClass update(@PathVariable Long id, @RequestBody YourClass yourClass) {
YourClass existingYourClass = repository.findOne(id);
BeanUtils.copyProperties(yourClass, existingYourClass);
return repository.saveAndFlush(existingYourClass);
}
@RequestMapping(value = "entities/{id}", method = RequestMethod.DELETE)
public YourClass delete(@PathVariable Long id) {
YourClass existingYourClass = repository.findOne(id);
repository.delete(existingYourClass);
return existingYourClass;
}
}
The excellent tutorial you should complete to understand it is Accessing Data with JPA.
What about configuring MySQL database, first you need to add mysql-connector-java dependency to Maven's pom.xml
, and then provide the datasource details.
You can do it in the following ways: in your configurational Spring bean XML file or by using Java Annotations. But the easiest way to do it is by using Spring Boot and provide your datasource details in application.properties
file as in example:
application.db.driver = com.mysql.jdbc.Driver
application.db.url = jdbc:mysql://localhost:3306/yourDbName
application.db.username = username
application.db.password = password
Finally in Spring Guides you can find a lot of very useful tutorials about using Spring project releases and techniques as recommended by the Spring team, starting from scratch and completing them step-by-step. They are usually short (15-20 minutes per guide) and very clear.
And of course you have to learn some build automation tool like Maven, Gradle, Ant – it is incredibly handy. Here is the article about how to start using Maven for very beginners: Maven in 5 Minutes.
Good luck with your studying.
Upvotes: 4