springboot-集成mybaits和h2

  1. 项目结构
    在这里插入图片描述
  2. 加入依赖(mybatis和h2)
<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.3.2</version>
</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>
  1. yml文件配置
server:
  port: 8080
#************H2  Begin****************
spring:
  datasource:
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:test
    username:
    password:
    schema: classpath:db/schema.sql
    data: classpath:db/data.sql
  h2:
    console:
      settings:
        web-allow-other: true
        trace: true
      path: /h2-console
      enabled: true
#************H2  End***************
#************mybatis Start***************
mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.example.demo.entity
#************mybatis End***************
  1. 数据库初始化文件
schema.sql:
drop table if exists student;
create table student (id int primary key auto_increment, name varchar, addr varchar);

data.sql:
insert into student (name, addr) values ('张三', '山东');
insert into student (name, addr) values ('李四', '山西');
insert into student (name, addr) values ('王五', '河南');
insert into student (name, addr) values ('赵六', '河北');

  1. 启动类
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
  1. xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.StudentMapper">
    <select id="getList" resultType="student">
        select id, name, addr
        from student
    </select>
    <insert id="add" parameterType="student">
        INSERT INTO student (name, addr) VALUES (#{name}, #{addr})
    </insert>
</mapper>
  1. mapper
import com.example.demo.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Mapper
public interface StudentMapper {
    int add(Student stu);

    List<Student> getList();
}
  1. dao
import com.example.demo.entity.Student;
import com.example.demo.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StudentDao {
    @Autowired
    private StudentMapper mapper;

    public List<Student> getList() {
        return mapper.getList();
    }

    public Integer add(Student stu) {
        return mapper.add(stu);
    }
}
  1. service
import com.example.demo.dao.StudentDao;
import com.example.demo.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {
    @Autowired
    private StudentDao dao;

    public List<Student> getList() {
        return dao.getList();
    }

    public Integer add(Student stu) {
        return dao.add(stu);
    }

}
  1. controller
import com.example.demo.entity.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("student")
public class StudentCtl {
    @Autowired
    private StudentService service;

    @GetMapping("getList")
    public List<Student> getList() {
        return service.getList();
    }

    @PostMapping("add")
    public Integer add(Student stu) {
        return service.add(stu);
    }
}
  1. db2数据库访问
http://localhost:8080/h2-console

在这里插入图片描述

  1. 接口调用测试

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值