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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 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)





相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
6天前
|
SQL Java 数据库连接
SpringBoot整合Mybatis
SpringBoot整合Mybatis
34 2
|
4天前
|
算法 Java 数据库连接
Spring+MySQL+数据结构+集合,Alibaba珍藏版mybatis手写文档
Spring+MySQL+数据结构+集合,Alibaba珍藏版mybatis手写文档
|
6天前
|
Java 数据库连接 Spring
Spring 整合mybatis
Spring 整合mybatis
21 2
|
6天前
|
SQL Java 数据库连接
【JavaEE】懒人的福音-MyBatis框架—[单表]增删改查等常规操作(下)
【JavaEE】懒人的福音-MyBatis框架—[单表]增删改查等常规操作
8 0
|
6天前
|
SQL 前端开发 Java
【JavaEE】懒人的福音-MyBatis框架—[单表]增删改查等常规操作(上)
【JavaEE】懒人的福音-MyBatis框架—[单表]增删改查等常规操作
8 0
|
6天前
|
JSON Java 数据格式
nbcio-boot升级springboot、mybatis-plus和JSQLParser后的LocalDateTime日期json问题
nbcio-boot升级springboot、mybatis-plus和JSQLParser后的LocalDateTime日期json问题
|
6天前
|
SQL Java 数据库连接
【mybatis】动态sql之批量增删改查
【mybatis】动态sql之批量增删改查
13 0
|
6天前
|
SQL Java 数据库连接
15:MyBatis对象关系与映射结构-Java Spring
15:MyBatis对象关系与映射结构-Java Spring
31 4
|
6天前
|
XML Java 数据库连接
Spring Boot与MyBatis:整合与实战
【4月更文挑战第29天】在现代的Java Web应用开发中,持久化层框架扮演了至关重要的角色。MyBatis作为一款优秀的持久化框架,被广泛应用于Java开发中。Spring Boot提供了简化开发流程的功能,而与MyBatis的整合也变得更加便捷。
29 0
|
6天前
|
Java 数据库连接 数据库
spring+mybatis_编写一个简单的增删改查接口
spring+mybatis_编写一个简单的增删改查接口
18 2