当前位置: 首页 > news >正文

springboot整合mybatisplus (详细)

springboot整合mybatisplus (详细)

文章目录

    • springboot整合mybatisplus (详细)
    • 1.项目目录
    • 2.pom.xml
    • 3.StudentInfo
    • 4.StudentController
    • 5.StudentService
    • 6.StudentServiceImpl
    • 7.StudentMapper
    • 8.StudentMapper.xml
    • 9.application.yml
    • 10 表sql
    • 11.执行成功例子
    • 12 完整代码地址

1.项目目录

在这里插入图片描述

2.pom.xml

        <!--Mybatis-plus的依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1</version>
        </dependency>
        <!--mysql的依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

3.StudentInfo

package com.example.schedulelockscreen.model;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("student")
public class StudentInfo {
    //若id为主键,则为@TableId("id")
    @TableField("id")
    private String id;

    @TableField("sname")
    private String sname;

    @TableField("classId")
    private String classId;

    @TableField("birthday")
    private String birthday;

    @TableField("email")
    private String email;
}

4.StudentController

package com.example.schedulelockscreen.controller;


import com.example.schedulelockscreen.IService.StudentService;
import com.example.schedulelockscreen.model.StudentInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
//@RequestMapping("/demo11")
public class StudentController {

    @Autowired(required = false)
    private StudentService studentService;

    /**
     * 查询学生信息
     * @param id
     * @return
     */
    @RequestMapping("getInfo")
    public StudentInfo getStudentInfo(){

        StudentInfo byId = null;
        try {
            byId = studentService.getById(2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return byId;
    }

    /**
     * 插入学生信息
     * @param studentInfo
     */
    @RequestMapping("/insert")
    public void insertInfo(StudentInfo studentInfo){

        StudentInfo info=new StudentInfo();
        info.setId(studentInfo.getId());
        info.setSname(studentInfo.getSname());
        info.setClassId(studentInfo.getClassId());
        info.setBirthday(studentInfo.getBirthday());
        info.setEmail(studentInfo.getEmail());
        studentService.save(info);
    }

    /**
     * 查询全部学生信息
     * @return
     */
    @RequestMapping("/selectAll")
    public List<StudentInfo> selectAll(){
        return studentService.list();
    }

    /**
     * 根据id更新学生表信息
     * @param studentInfo
     */
    @RequestMapping("/update")
    public void updateById(StudentInfo studentInfo){

        StudentInfo info=new StudentInfo();
        info.setId(studentInfo.getId());
        info.setSname(studentInfo.getSname());
        info.setClassId(studentInfo.getClassId());
        info.setBirthday(studentInfo.getBirthday());
        info.setEmail(studentInfo.getEmail());
        studentService.updateById(info);
    }

    /**
     * 根据id删除学生信息
     * @param id
     */
    @RequestMapping("/delete")
    public void deleteById(String id){
        studentService.removeById(id);
    }

}


5.StudentService

package com.example.schedulelockscreen.IService;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.schedulelockscreen.model.StudentInfo;

public interface StudentService extends IService<StudentInfo> {
}

6.StudentServiceImpl

package com.example.schedulelockscreen.IService.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.schedulelockscreen.IService.StudentService;
import com.example.schedulelockscreen.Mapper.StudentMapper;
import com.example.schedulelockscreen.model.StudentInfo;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentInfo> implements StudentService {
}

7.StudentMapper

package com.example.schedulelockscreen.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import com.example.schedulelockscreen.model.StudentInfo;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface StudentMapper extends BaseMapper<StudentInfo> {
}

8.StudentMapper.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.example.schedulelockscreen.Mapper.StudentMapper">

</mapper>

9.application.yml

#端口号8080
server:
  port: 8083

#数据库名:mysql,用户名root,密码123456
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.jdbc.Driver

# mybatis-plus配置
mybatis-plus:
  # xml文件位置
  mapper-locations: classpath:mapper/*.xml


10 表sql

/*
 Navicat Premium Dump SQL

 Source Server         : 本地--mysql
 Source Server Type    : MySQL
 Source Server Version : 50744 (5.7.44-log)
 Source Host           : localhost:3306
 Source Schema         : test

 Target Server Type    : MySQL
 Target Server Version : 50744 (5.7.44-log)
 File Encoding         : 65001

 Date: 01/03/2025 08:59:19
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `id` varchar(2) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '学生ID',
  `sname` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '学生姓名',
  `classId` varchar(3) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '班级ID',
  `birthday` varchar(5) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '学生生日',
  `email` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '学生电子邮箱'
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '32423', '1', '4', '234');
INSERT INTO `student` VALUES ('2', '34', '3', '3', '3');

SET FOREIGN_KEY_CHECKS = 1;

11.执行成功例子

在这里插入图片描述

12 完整代码地址

https://download.csdn.net/download/weixin_48616345/90441555

相关文章:

  • k8s面试题总结(六)
  • 意识的本质是什么?
  • Vue.js 学习笔记
  • 010 rocketmq批量消息
  • 【STM32F103ZET6——库函数】6.PWM
  • Hue UI展示中文
  • 关于深度学习的一份介绍
  • CentOS vs Ubuntu - 常用命令深度对比及最佳实践指南20250302
  • 谈谈 ES 6.8 到 7.10 的功能变迁(6)- 其他
  • P3398 仓鼠找 sugar【题解】
  • 【Linux】Linux权限
  • MLP生成一些训练和预测数据
  • 时序逻辑电路——有限状态机FSM
  • 一文速通C++非类型模板参数
  • 《几何原本》命题I.2
  • 神经网络代码入门解析
  • Docker项目部署-部署Java应用
  • 二分查找-I(C++)
  • 【通俗讲解电子电路】——从零开始理解生活中的电路(二)
  • Tomcat部署
  • 做网站详细教程/百度竞价查询
  • 网络公司 网站设计/制造业中小微企业
  • 动易 如何在一个服务器测试两个网站/推广关键词外包
  • 哈尔滨制作网站企业/小说关键词生成器
  • 电子政务网站建设法律法规/搜索引擎优化服务
  • 宣讲家网站两学一做心得体会/seo课程简介