springboot中使用h2数据库(内存模式)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了springboot中使用h2数据库(内存模式),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2911字,纯文字阅读大概需要5分钟。
内容图文
![springboot中使用h2数据库(内存模式)](/upload/InfoBanner/zyjiaocheng/516/90b2210ae3594a80b91a060a6850b7db.jpg)
在pom文件中,一般我都包含了下面一段
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/**</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.tld</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
截图:
h2数据库的配置:application-h2.properties
#spring.datasource.url = jdbc:h2:file:~/.h2/testdb spring.datasource.url=jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000 spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.datasource.schema=classpath:db/schema.sql spring.datasource.data=classpath:db/data.sql
db/data.sql内容:
insert into mytest(name) values(‘TheoryDance‘);
schema.sql内容:
create table mytest(id int primary key auto_increment, name varchar(20) not null);
在测试类中添加一个测试方法
package com.grand.mysql_handler; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.grand.mysql_handler.mapper.SystemMapper; @SpringBootTest @RunWith(SpringRunner.class) public class MyRestTest2 { @Resource private SystemMapper systemMapper; @Test public void testH2() { List<Map<String,Object>> list = systemMapper.selectBySql("select * from mytest"); System.out.println(list); } }
其中SysMapper.java内容如下(使用的Mybatis连接数据库):
package com.grand.mysql_handler.mapper; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; @Mapper public interface SystemMapper { @Insert("${sql}") int insertBySql(@Param("sql")String sql); @Delete("${sql}") int deleteBySql(@Param("sql")String sql); @Update("${sql}") int updateBySql(@Param("sql")String sql); @Select("${sql}") List<Map<String,Object>> selectBySql(@Param("sql")String sql); @Select("${sql}") Map<String,Object> selectOneBySql(@Param("sql")String sql); }
测试结果:
springboot中使用h2数据库(内存模式)
标签:截图 imp uil param plugins handle 版本 group jar
本文系统来源:https://www.cnblogs.com/TheoryDance/p/11941180.html
内容总结
以上是互联网集市为您收集整理的springboot中使用h2数据库(内存模式)全部内容,希望文章能够帮你解决springboot中使用h2数据库(内存模式)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。