Home  >  Article  >  Java  >  How to integrate Hibernate in SpringBoot project

How to integrate Hibernate in SpringBoot project

WBOY
WBOYforward
2023-05-18 09:49:231648browse

Integrating Hibernate in Spring Boot projects

Preface

Hibernate is a popular ORM (Object Relational Mapping) framework that can map Java objects to database tables to facilitate Persistence operations. In the Spring Boot project, integrating Hibernate can help us perform database operations more easily. This article will introduce how to integrate Hibernate in the Spring Boot project and provide corresponding examples.

1.Introduce dependencies

Introduce the following dependencies in the pom.xml file:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
</dependency>

Among them, spring-boot-starter-data-jpa is provided by Spring Boot As a starting dependency for integrating JPA (Java Persistence API), it already includes Hibernate-related dependencies. The driver for the MySQL database is mysql-connector-java. hibernate-core is the core dependency of Hibernate.

2. Configure the data source

Configure the data source in application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=create-drop

The MySQL database is used here and can be modified according to the actual situation. Among them, the spring.jpa.hibernate.ddl-auto attribute specifies how Hibernate automatically generates database tables, and create-drop means that the table will be created every time the application is started, and the table will be deleted when the application is closed.

3. Create an entity class

Create a simple entity class for mapping to a database table:

@Entity
@Table(name = "person")
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "age")
    private int age;

    // getters and setters
}

Use the @Entity annotation on the entity class to indicate that this is A JPA entity class. The @Table annotation is used to specify the database table name to which the entity class is mapped. Use the @Id annotation to define the primary key of the entity class, and use @GeneratedValue to specify how the primary key is generated. The @Column annotation is used to specify the database column name to which entity class attributes are mapped.

4. Create Repository

Create a simple Repository for accessing the database:

@Repository
public interface PersonRepository extends JpaRepository<Person, Long> {
}

Use the @Repository annotation on the Repository to indicate that it is a Spring component, and Used to access the database. PersonRepository inherits from JpaRepository. This interface provides many common database operation methods, such as save, findById, etc.

5. Write business code

Use PersonRepository in Service for database operations:

@Service
public class PersonService {
public void savePerson(Person person) {
    personRepository.save(person);
}

public List<Person> getPersons() {
    return personRepository.findAll();
}

@Service annotation is used on a Service to indicate that it is a Spring component used for handling business logic. In this example, we define two methods, savePerson is used to save Person objects to the database, and getPersons is used to obtain all Person objects.

6. Write a controller to process http requests

Write a simple controller to process HTTP requests:

@RestController
public class PersonController {
    @Autowired
    private PersonService personService;

    @PostMapping("/person")
    public void savePerson(@RequestBody Person person) {
        personService.savePerson(person);
    }

    @GetMapping("/persons")
    public List<Person> getPersons() {
        return personService.getPersons();
    }
}

Use the @RestController annotation on the controller, Indicates that this is a Spring component and is used to handle HTTP requests. In this example, we define two methods, savePerson is used to process POST requests and save Person objects to the database, and getPersons is used to process GET requests and obtain all Person objects.

7. Run the application

Now you can start the application and access http://localhost:8080/persons to get all Person objects. If you need to add a new Person object, you can use a POST request to send data to http://localhost:8080/person. If everything is fine, you should see the following output:

[{"id":1,"name":"Alice","age":20},{"id":2 ,"name":"Bob","age":30}]

The above is the detailed content of How to integrate Hibernate in SpringBoot project. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete