SpringBoot入門-整合H2測試Mybits DAO

聲明:原創(chuàng)文章,轉(zhuǎn)載請注明出處。http://www.itdecent.cn/p/ecf51b2e53a1
本文代碼地址:https://github.com/hawkingfoo/java-web

一、概述

上一節(jié)中,我們分享了SpringBoot快速整合Mybits的方法。本節(jié)中我們將在web項目中引入H2數(shù)據(jù)庫相關(guān)的操作。即SpringBoot通過整合MyBatis訪問H2數(shù)據(jù)庫。

二、快速整合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>

        <!--測試相關(guān)依賴-->
        <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、添加數(shù)據(jù)庫相關(guān)配置

src/test/resources目錄下,添加application.properties文件。具體內(nèi)容如下:

# mysql 驅(qū)動: h2
spring.datasource.driver-class-name=org.h2.Driver
# h2 內(nèi)存數(shù)據(jù)庫 庫名: test
spring.datasource.url=jdbc:h2:mem:test
# 初始化數(shù)據(jù)表
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文件。具體內(nèi)容如下:

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、添加其他代碼

這里我們需要創(chuàng)建4個類,第一個是SpringBoot的啟動類,在test目錄下創(chuàng)建。

  • 創(chuàng)建SpringBoot啟動類
@SpringBootApplication
@EnableAutoConfiguration
public class ApplicationTest {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationTest.class, args);
    }
}
  • 創(chuàng)建一個Model類
public class Student implements Serializable {
    private int id;
    private String name;
    private int sex;    // 0=male, 1=female
    private String addr;
}

這里需要注意的是,屬性的名字要和數(shù)據(jù)庫中的名字保持一致。

  • 創(chuàng)建一個Mapper類
@Component
@Mapper
public interface StudentMapper {
    @Insert("INSERT INTO student (name, sex, addr) VALUES (#{name}, #{sex}, #{addr})")
    int insert(Student stu);
}
  • 創(chuàng)建一個測試類
@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());
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容