一.前言 H2 是一个用 Java 开发的嵌入式数据库,它本身只是一个类库,即只有一个 jar 文件,可以直接嵌入到应用项目中。H2 主要有如下三个用途: 第一个用途,也是最常使用的用途就在于可以同应用程序打包在一起发布,
首先,给大家看一下我的项目结构
springboot的版本为:2.1.9 具体可以看一下我的pom文件
以下就是我该演示项目的所有依赖,h2的版本交给springboot去进行确定
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.1version>
dependency>
<dependency>
<groupId>com.h2databasegroupId>
<artifactId>h2artifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-Maven-pluginartifactId>
plugin>
plugins>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**
@SpringBootApplication
@MapperScan("com.springboot.*.mapper")
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
配置后,由mybatis进行实体类的映射
public interface CatMapper {
List selectAll();
}
xml version="1.0" encoding="UTF-8"?>
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"Http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springboot.mybatis.mapper.CatMapper">
<select id="selectAll" resultType="com.springboot.mybatis.entity.Cat">
SELECT * from cat
select>
mapper>
与数据库的实体对应
@Data
public class Cat {
private Long id;
private String name;
private Integer age;
private String color;
private Double score;
}
public interface CatService {
String meow();
List list();
}
@Service
public class CatServiceImpl implements CatService {
@Autowired
private CatMapper catMapper;
@Override
public String meow() {
return "瞄";
}
@Override
public List list() {
return catMapper.selectAll();
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisApplicationTests {
@Autowired
private CatService catService;
@Test
public void contextLoads() {
List list = catService.list();
list.forEach(System.out::println);
}
}
Creating a new sqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6caf7803] was not reGIStered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@408543908 wrapping conn0: url=jdbc:h2:mem:test user=ROOT] will not be managed by Spring
==> Preparing: SELECT * from cat
==> Parameters:
<== Columns: ID, NAME, AGE, COLOR, SCORE
<== Row: 1, Jone, 18, 黃色, 0.4
<== Row: 2, Jack, 20, 白色, 0.5
<== Row: 3, Tom, 28, 金色, 0.1
<== Row: 4, Sandy, 21, 紅色, 0.8
<== Row: 5, Billie, 24, 綠色, 0.7
<== Total: 5
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6caf7803]
Cat(id=1, name=Jone, age=18, color=黃色, score=0.4)
Cat(id=2, name=Jack, age=20, color=白色, score=0.5)
Cat(id=3, name=Tom, age=28, color=金色, score=0.1)
Cat(id=4, name=Sandy, age=21, color=紅色, score=0.8)
Cat(id=5, name=Billie, age=24, color=綠色, score=0.7)
运行main方法
浏览器输入
http://localhost:8081/h2-console
账号 root 密码 test,进去
查看
本人长期从事java开发,如果有什么疑问,可以留言,我会及时解答
--结束END--
本文标题: h2数据库作为内存型与springboot+mybatis的案例
本文链接: https://lsjlt.com/news/5188.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0