SpringBoot整合H2新手入门

添加依赖

    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>

项目结构

在这里插入图片描述

配置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

配置数据库的表结构

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

create table if not exists tb_user (
USER_ID int not null primary key auto_increment,
USER_NAME varchar(100)
);

初始化数据

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

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

测试

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;
    }
}

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> {
    
    

}

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();
}

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();
    }
}

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();
    }
}

测试

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

猜你喜欢

转载自blog.csdn.net/Java_Fly1/article/details/127748795