spring+mybatise+h2 database

添加依賴:java

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-autoconfigure</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>

yml 配置文件:
test/resources/application.yml
# 服務器端口號
server:
  port: 8081
# 是否生成ddl語句
spring:
  datasource:
    url: jdbc:h2:mem:testdbsa
    schema: classpath:schema.sql # 指定生成數據庫的schema文件位置
    data: classpath:data.sql  # 指定插入數據庫語句的腳本位置
    driver-class-name: org.h2.Driver
    data-username: root
    data-password:
  h2:
    console:
      settings.web-allow-others: true
      path: /h2-console
      enabled: true
      settings.trace: true
test/resources/schema.sql
CREATE TABLE test_table( id char(20) not null primary key, name char(20), age INTEGER );
test/resources/data.sql(若data.sql文件內容爲空,會報錯,因此默認插入一條數據)
insert into test_table(id, name, age) values ('1', 'zhangsa', 28)

DemoDao.javaweb

package com.example.demo.dao; import com.example.demo.entity.People; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository;  @Repository @Mapper public interface DemoDao { @Insert("insert into test_table(id, name, age) values (#{id}, #{name}, #{age})") void insert(People people); @Select("select * from test_table where id= #{id}") People findById(Integer id); }
TestConfig.java

package com.example.demo; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; @ComponentScan("com.example.demo") @EnableAutoConfiguration @SpringBootConfiguration public class TestConfig { }

DemoApplicationTests.javaspring

 

package com.example.demo; import com.example.demo.dao.DemoDao; import com.example.demo.entity.People; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes={TestConfig.class}) //@SpringBootTest(classes = TestConfig.class) public class DemoApplicationTests { @Autowired private DemoDao demoDao; @Test public void contextLoads() { People p1 = demoDao.findById(1); System.out.println("name=" + p1.getName()); System.out.println(p1.toString()); People p = new People(); p.setAge(18); p.setId(3); p.setName("yiyi"); demoDao.insert(p); People p2 = demoDao.findById(3); System.out.println(p2.toString()); } }
相關文章
相關標籤/搜索