SpringBoot-Mybatis

SpringBoot 整合 Mybatis

  1. SpringBoot-Mybatis

10.1 导入 MyBatis 所需要的依赖


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.2.0

10.2 配置数据库信息

spring.datasource.username=root
spring.datasource.password=aadzj
spring.datasource.url=jdbc:mysql://localhost:3306/userdb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis整合 全限定类别名,关联配置文件
mybatis.type-aliases-package=com.dzj.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

10.3 测试数据库连接

10.4 创建实体类

导入lombok 依赖


    org.projectlombok
    lombok

创建实体类Student

文件路径:java–com–dzj–pojo–Student.java

package com.dzj.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    private String sno;
    private String sname;
    private String sclass;
    private String cname;
}

10.5 创建 Mapper 接口

文件路径:java–com–dzj–mapper–StudentMapper.java

package com.dzj.mapper;

import com.dzj.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

// 这个注解表示了这是一个Mybatis的mapper类
@Mapper
@Repository
public interface StudentMapper {

    List queryStudentList();

    Student queryByStudentId(int sno);

    int addStudent(Student student);

    int updateStudent(Student student);

    int deleteStudent(int sno);
}

10.6 对应的Mapper映射文件

文件路径:resource–mybatis–mapper–StudentMapper.xml


        select * from student

    select * from student where sno=#{sno}

    insert into student(sno,sname,class,cname) values(#{sno},#{sname},#{sclass},#{cname})

    update student set sname=#{sname},class=#{sclass},cname=#{cname} where sno = #{sno}

    delete from student where sno = #{sno}

10.7 maven配置资源过滤问题


        src/main/java

            **/*.xml

        true

10.8 编写 StudentController进行测试

package com.dzj.controller;

import com.dzj.mapper.StudentMapper;
import com.dzj.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class StudentController {

    @Autowired
    private StudentMapper studentMapper;

    @RequestMapping("/listStudent")
    public List queryStudentList(){
        List students = studentMapper.queryStudentList();
        for (Student student : students) {
            System.out.println(student);
        }
        return students;
    }
    @GetMapping("/queryStudent/{sno}")
    public Student queryStudent(@PathVariable("sno")int sno){
        Student student = studentMapper.queryByStudentId(sno);
        return student;
    }

    @GetMapping("/addStudent")
    public String addStudent(){
        int i = studentMapper.addStudent(new Student("105", "dengzi", "5班", "English"));
        return "添加成功";
    }

    @GetMapping("/deleteStudent/{id}")
    public String deleteStudent(@PathVariable("id")int id){
        studentMapper.deleteStudent(id);
        return "删除成功";
    }

    @GetMapping("/updateStudent")
    public String updateStudent(){
        studentMapper.updateStudent(new Student("101","wenliu","2班","语文"));
        return "修改成功";
    }
}

Original: https://www.cnblogs.com/aadzj/p/15636759.html
Author: 小公羊
Title: SpringBoot-Mybatis

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/612315/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • supervisord 进程管理利器

    Supervisor概述 ​ supervisor是一个 Client/Server模式的系统,允许用户在类unix操作系统上监视和控制多个进程,或者可以说是多个程序。superv…

    Linux 2023年5月27日
    090
  • Linux实用命令(更新中)

    参数 描述 样例 样例说明 -l 详细文件信息 -t 修改时间排序 -r 倒序排列 -h 可读的文件大小单位 -a 显示隐藏文件 常用用法 ls -ltr zip 参数 描述 样例…

    Linux 2023年5月27日
    085
  • 面试题汇总

    JAVA 基础 1.java 性能优化 ①尽量指定类、方法的final 修饰符 ②尽量重用对象 ③尽可能使用局部变量 ④及时关闭流 ⑤尽量减少对变量的重复计算 ⑥尽量采用懒加载的策…

    Linux 2023年6月7日
    083
  • 【凸优化】3 多面体,单纯形,半正定锥

    1 多面体 Polyhedra 定义:多面体为一系列的(有限个)线性等式和不等式的解集: [\mathcal{P}={x|a_j^T x \leq b_j, j=1,……

    Linux 2023年6月7日
    090
  • python入门基础知识四(字典与集合)

    dict_name = {key1:value1,key2,value2,…} 空字典:dict_name = {} or dict_name = dict() 字典的…

    Linux 2023年6月7日
    057
  • 分布式运算中,高精度校时器的畅想

    这是我写的,带有一定的娱乐性质的文章。你可以把它理解为神经病的yy。昨天,我看了个帖子《Facebook工程师开发开源自计时设备 仅需一个PCIe插槽即可工作》,有感而发写了此文。…

    Linux 2023年6月14日
    088
  • 做任何事(决策)之前都要先考虑成本,再考虑收益

    所谓成本,就是我们在做一件事情时所付出的代价。 这个代价,或者说这个成本,有多有少,有显性有隐性,有我们知道的成本,也有我们不知道的成本。一切都是有成本的。 成本都有什么呢?一件事…

    Linux 2023年6月14日
    084
  • cpp-函数

    1.基础概念 形参:用在定义、申明处的参数,用于说明参数的类型、名称 实参:用在函数调用,用…

    Linux 2023年6月7日
    0109
  • Linux 中 图片的产生与查看

    使用场景 当在 Linux 的控制台想要显示一张图片,使用matplotlib.plt.plot() 和matplotlib.plt.show() 会报错。此时可以曲线救国,不直接…

    Linux 2023年6月7日
    067
  • 软件测试基础理论

    软件基础的理论 一, 什么是软件产品 它是一个逻辑产品,没有实体,包括程序,文档和数据,需要通过终端设备才能体现出来功能和作用 二, 软件产品的中间过程文档 客户需求 &#…

    Linux 2023年6月7日
    085
  • centos7用rpm安装mysql5.7【初始用yum安装发现下载非常慢,就考虑本地用迅雷下载rpm方式安装】

    1.下载 4个rpm包 mysql-community-client-5.7.26-1.el7.x86_64.rpmmysql-community-common-5.7.26-1….

    Linux 2023年6月7日
    087
  • ACL和NAT

    NAT 概述: NAT(网络地址翻译)一个数据包目的ip或者源ip为私网地址, 运营商的设备 无法转发数据。 NAT工作机制: 一个数据包从企业内网去往公网时,路由器将数据包当 中…

    Linux 2023年6月6日
    088
  • Python 之Memcache中间件

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载,它通过在内存中缓存数据和减少读取数据库的次数,从而提高动态数据库驱动网站的速度。Mem…

    Linux 2023年6月14日
    090
  • 《深度工作:如何有效使用每一点脑力》读后感

    空闲时间阅读了一下《深度工作:如何有效使用每一点脑力》,作为一个沉迷网络的人,已经很难有聚精会神的时候,所以阅读此书,记录一下读后感,争取应用到生活当中。全书分为两个方面进行说明:…

    Linux 2023年6月7日
    086
  • 汉诺塔

    设计并实现一个游戏:汉诺塔。完成这个实验,涉及C++面向对象编程以及基本的数据结构知识(如栈和队列)但具此次实现并没有使用STL库。 1. 汉诺塔问题 汉诺塔是一个著名的数学问题。…

    Linux 2023年6月13日
    083
  • Zabbix-(1)安装

    环境: VMware Workstation Pro 16.0 版本 系统 Centos7 …

    Linux 2023年6月13日
    073
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球