springboot整合mybatis

一、项目结构展示

二、开始整合

1、引入pom依赖

进入Maven中央仓库选择自己所需要的依赖,maven仓库地址:Maven Central

完整Maven依赖如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.mgx</groupId><artifactId>springboot-mgx</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-mgx</name><description>springboot项目整合</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.4.RELEASE</version></parent><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding></properties><dependencies><!-- springboot web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!--mysql相关--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.27</version></dependency><!--mybatis相关--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.1.4.RELEASE</version></plugin></plugins></build></project>

2、添加application 或 bootstrap 配置

server:# 端口号port: 8080spring:# 配置数据源datasource:username: rootpassword: 123456url: jdbc:mysql://localhost:3306/mgx_test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTCdriver-class-name: com.mysql.cj.jdbc.Driver# 配置mybatis
mybatis:#指定位置扫描Mapper接口对应的XML文件 classpath:xml文件位置mapper-locations: classpath:mapper/*.xml#指定扫描包位置让mybatis自动扫描到指定义的entity包下type-aliases-package: com.mgx.entity

3、创建表

CREATE TABLE `t_info_user` (`id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '自增主键',`name` varchar(64) NOT NULL COMMENT '姓名',`phone` varchar(11) NOT NULL COMMENT '手机号',`age` int(3) NOT NULL COMMENT '年龄',`sex` int(1) NOT NULL COMMENT '性别  0-女 1-男 2-其他',`address` varchar(255) NOT NULL COMMENT '家庭地址',`income` decimal(19,2) DEFAULT NULL COMMENT '年收入',PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户基础信息表';

4、项目结构

5、类内容

UserController

package com.mgx.controller;import com.mgx.entity.InfoUser;
import com.mgx.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;import javax.annotation.Resource;/*** @author mgx* @date 2023/9/15 3:25 PM*/
@RestController
@RequestMapping("/user")
public class UserController {@Resourceprivate UserService userService;@PostMapping("/addUserInfo")public String addUserInfo(@RequestBody InfoUser infoUser) {boolean flag = userService.addUserInfo(infoUser);if (flag){return "添加成功!";} else {return "添加失败!";}}@GetMapping("/getUserInfo")public InfoUser getUserInfo(@Param("id") Long id) {return userService.getUserInfo(id);}@DeleteMapping("/deleteUserInfo")public String deleteUserInfo(@Param(value = "id") Long id){boolean flag = userService.deleteUserInfo(id);if (flag){return "删除成功!";} else {return "删除失败!";}}@PutMapping("/updateUserInfo")public String updateUserInfo(@RequestBody InfoUser infoUser){boolean flag = userService.updateUserInfo(infoUser);if (flag){return "更新成功!";} else {return "更新失败!";}}}

UserService

package com.mgx.service;import com.mgx.entity.InfoUser;/*** @author mgx* @date 2023/9/15 3:38 PM*/
public interface UserService {boolean addUserInfo(InfoUser infoUser);InfoUser getUserInfo(Long id);boolean deleteUserInfo(Long id);boolean updateUserInfo(InfoUser infoUser);
}

UserServiceImpl

package com.mgx.service.impl;import com.mgx.entity.InfoUser;
import com.mgx.mapper.InfoUserMapper;
import com.mgx.service.UserService;
import org.springframework.stereotype.Service;import javax.annotation.Resource;/*** @author mgx* @date 2023/9/15 3:38 PM*/
@Service
public class UserServiceImpl implements UserService {@Resourceprivate InfoUserMapper infoUserMapper;@Overridepublic boolean addUserInfo(InfoUser infoUser) {int addRow = infoUserMapper.insert(infoUser);return addRow == 1;}@Overridepublic InfoUser getUserInfo(Long id) {return infoUserMapper.selectByPrimaryKey(id);}@Overridepublic boolean deleteUserInfo(Long id) {int deleteRow = infoUserMapper.deleteByPrimaryKey(id);return deleteRow == 1;}@Overridepublic boolean updateUserInfo(InfoUser infoUser) {int updateRow = infoUserMapper.updateByPrimaryKeySelective(infoUser);return updateRow == 1;}}

InfoUser

package com.mgx.entity;import java.math.BigDecimal;/*** 用户基础信息表* @author mgx*/
public class InfoUser {/*** 自增主键*/private Long id;/*** 姓名*/private String name;/*** 手机号*/private String phone;/*** 年龄*/private Integer age;/*** 性别  0-女 1-男 2-其他*/private Integer sex;/*** 家庭地址*/private String address;/*** 年收入*/private BigDecimal income;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public Integer getSex() {return sex;}public void setSex(Integer sex) {this.sex = sex;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public BigDecimal getIncome() {return income;}public void setIncome(BigDecimal income) {this.income = income;}
}

InfoUserMapper

package com.mgx.mapper;import com.mgx.entity.InfoUser;/*** @author  mgx* @date  2023/9/15 3:29 PM*/
public interface InfoUserMapper {int deleteByPrimaryKey(Long id);int insert(InfoUser record);int insertSelective(InfoUser record);InfoUser selectByPrimaryKey(Long id);int updateByPrimaryKeySelective(InfoUser record);int updateByPrimaryKey(InfoUser record);}

InfoUserMapper.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.mgx.mapper.InfoUserMapper"><resultMap id="BaseResultMap" type="com.mgx.entity.InfoUser"><!--@mbg.generated--><!--@Table t_info_user--><id column="id" jdbcType="BIGINT" property="id" /><result column="name" jdbcType="VARCHAR" property="name" /><result column="phone" jdbcType="VARCHAR" property="phone" /><result column="age" jdbcType="INTEGER" property="age" /><result column="sex" jdbcType="INTEGER" property="sex" /><result column="address" jdbcType="VARCHAR" property="address" /><result column="income" jdbcType="DECIMAL" property="income" /></resultMap><sql id="Base_Column_List"><!--@mbg.generated-->id, `name`, phone, age, sex, address, income</sql><select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap"><!--@mbg.generated-->select <include refid="Base_Column_List" />from t_info_userwhere id = #{id,jdbcType=BIGINT}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Long"><!--@mbg.generated-->delete from t_info_userwhere id = #{id,jdbcType=BIGINT}</delete><insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.mgx.entity.InfoUser" useGeneratedKeys="true"><!--@mbg.generated-->insert into t_info_user (`name`, phone, age, sex, address, income)values (#{name,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{sex,jdbcType=INTEGER}, #{address,jdbcType=VARCHAR}, #{income,jdbcType=DECIMAL})</insert><insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.mgx.entity.InfoUser" useGeneratedKeys="true"><!--@mbg.generated-->insert into t_info_user<trim prefix="(" suffix=")" suffixOverrides=","><if test="name != null">`name`,</if><if test="phone != null">phone,</if><if test="age != null">age,</if><if test="sex != null">sex,</if><if test="address != null">address,</if><if test="income != null">income,</if></trim><trim prefix="values (" suffix=")" suffixOverrides=","><if test="name != null">#{name,jdbcType=VARCHAR},</if><if test="phone != null">#{phone,jdbcType=VARCHAR},</if><if test="age != null">#{age,jdbcType=INTEGER},</if><if test="sex != null">#{sex,jdbcType=INTEGER},</if><if test="address != null">#{address,jdbcType=VARCHAR},</if><if test="income != null">#{income,jdbcType=DECIMAL},</if></trim></insert><update id="updateByPrimaryKeySelective" parameterType="com.mgx.entity.InfoUser"><!--@mbg.generated-->update t_info_user<set><if test="name != null">`name` = #{name,jdbcType=VARCHAR},</if><if test="phone != null">phone = #{phone,jdbcType=VARCHAR},</if><if test="age != null">age = #{age,jdbcType=INTEGER},</if><if test="sex != null">sex = #{sex,jdbcType=INTEGER},</if><if test="address != null">address = #{address,jdbcType=VARCHAR},</if><if test="income != null">income = #{income,jdbcType=DECIMAL},</if></set>where id = #{id,jdbcType=BIGINT}</update><update id="updateByPrimaryKey" parameterType="com.mgx.entity.InfoUser"><!--@mbg.generated-->update t_info_userset `name` = #{name,jdbcType=VARCHAR},phone = #{phone,jdbcType=VARCHAR},age = #{age,jdbcType=INTEGER},sex = #{sex,jdbcType=INTEGER},address = #{address,jdbcType=VARCHAR},income = #{income,jdbcType=DECIMAL}where id = #{id,jdbcType=BIGINT}</update>
</mapper>

SpringbootMgxApplication

package com.mgx;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author mgx*/
@MapperScan("com.mgx.mapper")
@SpringBootApplication
public class SpringbootMgxApplication {public static void main(String[] args) {System.out.println("开始启动");SpringApplication.run(SpringbootMgxApplication.class, args);System.out.println("启动成功");}}

三、验证springboot整合mybatis是否成功

1、启动项目,端口号8080

2、使用postman调用接口

增:

改:

查:

删: 

 

 到此springboot整合mybatis成功。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/79269.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

markdown工具Atom预览与插件安装

​atom是以命令行作为插件选项的入口 打开命令输入框 Windows: ctrl shift p Mac: command shift p 输入命令安装 输入 markdown preview toggle &#xff0c;可以偷懒只输入mdpt(模糊匹配) 按enter键即可看到预览&#xff0c;如图&#xff0c;左边编辑&#xff0c;右…

Vue中的过滤器 Filters

过滤器 Filters 过滤器一般用于格式化文本内容&#xff0c;通常可以在两个地方使用&#xff0c;主要是模板语法、以及 v-bind 表达式中。例如我想对展示的文本进行一些特殊处理&#xff0c;将金额进行四舍五入后再展示。选项 filters 内可以编写多个自定义过滤器。 用法&…

LightDB 23.3 通过GUC参数控制commit fetch

背景 commit游标提交之后&#xff0c;可以继续使用fetch进行结果集的操作。commit和fetch结合使用功能开发时不考虑分布式。后续&#xff0c;又对分布式进行了测试&#xff0c;发现持有portal后&#xff0c;代码中会对querydesc进行非空判断。当querydesc为空时&#xff0c;Li…

工业交换机常见的硬件故障有哪些?

工业交换机常见的硬件故障主要是由于受到供电电源、室内温度、室内湿度、电磁干扰、静电等机房环境的影响&#xff0c;造成工业交换机电源、背板、模块、端口等部件出现故障。具体可以分为以下几类。 1.电力供应故障&#xff1a; 由于外部供电不稳定、电源线路老化或雷击等原因…

LiveNVR监控流媒体Onvif/RTSP功能-支持海康摄像头海康NVR通过EHOME协议ISUP协议接入分发视频流或是转GB28181

LiveNVR支持海康NVR摄像头通EHOME接入ISUP接入LiveNVR分发视频流或是转GB28181 1、海康 ISUP 接入配置2、海康设备接入2.1、海康EHOME接入配置示例2.2、海康ISUP接入配置示例 3、通道配置3.1、直播流接入类型 海康ISUP3.2、海康 ISUP 设备ID3.3、启用保存3.4、接入成功 4、相关…

亚马逊封买家账号的原因有哪些

亚马逊可能封锁买家账号的原因有多种&#xff0c;主要是出于保护市场和维护平台秩序的考虑。以下是一些可能导致亚马逊封锁买家账号的常见原因&#xff1a; 1、涉及违规行为&#xff1a;如果买家违反了亚马逊的使用政策&#xff0c;如发表虚假评价、滥用退货政策、欺诈或盗窃等…

归并排序三种常见写法

算法思路 归并排序是一种分治算法&#xff1a;首先将数组分成两半&#xff0c;然后对每一半进行归并排序&#xff0c;最后将两个有序的子数组合并&#xff0c;以得到最终的排序数组。为了简洁下面代码中会调用 STL 的 i n p l a c e _ m e r g e inplace\_merge inplace_merg…

【视觉SLAM入门】7.3.后端优化 基于KF/EKF和基于BA图优化的后端,推导及举例分析

"时间倾诉我的故事" 1. 理论推导2. 主流解法3. 用EKF估计状态3.1. 基于EKF代表解法的感悟 4. 用BA法估计状态4.1 构建最小二乘问题4.2 求解BA推导4.3 H的稀疏结构4.4 根据H稀疏性求解4.5 鲁棒核函数4.6 编程注意 5.总结 引入&#xff1a; 前端里程计能给出一个短时间…

markdown学习笔记

markdown学习笔记 1.文字&#xff08;依靠HTML&#xff09; 1.1文字缩进-空格转义符 单字符空&#xff1a;&emsp; 半字符空&#xff1a;&ensp;1.2文字对齐 「居中&#xff1a;」<center> 居中 </center> or <p align"center"> 居中 …

吃瓜教程第一二章学习记录

当大多数人听到 "机器学习 "时&#xff0c;他们会联想到机器人&#xff1a;一个可靠的管家或一个致命的终结者&#xff0c;这取决于你问谁。但是&#xff0c;机器学习并不只是未来主义的幻想&#xff0c;它已经存在了。事实上&#xff0c;在一些特殊的应用中&#xf…

Mysql---第四篇

系列文章目录 文章目录 系列文章目录一、关心过业务系统里面的sql耗时吗?统计过慢查询吗?对慢查询都怎么优化过?二、事务的基本特性和隔离级别一、关心过业务系统里面的sql耗时吗?统计过慢查询吗?对慢查询都怎么优化过? 在业务系统中,除了使用主键进行的查询,其他的都…

upload-labs文件上传漏洞通关

一、环境搭建 upload-labs是一个使用php语言编写的&#xff0c;专门收集渗透测试和CTF中遇到的各种上传漏洞的靶场。 下载地址&#xff1a;https://github.com/c0ny1/upload-labs/releases 在 win 环境下 直接解压到phpstudy下即可 二、通关 &#xff08;一&#xff09;16关…

为什么百度百科有词条,而维基百科无法创建?

很多企业有出口业务&#xff0c;想在互联网上开展全球性网络营销&#xff0c;维基百科往往被认为是开展海外营销的第一站。其作用相当于开展国内网络营销的百度百科&#xff0c;经常有些企业给小马识途营销顾问提供的词条内容就是百度百科的内容&#xff0c;可事实上两个平台的…

使用凌鲨进行聚合搜索

作为研发人员&#xff0c;我们经常需要在多个来源之间查找信息&#xff0c;以便进行研发工作。除了常用的搜索引擎如百度和必应之外&#xff0c;我们还需要查阅各种代码文档和依赖包等资源。这些资源通常分散在各个网站和文档库中&#xff0c;需要花费一定的时间和精力才能找到…

什么是GPT与MBR

GPT&#xff08;GUID Partition Table&#xff09;和MBR&#xff08;Master Boot Record&#xff09;是两种不同的磁盘分区表格式。 MBR是一种较早的磁盘分区表格式&#xff0c;它使用512字节的扇区作为存储空间。MBR分区表可以定义最多4个主分区&#xff0c;每个主分区都可以…

Redis缓存更新策略、详解并发条件下数据库与缓存的一致性问题以及消息队列解决方案

0、前言 我们知道&#xff0c;缓存由于在内存中&#xff0c;数据处理速度比直接操作数据库要快很多&#xff0c;因此常常将数据先读到缓存中&#xff0c;再进行查询、更新等操作。 但与之而来的问题就是&#xff0c;内存中的数据不仅没有持久化&#xff0c;而且需要保证…

如何使用防关联指纹浏览器批量管理多个TikTok账号?

海外TikTok是目前全球最受欢迎的社交媒体应用之一。随着其用户数量的持续增长&#xff0c;海外TikTok用户们已经意识到拥有多个账号的重要性。 首先&#xff0c;海外TikTok需要多账号是为了更好地保护个人隐私。在社交媒体时代&#xff0c;个人信息保护变得尤为重要。通过拥有多…

如何在微软Edge浏览器上一键观看高清视频?

编者按&#xff1a;视频是当下最流行的媒体形式之一。但由于视频压缩、网络不稳定等原因&#xff0c;我们常常可以看到互联网上的很多视频其画面质量并不理想&#xff0c;尤其是在浏览器端&#xff0c;这极大地影响了观看体验。不过&#xff0c;近期微软 Edge 浏览器推出了一项…

linux命令查看谁在使用服务器的GPU

命令&#xff1a;查看GPU使用情况 nvidia-smi 可以知悉GPU占用情况和主要使用GPU的进程&#xff0c;如下图所示&#xff1a; 实时查看gpu使用&#xff1a; nvidia-smi -l 1 表示每隔1s刷新一下&#xff0c;数字可更改。 查看进程的归属者 方法一&#xff1a;ps -f -p pid…

发布文章到wordpress

给朋友新建的wp网站,没有内容怎么办,总不能一篇篇的挨个写入吧。用wp提供的录入模块就可以了 参考 wp说明文档 获取docx内容保存到wp 资料有个docx文件,但文件格式混乱,好在有目录,可以基于目录,对文章分割,用正则拆分存入wp 首先用pandoc把docx转为md文件,速度较慢,…