SpringBoot入门-整合H2测试Mybits DAO

本文介绍了如何在SpringBoot项目中快速整合H2数据库,并通过MyBatis进行数据操作。主要内容包括修改pom.xml文件添加相关依赖、配置数据库连接及初始化表结构、创建启动类、实体类、Mapper接口及测试类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文代码地址:https://github.com/hawkingfoo/java-web

一、概述

上一节中,我们分享了SpringBoot快速整合Mybits的方法。本节中我们将在web项目中引入H2数据库相关的操作。即SpringBoot通过整合MyBatis访问H2数据库。

二、快速整合H2

1、修改pom.xml,添加依赖
     <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!--测试相关依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
2、添加数据库相关配置

src/test/resources目录下,添加application.properties文件。具体内容如下:

# mysql 驱动: h2
spring.datasource.driver-class-name=org.h2.Driver
# h2 内存数据库 库名: test
spring.datasource.url=jdbc:h2:mem:test
# 初始化数据表
spring.datasource.schema=classpath:init_table.sql
spring.datasource.username=
spring.datasource.password=
# 打印 SQL语句, Mapper所处的包
logging.level.com.hawkingfoo.dao=debug

src/test/resources目录下,添加init_table.sql文件。具体内容如下:

DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(1024) NOT NULL,
  `sex` tinyint(1) NOT NULL,
  `addr` varchar(1024) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
3、添加其他代码

这里我们需要创建4个类,第一个是SpringBoot的启动类,在test目录下创建。
- 创建SpringBoot启动类

@SpringBootApplication
@EnableAutoConfiguration
public class ApplicationTest {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationTest.class, args);
    }
}
  • 创建一个Model类
public class Student implements Serializable {
    private int id;
    private String name;
    private int sex;    // 0=male, 1=female
    private String addr;
}

这里需要注意的是,属性的名字要和数据库中的名字保持一致。

  • 创建一个Mapper类
@Component
@Mapper
public interface StudentMapper {
    @Insert("INSERT INTO student (name, sex, addr) VALUES (#{name}, #{sex}, #{addr})")
    int insert(Student stu);
}
  • 创建一个测试类
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {ApplicationTest.class, DataSourceAutoConfiguration.class})
public class StudentMapperTest {
    @Autowired
    private StudentMapper studentMapper;

    @Test
    public void testInsert() {
        Student stu = new Student("a", 0, "x");
        studentMapper.insert(stu);

        List<Student> studentList = studentMapper.selectAll();
        Assert.assertEquals(1, studentList.size());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值