SpringBoot整合Mybatis-程序员宅基地

技术标签: spring boot  Springboot  

一、基础工作

1、创建项目springboot项目,勾选web+JDBC+MySQL驱动

在这里插入图片描述
在这里插入图片描述
2、导入mybatis-springboot-starter依赖

  <!-- mybatis-springboot-starter-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

这里应该注意版本对应
在这里插入图片描述
3、编写application.properties配置文件

#数据库配置 根据自己数据库username和password修改
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

#整合mybatis
#给pojo实体类起别名
mybatis.type-aliases-package=com.kuang.pojo  
#接口的配置文件的位置 我这里接口配置文件是UserMapper.xml 如下图所示
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml 

在这里插入图片描述

二、springboot整合mybatis案例

上面基础工作做好,其实基础框架就已经搭建好了,直接开始进行案例的操作就可以了。

项目结构
在这里插入图片描述

1、编写pojo包下的实体类User

package com.kuang.pojo;


public class User {
    

    private int id;
    private String name;
    public String pwd;

    public int getId() {
    
        return id;
    }

    public void setId(int id) {
    
        this.id = id;
    }

    public String getName() {
    
        return name;
    }

    public void setName(String name) {
    
        this.name = name;
    }

    public String getPwd() {
    
        return pwd;
    }

    public void setPwd(String pwd) {
    
        this.pwd = pwd;
    }

    public User(int id, String name, String pwd) {
    
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }

    public User() {
    
    }

    @Override
    public String toString() {
    
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }
}

2、编写接口UserMapper

import com.kuang.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;


 /*加了这个注解 就表示了 这是一个Mybatis的mapper类
就相当于之前使用的spring整合mybatis接口 也可以使用@MapperScan("com.kuang.mapper")*/
@Mapper

/**@Component 也可以用这个 万能的*/
@Repository
public interface UserMapper {
    



    List<User> queryUserList();

    User queryUserById(int id);

    int addUser(User user);

    int updateUser(User user);

    int deleteUser(int id);


}

3、编写接口实现配置文件UserMapper.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.kuang.mapper.UserMapper">
    <select id="queryUserList" resultType="User">
        select * from mybatis.user

    </select>

    <select id="queryUserById" resultType="User">
        select *
        from mybatis.user
        where id=#{
    id};
    </select>

    <insert id="addUser" parameterType="User">
    insert into mybatis.user(id,name,pwd)
    values(#{
    id},#{
    name},#{
    pwd});

    </insert>

    <update id="updateUser" parameterType="User">
        update mybatis.user
        set name=#{
    name},pwd=#{
    pwd}
        where id=#{
    id};
    </update>

    <delete id="deleteUser" parameterType="int">
     delete
     from mybatis.user
     where id=#{
    id};

    </delete>

</mapper>

配置文件UserMapper.xml的位置在这里插入图片描述
4、编写contoller中UserController


import com.kuang.mapper.UserMapper;
import com.kuang.pojo.User;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import sun.net.util.IPAddressUtil;

import java.util.List;

@RestController
public class UserController {
    

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/queryUserList")
    public List<User> queryUserList(){
    
        List<User> userList = userMapper.queryUserList();

        for(User user:userList){
    
            System.out.println(user);
        }

        return userList;
    }

    @GetMapping("/queryUserById")
    public User queryUserById(){
    

        User user = userMapper.queryUserById(1);
        return user;



    }


    @GetMapping("/addUser")
    public String addUser(){
    
        userMapper.addUser(new User(21,"阿毛","123456"));
        return "添加成功";

    }

    @GetMapping("/updateUser")
    public String updateUser(){
    
        userMapper.updateUser(new User(1,"秃驴","123465"));
        return "更改成功";
    }


    @GetMapping("/deleteUser")
    public String deleteUser(){
    
        userMapper.deleteUser(1);
        return "删除成功";

    }


}

至此,springboot整合mybatis 并且进行增删改查操作就全部结束了。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/junR_980218/article/details/124805813

智能推荐

运维面试问道 故障经验处理过啥故障全部案例_运维工程师面试问故障怎么回答-程序员宅基地

文章浏览阅读859次,点赞18次,收藏13次。16、mysql的innodb如何定位锁问题,mysql如何减少主从复制延迟?2、在工作中,运维人员经常需要跟运营人员打交道,请问运营人员是做什么工作的?6、Squid、Varinsh和Nginx有什么区别,工作中你怎么选择?5、LVS、Nginx、HAproxy有什么区别?9、讲述一下Tomcat8005、8009、8080三个端口的含义?7、Tomcat和Resin有什么区别,工作中你怎么选择?14、讲一下Keepalived的工作原理?15、讲述一下LVS三种模式的工作过程?

Qt之解决QWidget样式表(StyleSheet)不起作用_pyqt中样式tabwidget字体转置失效-程序员宅基地

