1. An overview of the
Because I’m going to use Spring Boot, I just learned it recently. This is a simple demo of a Web project with mysq+Hibernate+ Tomcat, which is easy to expand into your own project.
2. Create an initial Spring Demo
The IDE used by the author is IDEA. Create a project and choose Spring Initalizer.
3. Configure the data source
The configuration data source is divided into two parts, one is to create tables and users, and the other is configured in application.properties.
(1) to build library
create database test;
Copy the code
Note that you don’t need to create a table here, because Hibernate automatically creates a table with the same name from the entity class.
(2) building users
create user 'db'@The '%' identified by 'xxxxx';
Copy the code
(3) User authorization
grant all on test.* to 'db'@The '%';
Copy the code
In this case, it is recommended to authorize all, because Hibernate will use the create table permission later.
(4) the configuration application. The properties
Configure four properties:
spring.jpa.hibernate.ddl-auto=
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
Copy the code
The first property is that you can take values
- none
- update
- create
- create-drop
a.none
None is the default value for mysql and does not change the database structure.
b.update
Hibernate changes the database based on the given entity class.
c.create
The database is created but not deleted on shutdown.
d.create-drop
Create the database and delete the database when SessionFactory is closed. This is the default option for H2 and other embedded databases.
It must be set to update or create on the first run because the exact entity class is not yet known. After the first run, it can be set to update or none. url for mysql
jdbc:mysql://ip:3306/database
Copy the code
The remaining two are the username and password. The following is the author’s configuration for reference:
4. Create an entity class
To create a simple User Entity class, use javax’s Entity,Id,GeneratedValue, and GenerationType annotations. Entity is used to identify the Entity class,Id is used to identify the primary key, and GeneratedValue and GenerationType are used to configure the primary key.
package com.test;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
public Integer getId(a) {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name; }}Copy the code
In addition to the primary key, you can add any attributes you want, as well as setters and getters, and Hibernate will automatically make the entity class into a table.
5. Create the Repository
Create a repository to hold user records. You need to inherit CrudRepository<T,ID>. The first type is an entity class and the second type is a primary key type.
package com.test;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User.Integer>
{}Copy the code
6. Create a controller
The controller is used to control Http requests. You can configure different paths to perform different operations on the controller.
package com.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping(path = "/demo")
public class MainController {
@Autowired
private UserRepository userRepository;
@PostMapping(path = "/add")
public @ResponseBody String addNewUser(@RequestParam String name)
{
User user = new User();
user.setName(name);
userRepository.save(user);
return "Saved.";
}
@GetMapping(path = "/all")
public @ResponseBody Iterable<User> getAllUsers(a)
{
returnuserRepository.findAll(); }}Copy the code
The value in @requestParam indicates that the URL starts with this value. @postMapping is the path to handle only post requests. @responseBody indicates the type of return. @requestParam indicates a parameter that was retrieved from get or POST. GetAllUsers () returns either JSON or XML.
7. Test on the IDE
First of all, the input
localhost:8080/demo/all
Copy the code
Because the author has a previous row of data so it is shown.
curl localhost:8080/demo/add -d name=123
Copy the code
Response:
8. Pack it up
Build->Build Artifacts.
9. Complete code
- github
- Yards cloud