SpringBoot入门-整合H2测试Mybits DAO

0.127字数 262阅读 481

声明:原创文章,转载请注明出处。https://www.jianshu.com/p/ecf51b2e53a1
本文代码地址: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());
    }
}

推荐阅读更多精彩内容