springboot-集成mybaits和h2

本文详细介绍如何在Spring Boot项目中整合MyBatis和H2数据库,包括配置依赖、YML配置、数据库初始化、XML映射文件、Mapper接口、DAO层、Service层及Controller层的实现,并提供接口调用测试。
  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. 接口调用测试

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

基于Vue的家庭理财系统有诸多特点优势。Vue.js采用响应式数据绑定、虚拟DOM、组件化等现代化技术,提供了灵活、高效、易于维护的开发模式。当数据发生变化时,能自动更新UI,开发者可专注于数据处理[^1]。 Vue采用与Angular类似的双向数据绑定机制,数据模型视图自动同步,简化了数据与界面交互的逻辑。其核心思想是组件化开发,每个组件是独立、可复用的代码片段,包含HTML、CSSJavaScript,简化了大型项目的开发维护。Vue被设计为渐进式框架,可根据需要逐步引入功能,既可以简单地作为页面的小部分使用,也能通过Vue CLIVue Router、Vuex等工具构建复杂的单页面应用[^2]。 Vue使用直观的HTML模板语法,声明式地将DOM绑定到底层数据模型,模板语法简单易懂,入门门槛较低。它还使用虚拟DOM,在更新界面时更加高效,会计算出最小的变化量,尽量减少对真实DOM的操作,提高性能。其数据模型是响应式的,数据变化时会触发相应组件重新渲染,自动处理视图更新。此外,Vue提供丰富的指令,如v - if、v - for、v - bind等,可轻松实现条件渲染、列表渲染、数据绑定等功能[^2]。 有基于springboot + vue.js的家庭理财系统,附带文章源码部署视频讲解等,持久层框架使用MyBaits;也有基于Java的家庭理财系统设计与实现,包含源码、文档、部署等内容;还有基于SpringBootVue的家庭理财预算系统,并且系统测试在其设计与实现中起着至关重要的作用,通过功能测试、性能测试稳定性测试等方法,可确保系统正常运行并提供良好的用户体验[^1][2][3]。 基于ssm的家庭理财管理系统设计开发实现vue,其核心模块包括任务管理模块(负责任务的创建、分配、状态追踪)、权限控制模块(基于RBAC模型的权限管理系统)、工作流引擎(使用Activiti或Flowable实现任务流转)、消息通知模块(集成邮件/站内信通知机制)、统计报表模块(提供任务完成情况的数据可视化)[^4]。 ### 示例代码 以下是一个简单的Vue组件示例,用于显示家庭理财的收入信息: ```vue <template> <div> <h2>家庭理财收入信息</h2> <ul> <li v-for="income in incomes" :key="income.id">{{ income.name }}: {{ income.amount }} 元</li> </ul> </div> </template> <script> export default { data() { return { incomes: [ { id: 1, name: '工资收入', amount: 8000 }, { id: 2, name: '投资收益', amount: 2000 } ] }; } }; </script> <style scoped> h2 { color: #333; } ul { list-style-type: none; padding: 0; } li { margin-bottom: 5px; } </style> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值