SpringBoot整合Mybatis,实现简单的增删改查

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: SpringBoot整合Mybatis,实现简单的增删改查



这是最终的文件目录结构:

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200608070844727.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)

**新建maven工程**


1,配置pom.xml


```yaml

<parent>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-parent</artifactId>

   <version>2.1.5.RELEASE</version>

</parent>


<dependencies>

   <dependency>

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-atarter-web</artifactId>

   </dependency>


   <dependency>

       <groupId>org.mybatis.spring.boot</groupId>

       <artifactId>mybatis-spring-boot-starter</artifactId>

       <version>1.3.1</version>

   </dependency>


   <dependency>

       <groupId>mysql</groupId>

       <artifactId>mysql-connector-java</artifactId>

       <version>8.0.15</version>

   </dependency>

   <dependency>

       <groupId>org.projectlomok</groupId>

       <artifactId>lombok</artifactId>

   </dependency>

</dependencies>

```


2,创建数据表


```sql

create table studentMB(

  id int primary key auto_increment,

  name varchar(11),

  score double,

  birthday date,

 );

```


3,创建对应的实体类



```java

package com.shuang.entity;


import lombok.Data;


import java.util.Date;


@Data

public class Student {

   private Long id;

   private String name;

   private Double score;

   private Date birthday;

}

```


4、创建StudentRepository


```java

package com.shuang.repository;


import com.shuang.entity.Student;


import java.util.List;


public interface StudentRepository {

   public List<Student> findAll();

   public Student findById(Long id);

   public void save(Student student);

   public void update(Student student);

   public void deleteById(Long id);

}

```


5,在resources/mapping路径下创建StudentRepository对应的Mapper.xml


```yaml


<?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">


<!-- namespace:该mapper.xml映射文件的唯一标识 -->

<mapper namespace="org.shuang.repository.StudentRepository">

   <select id="findAll" resultType="Student">

       select * from student;

   </select>


   <select id="findById" parameterType="java.lang.Long"  resultType="Student">

       select * from student where id=#{id};

   </select>


   <insert id="save" parameterType="Student">

       insert into student(name,score,birthday) values(#{name},#{score},#{birthday});

   </insert>


   <update id="update" parameterType="Student">

       update student set name=#{name},score=#{score},birthday=#{birthday} where id=#{id};

   <update>


   <delete id="deleteById" parameterType="java.lang.Long">

       delete from student where id=#{id};

   </delete>

</mapper>

```



6,创建StudentHandler,注入StudentRepository


```java

package com.shuang.controller;


import com.shuang.entity.Student;

import com.shuang.repository.StudentRepository;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;


import java.util.List;


@RestController

public class StudentHandler {

   @Autowired

   private StudentRepository studentRepository;


   @GetMapping("/findAll")

   public List<Student> findAll(){

       return studentRepository.findAll();

   }

   @PostMapping("/findById/{id}")

   public void save(@RequestBody Student student){

       studentRepository.save(student);

   }

   @PutMapping("/update")

   public void update(@RequestBody Student student){

       studentRepository.update(student);

   }

   @DeleteMapping("/deleteById/{id}")

   public void deleteById(@PathVariable("id") Long id){

       studentRepository.deleteById(id);

   }

```



7,application.yml


```yaml

spring:

 datasource:

   url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8

   &serverTimezone=GMT

   username: shuang

   password: mysql215311?

   driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:

 mapper-locations: classpath:/mapping/*.xml

 type-aliases-package: com.shuang.entity

 

```

```


```


8,创建Application


```java

package com.shuang;


import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication

@MapperScan("com.shuang.repository")

public class Application {

   public static void main(String [] args){

       SpringApplication.run(Application.class,args);

   }

}

```

启动:使用端口就可以访问了


![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200608070752301.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)