文章浏览阅读1.1k次,点赞8次,收藏11次。Qt之解决QWidget样式表(StyleSheet)不起作用_pyqt中样式tabwidget字体转置失效

【20151120】近期学习笔记-程序员宅基地

文章浏览阅读83次。 1)camel流程中的if else假设流程是CamelStart-&gt;A-&gt;B流程启动类 public class CamelStart { public static void main(String[] args) { ApplicationContext ctx = new FileSystemXmlApplicationContext("it..._camel xml if else

OpenAI又一神器!Whisper 语音转文字手把手教程-程序员宅基地

文章浏览阅读2.5k次。语音转文字在许多不同领域都有着广泛的应用。以下是一些例子:1.字幕制作:语音转文字可以帮助视频制作者快速制作字幕,这在影视行业和网络视频领域非常重要。通过使用语音转文字工具,字幕制作者可以更快地生成字幕,从而缩短制作时间,节省人工成本,并提高制作效率。2.法律文书:在法律领域,语音转文字可以帮助律师和律所将听证会、辩论和其他法律活动的录音转化为文字文档。这些文档可以用于研究、起草文件和法律分析等目..._whisper实时语音转文字

计算机毕业设计springboot房产中介管理系统9y6td9【附源码+数据库+部署+LW】_,基于springboot的房产中介系统选题意义-程序员宅基地

文章浏览阅读99次。选题背景:随着城市化进程的加快和人们生活水平的提高,房地产行业得到了迅猛发展。而在房地产交易过程中,房产中介作为连接买卖双方的桥梁,起到了至关重要的作用。然而,传统的房产中介管理方式存在着信息不对称、效率低下、服务质量参差不齐等问题,亟需一种更加高效、便捷、可靠的管理系统来提升整个行业的运营水平。选题意义:开发一款基于Spring Boot的房产中介管理系统具有重要的意义。首先,该系统可以实现信息的集中管理和共享,通过建立统一的数据库,将各类房源信息、客户需求、交易记录等数据进行整合,使得中介公司能_,基于springboot的房产中介系统选题意义

LilyGO T-Watch学习(一)环境搭建,编译下载_lilygo连接arduono-程序员宅基地

文章浏览阅读2.2k次。1、安装arduino IDE,安装后按照说明添加libIDE上选择“项目->加载库->添加.ZIP库”,选择C:\Users\用户名\Documents\Arduino\libraries\下的TTGO_TWatch_Library-master.zip;查看是否添加成功:项目->加载库,滑到最后可看见已添加的TTGO...参考:https://github.com/Xinyuan-LilyGO/TTGO_TWatch_Library2、安装esp32 ardui_lilygo连接arduono

随便推点

大屏大概是怎么个开发法(前端)_大屏开发-程序员宅基地

文章浏览阅读3.7k次,点赞16次,收藏36次。关于项目的维护与新增需求过程中值得一提的事_大屏开发

Qt开发——网络编程之UDP客户端_qt如何编写udp客户端-程序员宅基地

文章浏览阅读2k次,点赞2次,收藏3次。今天是大年初一,祝大家新年快乐!目录效果图手册选读bind绑定:readRead():例程udpclient.hudpclient.cpp效果图与上一节服务端收发配合使用手册选读bind绑定:对于UDP嵌套关键字,在绑定之后,每当UDP数据表到达指定地址或者指定端口时,readRead()信号就会被发射!readRead():每次..._qt如何编写udp客户端

Dynamic Movement Primitve - My Superficial Review_dynamics systems vs. optimal control — a unifying -程序员宅基地

文章浏览阅读1k次,点赞3次,收藏5次。Let’s talk about the Dynamic Movement Primitive (DMP) for robots learning from demonstration. In this article, we make an assumption that you readers all have the background of control theory and robo..._dynamics systems vs. optimal control — a unifying view

angular追加html,Angular HTML绑定-程序员宅基地

文章浏览阅读352次。Angular 2.0.0和Angular 4.0.0 final仅为了安全的内容DOMSanitizer潜在的不安全HTML需要使用Angulars DOM清理程序明确标记为受信任,因此不会删除内容中可能不安全的部分用管子@Pipe({name:'safeHtml'})exportclassSafe{constructor(privatesanitizer:DomSanitizer){..._angular safehtml

轻松学习C语言编程的秘诀:总结+灵感-程序员宅基地

文章浏览阅读120次。2019独角兽企业重金招聘Python工程师标准>>> ..._c语言秘籍

dataframe类型数据的遍历_pandas修改数据时要注意的问题-程序员宅基地

文章浏览阅读1.2k次。先导入数据:import numpy as npimport pandas as pdimportdatetimedf=pd.read_excel('/Users/dxn/Desktop/发票2020.xlsx',header=0,dtype={'发票号码':'str'})数据结构情况:>>> df 日期 供货单位 发票号码 价税合计0 ..._dataframe遍历时能赋值吗