• SpringBoot整合H2新手入门


    添加依赖

        <dependencies>
            <dependency>
                <groupId>com.h2databasegroupId>
                <artifactId>h2artifactId>
                <scope>runtimescope>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-data-jpaartifactId>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    项目结构

    在这里插入图片描述

    配置H2和JPA注入参数

    server:
      port: 9090
    spring:
      datasource:
        driverClassName: org.h2.Driver
        url: jdbc:h2:mem:dbtest
        username: sa
        password: sa
      h2:
        console:
          enabled: true
          path: /h2
          settings:
            web-allow-others: true
      jpa:
        hibernate:
          ddl-auto: update
        show-sql: true
      sql:
        init:
          platform: h2
          schema-locations: classpath:db/schema.sql
          data-locations: classpath:db/data.sql
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    配置数据库的表结构

    配置数据库的表结构schema.sql

    create table if not exists tb_user (
    USER_ID int not null primary key auto_increment,
    USER_NAME varchar(100)
    );
    
    • 1
    • 2
    • 3
    • 4

    初始化数据

    数据文件 data.sql, 默认插入一条

    INSERT INTO tb_user (USER_ID, USER_NAME)
    VALUES (1, '赵一');
    
    • 1
    • 2

    测试

    JDBC URL jdbc:h2:mem:dbtest
    User Name sa
    Password sa
    页面访问路径 localhost:9090/h2
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    案例

    使用H2存放用户表,并通过JPA操作用户数据

    实体类

    给User添加@Entity注解,和@Table注解

    package com.example.springbooth2.entity;
    
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "tb_user")
    public class User {
    
        @Id
        private int userId;
        private String userName;
    
        public int getUserId() {
            return userId;
        }
    
        public void setUserId(int userId) {
            this.userId = userId;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    Dao继承JpaRepository

    package com.example.springbooth2.dao;
    
    import com.example.springbooth2.entity.User;
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public interface UserRepository extends JpaRepository<User, Integer> {
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    service

    package com.example.springbooth2.service;
    
    import com.example.springbooth2.entity.User;
    
    import java.util.List;
    
    public interface UserService {
        void addUser(User user);
    
        List<User> list();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    package com.example.springbooth2.service.impl;
    
    import com.example.springbooth2.dao.UserRepository;
    import com.example.springbooth2.entity.User;
    import com.example.springbooth2.service.UserService;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    @Service
    public class UserServiceImpl implements UserService {
    
        @Resource
        private UserRepository userDao;
    
        @Override
        public void addUser(User user) {
            userDao.save(user);
        }
    
        @Override
        public List<User> list() {
            return userDao.findAll();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    Controller

    package com.example.springbooth2.controller;
    
    import com.example.springbooth2.entity.User;
    import com.example.springbooth2.service.UserService;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    @RestController
    @RequestMapping("/user")
    public class UserController {
    
        @Resource
        private UserService userService;
    
        @PostMapping("add")
        public User add(User user) {
            userService.addUser(user);
            return user;
        }
    
        @GetMapping("list")
        public List<User> list() {
            return userService.list();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    测试

    localhost:9090/user/list
    在这里插入图片描述

  • 相关阅读:
    最新持续更新Crack:LightningChart 行业使用大全
    分享 | NB-IoT智能井盖传感器
    Python 利用pandas 和 matplotlib绘制柱状图
    Nginx简介与作用
    shigen的一些shell脚本分享
    顺应潮流,解放双手,让ChatGPT不废话直接帮忙编写可融入业务可运行的程序代码(Python3.10实现)
    数据库设计与前端框架
    JVM -- JVM内存结构:程序计数器、虚拟机栈、本地方法栈、堆、方法区(二)
    DDD学习笔记
    Scala编程实战 —— 一文学会编码大数据基础案例wordcount
  • 原文地址:https://blog.csdn.net/Java_Fly1/article/details/127748795