相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
6天前
|
缓存 前端开发 Java
【Java面试题汇总】Spring,SpringBoot,SpringMVC,Mybatis,JavaWeb篇(2023版)
Soring Boot的起步依赖、启动流程、自动装配、常用的注解、Spring MVC的执行流程、对MVC的理解、RestFull风格、为什么service层要写接口、MyBatis的缓存机制、$和#有什么区别、resultType和resultMap区别、cookie和session的区别是什么?session的工作原理
【Java面试题汇总】Spring,SpringBoot,SpringMVC,Mybatis,JavaWeb篇(2023版)
|
7天前
|
Java 数据库连接 数据格式
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
IOC/DI配置管理DruidDataSource和properties、核心容器的创建、获取bean的方式、spring注解开发、注解开发管理第三方bean、Spring整合Mybatis和Junit
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
|
30天前
|
Java 数据库连接 Spring
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
文章是关于Spring、SpringMVC、Mybatis三个后端框架的超详细入门教程,包括基础知识讲解、代码案例及SSM框架整合的实战应用,旨在帮助读者全面理解并掌握这些框架的使用。
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
|
11天前
|
前端开发 JavaScript Java
技术分享:使用Spring Boot3.3与MyBatis-Plus联合实现多层次树结构的异步加载策略
在现代Web开发中,处理多层次树形结构数据是一项常见且重要的任务。这些结构广泛应用于分类管理、组织结构、权限管理等场景。为了提升用户体验和系统性能,采用异步加载策略来动态加载树形结构的各个层级变得尤为重要。本文将详细介绍如何使用Spring Boot3.3与MyBatis-Plus联合实现这一功能。
47 2
|
20天前
|
SQL XML Java
mybatis :sqlmapconfig.xml配置 ++++Mapper XML 文件(sql/insert/delete/update/select)(增删改查)用法
当然,这些仅是MyBatis功能的初步介绍。MyBatis还提供了高级特性,如动态SQL、类型处理器、插件等,可以进一步提供对数据库交互的强大支持和灵活性。希望上述内容对您理解MyBatis的基本操作有所帮助。在实际使用中,您可能还需要根据具体的业务要求调整和优化SQL语句和配置。
26 1
|
22天前
|
Java 数据库连接 测试技术
SpringBoot 3.3.2 + ShardingSphere 5.5 + Mybatis-plus:轻松搞定数据加解密,支持字段级!
【8月更文挑战第30天】在数据驱动的时代,数据的安全性显得尤为重要。特别是在涉及用户隐私或敏感信息的应用中,如何确保数据在存储和传输过程中的安全性成为了开发者必须面对的问题。今天,我们将围绕SpringBoot 3.3.2、ShardingSphere 5.5以及Mybatis-plus的组合,探讨如何轻松实现数据的字段级加解密,为数据安全保驾护航。
72 1
|
30天前
|
Web App开发 前端开发 关系型数据库
基于SpringBoot+Vue+Redis+Mybatis的商城购物系统 【系统实现+系统源码+答辩PPT】
这篇文章介绍了一个基于SpringBoot+Vue+Redis+Mybatis技术栈开发的商城购物系统,包括系统功能、页面展示、前后端项目结构和核心代码,以及如何获取系统源码和答辩PPT的方法。
|
30天前
|
SQL Java 关系型数据库
SpringBoot 系列之 MyBatis输出SQL日志
这篇文章介绍了如何在SpringBoot项目中通过MyBatis配置输出SQL日志,具体方法是在`application.yml`或`application.properties`中设置MyBatis的日志实现为`org.apache.ibatis.logging.stdout.StdOutImpl`来直接在控制台打印SQL日志。
SpringBoot 系列之 MyBatis输出SQL日志
|
1月前
|
Java 关系型数据库 MySQL
1、Mybatis-Plus 创建SpringBoot项目
这篇文章是关于如何创建一个SpringBoot项目,包括在`pom.xml`文件中引入依赖、在`application.yml`文件中配置数据库连接,以及加入日志功能的详细步骤和示例代码。
|
15天前
|
Java 数据库连接 开发者
MyBatis-Plus整合SpringBoot及使用
MyBatis-Plus为MyBatis提供了强大的增强,使得在Spring Boot项目中的数据访问层开发变得更加快捷和简便。通过MyBatis-Plus提供的自动CRUD、灵活的查询构造器和简洁的配置,开发者
29 0