时间:2021-07-01 10:21:17 帮助过:13人阅读
在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