【Java快速入门】--基于SpringBoot的JPA数据库ORM操作

依赖

<!-- jpa数据库操作 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

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

数据库配置

server:
  port: 8090 #服务端口
  compression: #开启数据压缩
    enabled: true
    min-response-size: 1024
    mime-types: application/json

info:
  version: 1.2

spring:
  servlet:
    multipart:
      max-file-size: 50MB

  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://172.31.3.188:3307/test
    username: root
    password: 123456

  jpa:
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    hibernate:
      ddl-auto: create
    show-sql: true
    database: mysql

定义数据库模型

package com.xxxx.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity // 定义数据库模型
public class UserTable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id") // 字段
    private Long id;
    @Column(name = "username")
    private String username;
    @Column(name = "password")
    private String password;

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

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

测试数据库操作

package com.xxxx;

import javax.annotation.Resource;

import com.xxxx.Dao.UserDao;
import com.xxxx.entity.UserTable;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class UserTest {

    @Resource
    UserDao userDao;

    @Test
    void testJPA() {
        UserTable userData = new UserTable();
        userData.setId(null);
        userData.setPassword("123456");
        userData.setEmail("111");
        userData.setUsername("summerday");
        UserTable user = userDao.save(userData);
        System.out.println("添加用户: " + user);
        UserTable u = userDao.findByUsernameAndPassword("summerday", "123456");
        System.out.println("根据用户名和密码查询用户: " + u);
        long count = userDao.count();
        System.out.println("当前用户数量: " + count);
    }
}

运行展示

...
Hibernate: drop table if exists user_table
Hibernate: create table user_table (id bigint not null auto_increment, email varchar(255), password varchar(255), username varchar(255), primary key (id)) engine=InnoDB
2022-03-16 16:46:25.696  INFO 16434 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-03-16 16:46:25.729  INFO 16434 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-03-16 16:46:27.442  WARN 16434 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-03-16 16:46:34.906  INFO 16434 --- [           main] com.xxxx.UserTest                        : Started UserTest in 22.902 seconds (JVM running for 24.037)
Hibernate: insert into user_table (email, password, username) values (?, ?, ?)
添加用户: com.xxxx.entity.UserTable@1851f19f
Hibernate: select usertable0_.id as id1_0_, usertable0_.email as email2_0_, usertable0_.password as password3_0_, usertable0_.username as username4_0_ from user_table usertable0_ where usertable0_.username=? and usertable0_.password=?
根据用户名和密码查询用户: com.xxxx.entity.UserTable@6cb5a64d
Hibernate: select count(*) as col_0_0_ from user_table usertable0_
当前用户数量: 1
2022-03-16 16:46:37.403  INFO 16434 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值