SpringBoot and data access (JPA and Mybatis)

Posted by BuzzPHP on Tue, 21 Sep 2021 06:38:50 +0200

part 1 ORM framework

ORM (Object-Relational Mapping)
The emergence of ORM framework is essentially to simplify the coding of database operations in the programming process. Now this ORM framework can be basically divided into two categories.
First, the representative of fully automatic ORM framework is -- > hibernate claims that you don't need to write sql. All sql in this framework is generated by Java code, and programmers don't need to jump out of the program to write sql
Second: the representative of semi-automatic ORM framework is -- > mybatis, which is famous for flexible debugging

This article will record the integration of SpringBoot with Jpa and Mybatis.

part 2 SpringBoot integration JPA

1 create a new module

Here, I like to use spring initializz to create SpringBoot projects. Ah... I'm mainly lazy.

2 select the necessary configuration

Some basic items are selected here, but a lazy plug-in is selected to save Lombok a lot of trouble. Things such as Getter and Setter only need to be described with annotations.

3. Introduce the dependency of JPA


4 change configuration

I usually use the yaml file format for configuration. Because of some coding problems, I don't like using properties as the configuration file. I will delete the application.properties in the resources directory and create a new application.yml file.

  port: 8090

    url: jdbc:mysql://localhost:3306/Pollen
    username: root
    password: root
    #Automatically create | update | verify database table structure
      ddl-auto: update
    #Set the database engine to InnoDB
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    #Print Sql statements to facilitate debugging
    show-sql: true

5 create entity class

package com.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;

@Data		Lombok The annotation of is used to generate the necessary components of the entity class (Getter, Setter, Constructor)
@AllArgsConstructor			Lombok Annotations are used to generate full parameter constructors
@NoArgsConstructor			Lombok Annotations are used to generate parameterless constructors
@Entity		Indicates that this is an entity class. To make ORM mapping with the database, the default class name is the Table name. If it is not specified, use @ Table to change the Table name or Schema 
public class User implements Serializable {
    //Implement a serialization interface
    private Long id;
    @Column(name = "username")
    private String username;
    @Column(name = "password")
    private String password;

6 create a Repository

package com.repository;

import com.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
//Inherit jparepository < user, long > 		 Generic I is an entity type and generic II is a primary key type 
public interface UserRepository extends JpaRepository<User,Long> {

7 add SpringBoot to use

part 3 SprintBoot integrates Mybatis

Similarly, Spring Initializ is used to create modules. In addition to some dependency choices of modules, some partners may directly find Mybatis in the Sql tab. If not, import dependencies on pom.xml

These are the first two steps

3 import Mybatis dependency


4 change configuration

Configuration mode (I)

step 1
Add the configuration in application.yml and configure the data source

step 2
Create the mybatis-config.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
        <package name="com.model"/>
        <mapper resource="mappers/UserMapper.xml"></mapper>

step 3
Write xxmapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.UserMapper">
    <select id="selectAll" resultType="com.model.User" >
    <!--Capitalize + Don't use it * -->
        select * from user

Then use it

Configuration mode (II)

You can override mybatis-config.xml through configuration
Write in application.yml:

  	mapper-locations: classpath*:/mappers/*.xml
  	type-aliases-package: com.model

Configuration mode (III)

step 1
Add the scanning annotation @ MapperScan to the mapper package on the startup class
Or add @ Mapper annotation on each Mapper interface class (not recommended)
step 2
Developing Mapper class can write sql directly on the annotation

@Select("select * from user")
<!--It may be necessary to pass parameters @Param-->

The above is all the content to share. Novices enter the station and ask the great God for guidance~~~~~

Topics: MySQL Hibernate Spring Boot