SpringBoot---------整合Mybatis

embedded/2024/10/19 9:33:19/

目录

第一步:首先引入依赖

第二步:连接数据库

第三步:配置application.yml文件

第四步:开始编码实现


第一步:首先引入依赖

        //mysql依赖<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>//mybatis依赖<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.3.1</version></dependency>//德鲁伊druid数据源依赖<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.6</version></dependency>

第二步:连接数据库

 我这里使用的是安装在Linux中的Mysql,与在Windows上的操作是一样的

第三步:配置application.yml文件

spring:datasource://配置mysqldriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://192.168.136.132/mydbusername: rootpassword: 1234//配置Druid数据源type: com.alibaba.druid.pool.DruidDataSource

第四步:开始编码实现

 这里我便于快速开发,只写了Controller层和Mapper层,Service层一般用于数据处理,暂时先不写,实体类的话这里也不做展示,很简单

 Controller层:

package com.example.springboot_learn.Controller;import com.example.springboot_learn.ApplicationData;
import com.example.springboot_learn.JsonResult;
import com.example.springboot_learn.Pojo.Book;
import com.example.springboot_learn.Pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.springboot_learn.mapper.studentmapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class BookController {//依赖注入Mapper层接口@Autowiredprivate studentmapper sm;//设置API,并且从Mapper层获取数据返回给前端@GetMapping("/selectstudent")public JsonResult SelectStudent(){List<Student> sdlist=sm.list();System.out.println(sdlist);return new JsonResult(sdlist);}}

Mapper层:

package com.example.springboot_learn.mapper;import com.example.springboot_learn.Pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;//Mapper注解将其放到Spring容器中
@Mapper
public interface studentmapper {//select注解进行查询数据,此外还有delete,insert,update都是一样的用法@Select("select * from mydb.student")List<Student> list();}

 启动项目,根据API成功获取到从数据库查询到的数据


http://www.ppmy.cn/embedded/23993.html

相关文章

网络安全之数据库基础篇(基础入门)

目录 一&#xff0c;操作数据库 1&#xff0c;查询所有数据库 2&#xff0c;创建数据库 3&#xff0c;查看数据库是否被创建 4&#xff0c;查看数据库的字符集 5&#xff0c;修改数据库的字符集 6&#xff0c;删除数据库 7&#xff0c;使用数据库 8&#xff0c;查看当前…

上位机开发-PyQt5

PyQt是一套Python的GUI开发框架&#xff0c;即图形用户界面开发框架 其中PyQt是Qt(C语言实现的)为Python专门提供的扩展 PySide官网&#xff1a;Qt for Python 插件安装 pip install 插件名字 # 安装 pip uninstall 插件名字 # 卸载 pip install 插件名字 -i 指定下载的镜…

PARL学习

1、更换源&#xff1a; pip config set global.index-url https://mirror.baidu.com/pypi/simple 2、 pip install paddlepaddle 3、 pip install parl 4、 pip install gym 5、 git clone --depth1 https://github.com/PaddlePaddle/PARL.git 6、 cd PARL/example…

【基础算法】前缀和

1.【模板】前缀和 前缀和 思路&#xff1a; 板子题&#xff0c;思路前缀和数组记录 。 虽是板子&#xff0c;但以后切记不要死记模版。 主要要开long long&#xff0c;防溢出。 #include <iostream> using namespace std;#include<vector>int main() {int n …

MIS微调SAM模型实时交互UI界面

前言 SAM模型的基本介绍可见SAM&#xff08;Segment Anything Model&#xff09;大模型使用--point prompt_sam大模型-CSDN博客 针对Meta团队去年发布的SAM大模型在医学图像分割领域表现性能较差的情况&#xff0c;笔者收集了一些MIS领域的数据集对SAM的架构进行fine tune&am…

数据分析案例-全球表面温度数据可视化与统计分析

&#x1f935;‍♂️ 个人主页&#xff1a;艾派森的个人主页 ✍&#x1f3fb;作者简介&#xff1a;Python学习者 &#x1f40b; 希望大家多多支持&#xff0c;我们一起进步&#xff01;&#x1f604; 如果文章对你有帮助的话&#xff0c; 欢迎评论 &#x1f4ac;点赞&#x1f4…

Linux详解:进程等待

文章目录 进程等待等待的必要性进程等待的方法waitwaitpid获取子进程status阻塞等待 与 非阻塞等待 进程等待 等待的必要性 子进程退出&#xff0c;父进程不进行回收的话&#xff0c;就可能造成僵尸进程&#xff0c;进而造成内存泄露 如果进程进入了僵尸状态&#xff0c;kill…

Unity DOTS1.0 入门(7) BlobAsset 核心机制概述

BlobAsset 概述&#xff1a; Blob Asset是一种特殊的数据结构&#xff0c;用于存储不可变的、只读的、大量的数据。Blob是Binary Large Object的缩写&#xff0c;意为二进制大对象。Blob Asset的主要特点是它们是不可变的&#xff0c;并且可以在内存中任意移动&#xff0c;这…