Importing dependencies The version that defines a lot of dependencies here uses the parent project to import a lot of dependencies using the initiator

<! Define the version number of a large number of dependencies -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2. RELEASE</version>
        <relativePath/>
    </parent>


    <dependencies>

    <! Initiators have a lot of dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <! -- springBoot JPA startup dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <! Mysql connection driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>


    </dependencies>
Copy the code

The configuration file configures the connection properties of the current JPA

Hibernate. Properties configuration file

hibernate.dialect.storage_engine=innodb
Copy the code

Application. Yml configuration file

logging:
  level:
    com.atguigu.dao: debug # config log
spring:
  datasource:
    username: root
    password: root
    url: JDBC: mysql: / / 192.168.137.123:3306 / springboot? useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    database: mysql
    show-sql: true
    generate-ddl: true
    hibernate:
      # 1. Table not being created; If the class structure is inconsistent with the table structure, the table will be updated. The class structure is identical to the table structure and does nothing.
      ddl-auto: update
      naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
server:
  port: 8888
Copy the code

Creating an entity Class

An entity class maps a table to create a table forward on server startup based on the Settings of the entity class

import javax.persistence.*;


// JPA: Java Persistence API Java Persistence specification lombok is recommended here
// Declare the table name
@Table(name = "t_user")
@Entity
@Data
public class User {

    @Id // Declare primary key
    @GeneratedValue(strategy = GenerationType.IDENTITY)  // auto_increment Automatically increments
    @Column(name = "id")// The corresponding table field
    private Long id;
    
    // Field name length cannot be empty. The current field value is unique
    @Column(name = "username",length = 64,nullable = false,unique = true)
    private String username;

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

    // omit the @column annotation, which will be mapped automatically. The field name is the same as the member variable name.
    //@Column(name = "name")
    private String name;

    @Transient // Temporary attributes. No mapping to table fields is required. This field will not be created in the table
    private int age;

    private String email ;

Copy the code

Dao layer code writing

Direct inheritance interface has been written in the basic censor

import org.springframework.data.jpa.repository.JpaRepository;

public interface UserDao extends JpaRepository<User.Long> {}Copy the code

Service layer code writing


/ / interface
import com.wang.entity.User;

import java.util.List;

public interface UserService {
    List<User> findAll(a);
}



// Interface implementation class
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    UserDao userDao;

    @Override
    public List<User> findAll(a) {
        returnuserDao.findAll(); }}Copy the code

The controller layer of writing

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/findAll")
    public List<User> findAll(a){
        returnuserService.findAll(); }}Copy the code

Query result display