Jpa operates the database

Note: the database uses the native database, the following is the construction of the table and initialization data:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT 'primary key ID',
  `dpt_id` bigint(0) NULL DEFAULT NULL COMMENT 'department id',
  `name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'name',
  `age` int(0) NULL DEFAULT NULL COMMENT 'age',
  `email` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'email',
  `head_img` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT 'avatar'.PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 0 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = 'User class' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1.1.'Jone'.18.'[email protected]'.'e');
INSERT INTO `user` VALUES (2.1.'Jack'.20.'[email protected]'.'d');
INSERT INTO `user` VALUES (3.1.'Tom'.28.'[email protected]'.'c');
INSERT INTO `user` VALUES (4.1.'Sandy'.21.'[email protected]'.'b');
INSERT INTO `user` VALUES (5.1.'Billie'.24.'[email protected]'.'a');

SET FOREIGN_KEY_CHECKS = 1;
Copy the code

Configure the data source in IDEA: idea–>view–> Tool Windows –> Database

Create the project and add the following dependencies

<! --springboot Web depend on -- -- >
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<! --springboot Operation database using JPA dependencies -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! Mysql database connection driver -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<! --lombok A artifact, to be introduced later -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>
Copy the code

Add the following database configuration to application.yml under the created project

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/demo? charset=UTF-8&serverTimezone=GMT%2B8
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
Copy the code

Set up the project directory structure as follows

The user code:

@Entity// Identifies an Entity class that corresponds to a database table
@Data// A note on lombok artifact, to be covered later
@Table ( name ="user" )// Which table in the database does this class correspond to
public class User implements Serializable {

   private static final long serialVersionUID =  829933141479418804L;

   /** * primary key ID */
   @Id// This field is the primary key of the database table
       @Column(name = "id" )// Which field in the table does this attribute correspond to
   @GeneratedValue(strategy= GenerationType.IDENTITY)// The primary key uses the database increment mode
   private Long id;

   /** * Department ID */
       @Column(name = "dpt_id" )
   private Long dptId;

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

   /** * age */
       @Column(name = "age" )
   private Long age;

   /** * email */
       @Column(name = "email" )
   private String email;

   /** ** avatar */
       @Column(name = "head_img" )
   private String headImg;

}
Copy the code

The strategy attribute of the @GeneratedValue annotation provides four values:

  1. – AUTO: The primary key is controlled by the program and is the default option.
  2. – IDENTITY: The primary key is automatically generated by the database, that is, the database ID grows automatically. Oracle does not support this mode.
  3. – SEQUENCE: generates the primary key from the SEQUENCE of the database. The @SequenceGenerator annotation specifies the SEQUENCE name. Mysql does not support this method.
  4. – TABLE: generates primary keys for specific database tables. This policy facilitates database migration.

UserJpa code:

@Repository// Indicates that this is a Repository class that manipulates databases

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

UserController code:

@RestController
@RequestMapping("user")

public class UserController {

    @Resource
    private UserJpa userJpa;

    @GetMapping("findAll")// Find all data
    public List<User> findAll(a){
        return this.userJpa.findAll();
    }

    @GetMapping("get")// Press the primary key to find data
    public User get(@RequestParam("id")Long id){
        return this.userJpa.findById(id).get(); }}Copy the code

Start the project, then test with Postman:

Query the specified id of the data

The whole process did not write a SQL, data so query out. Okay, that’s it for this video. The next section covers saving, deleting, updating, and customizing SQL

More original reading