目录

Springboot集成H2数据库

目录

Springboot集成H2数据库

记录Springboot集成H2数据库的几种模式 配置文件中分别是嵌入式模式、服务器模式、混合模式。 嵌入式模式:只能自己连接,其他客户端不能连接,可以通过浏览器查看数据 服务器模式:需要单独启动服务,类似mysql数据库,其他客户端可以连接 混合模式:不需要单独启动服务,其他客户端可以连接 yml文件内容如下:

spring:
datasource:
## 嵌入式模式
# url: jdbc:h2:file:D:\IdeaProjectsTB\Test\H2Test\src\main\resources\db\test
## 服务器模式
# url: jdbc:h2:tcp://localhost:9092/D:\IdeaProjectsTB\Test\H2Test\src\main\resources\db\test
##混合模式
url: jdbc:h2:D:\IdeaProjectsTB\Test\H2Test\src\main\resources\db\test;AUTO_SERVER=TRUE
username: sa
password: 123456
driverClassName: org.h2.Driver
h2:
console:
enabled: true

pom文件

xml version="1.0" encoding="UTF-8"?

4.0.0
com.yueyue.h2
H2Test
1.0-SNAPSHOT

org.springframework.boot
spring-boot-starter-parent
2.7.9

8
8
UTF-8



org.springframework.boot
spring-boot-starter-web


com.h2database
h2


org.springframework.boot
spring-boot-starter-jdbc

com.baomidou
mybatis-plus-boot-starter
3.5.10.1

org.projectlombok
lombok

实体类

package com.yueyue.h2.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("tb_user")
public class User {
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
}

mapper

package com.yueyue.h2.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yueyue.h2.entity.User;
public interface UserMapper extends BaseMapper {
}

controller

package com.yueyue.h2.controller;
import com.yueyue.h2.entity.User;
import com.yueyue.h2.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
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
private JdbcTemplate jdbcTemplate;
@Autowired
private UserMapper userMapper;
@PostMapping("/save")
public String save(@RequestBody User user) {
// String insertUserData = "insert into user(name,age) values (?,?)";
// jdbcTemplate.update(insertUserData, user.getName(), user.getAge());
userMapper.insert(user);
return "success";
}
// @PostMapping("/list")
// public List> list() {
// String selectUserData = "select * from user";
// return jdbcTemplate.queryForList(selectUserData);
// }
@PostMapping("/list")
public List list() {
return userMapper.selectList(null);
}
}

启动类

package com.yueyue.h2;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
@MapperScan("com.yueyue.h2.mapper")
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private JdbcTemplate jdbcTemplate;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public void run(String... args) throws Exception {
}
}

测试结果 https://i-blog.csdnimg.cn/direct/d02692b397f24dd79869128078069f2d.png https://i-blog.csdnimg.cn/direct/03adb63775174b3d9e1a35d7c7ab0232.png 代码地址