Springboot+Mybatis入门案例

一、项目结构

1.导入依赖

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>testcsdn</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.10.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--web网页,如果只在test测试类中测试,则可去掉-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--简化代码的工具包,针对实体类-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--mybatis‐plus的springboot支持-->
<!--        <dependency>-->
<!--            <groupId>com.baomidou</groupId>-->
<!--            <artifactId>mybatis-plus-boot-starter</artifactId>-->
<!--            <version>3.1.0</version>-->
<!--        </dependency>-->
        <!--mybatis的springboot支持-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
        </dependency>

    </dependencies>

</project>

 2.Controller层

package org.example.controller;

import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController //@RestController = @Controller + @ResponseBody 此处目的是返回json数据
@RequestMapping("/test")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/show")
    public String test01(){
        return studentService.findStudent();
    }

}

 3.mapper层

package org.example.mapper;

import org.example.pojo.Student;

public interface StudentMapper {
    Student findStudent();
}

4.pojo实体类

springboot+mybatis实体类不需要加@Table等注解,只要有@Data注解即可(包含getter/setter方法)

package org.example.pojo;

import lombok.Data;

@Data
public class Student {
    private Integer id;
    private String name;
    private Integer age;

}

 5.service层

接口

package org.example.service;

public interface StudentService {
    String findStudent();
}

实现类

package org.example.service;

import org.example.mapper.StudentMapper;
import org.example.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImp implements StudentService{

    @Autowired
    private StudentMapper studentMapper;
    @Override
    public String findStudent() {
        Student student = studentMapper.findStudent();
        return student.toString();
    }
}

6.启动类

package org.example;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("org.example.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 7.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="org.example.mapper.StudentMapper">
    <select id="findStudent" resultType="org.example.pojo.Student">
        select * from student where  id = 1
    </select>
</mapper>

8.application.yml

server:
  port: 8080 #服务端口号,可修改
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver #mysql5版本把.cj去掉,此处为mysql8.0
    url: jdbc:mysql://localhost:3306/test #test改成自己的数据库名
    username: root
    password: "123456"

mybatis:
  mapper-locations: classpath:mybatis/*.xml
  #目的是为了省略resultType里的代码量,可不加
#  type-aliases-package: com.example.pojo
#  configuration:
#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

9.测试类

package org.example.test;

import org.example.service.StudentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class StudentTest {
    @Autowired
    private StudentService studentService;

    @Test
    public void test(){
        String student = studentService.findStudent();
        System.out.println(student);
    }

}

二、Sql

/*
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 80033 (8.0.33)
 Source Host           : localhost:3306
 Source Schema         : test

 Target Server Type    : MySQL
 Target Server Version : 80033 (8.0.33)
 File Encoding         : 65001

 Date: 19/12/2023 10:14:04
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `age` int NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, '李华', 19);
INSERT INTO `student` VALUES (2, '张飞', 20);
INSERT INTO `student` VALUES (3, '丑陋', 22);
INSERT INTO `student` VALUES (4, '孙尚香', 18);
INSERT INTO `student` VALUES (5, '狄仁杰', 12);

SET FOREIGN_KEY_CHECKS = 1;

测试

右键启动类启动服务

在网页打开网址

http://localhost:8080/test/show

显示下图,即运行成功!

到这里你的springboot+mybatis项目就算是入门了,如果伙伴们有什么问题的话,评论留言,大家一起学习,一起进步!

环境:IDEA开发工具、数据库Mysql8.0、Springboot+Mybatis项目 

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Meta与Ray-Ban合作推出了一款全新智能眼镜外观时尚,而且搭载了能够“看到“你所看到的一切的人工智能技术

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

骨传导耳机和开放式耳机有什么区别?一文读懂骨传导耳机和开放式的关系!

先说结论&#xff0c;骨传导耳机和气传导耳机两者都属于是开放式耳机&#xff0c;开放式耳机指的是开放双耳佩戴的耳机&#xff01; 开放式耳机分为两种&#xff0c;分别是骨传导耳机和气传导耳机&#xff0c;虽然两者都属于开放式耳机&#xff0c;但它们的佩戴方式和传声原理…

SpringBoot接入轻量级分布式日志框架GrayLog

1.前言 日志在我们日常开发定位错误&#xff0c;链路错误排查时必不可少&#xff0c;如果我们只有一个服务&#xff0c;我们可以只简单的通过打印的日志文件进行排查定位就可以&#xff0c;但是在分布式服务环境下&#xff0c;多个环境的日志统一收集、展示则成为一个问题。目…

Relocations for this machine are not implemented,IDA版本过低导致生成汇编代码失败

目录 1、问题描述 2、安卓app发生崩溃&#xff0c;需要查看汇编代码上下文去辅助分析 3、使用IDA打开.so动态库文件&#xff0c;提示Relocations for this machine are not implemented 4、IDA版本较老&#xff0c;不支持ARM64的指令集&#xff0c;使用7.0版本就可以了 5、…

ACM32如何保护算法、协议不被破解或者修改

ACM32具有以下几种功能&#xff0c;可以保护算法、协议不被破解或者修改。 1.存储保护  RDP读保护  WRP写保护  PCROP 专有代码读保护  MPU存储区域权限控制  Secure User Memory存储区域加密 2.密码学算法引擎  AES  HASH  随机数生成  …

Hugging Face实战-系列教程19:文本摘要建模实战1 之 数据清洗(中文商城评价数据处理方法)

&#x1f6a9;&#x1f6a9;&#x1f6a9;Hugging Face 实战系列 总目录 有任何问题欢迎在下面留言 本篇文章的代码运行界面均在Jupyter Notebook中进行 本篇文章配套的代码资源已经上传 文本摘要建模实战1 之 数据清洗 文本摘要建模实战2 之 Tokenizer处理 1 任务概述 1.1 任…

MATLAB求解微积分(代码+详细解读)

大多数实际工程问题常常简化为微分方程&#xff0c;其求解显地至关重要。 符号微积分 极限 % matlab提供的求极限函数limit(),其调用格式为 % y limit(fun,x,x0) % fun为要求解的函数&#xff0c;x为函数自变量&#xff0c;x0为函数自变量的取值&#xff0c;x趋近于x0 clc;…

用户权益保护:TikTok如何守护数字隐私

随着社交媒体的普及&#xff0c;数字隐私问题逐渐成为用户关注的焦点。在这一背景下&#xff0c;TikTok作为一款备受欢迎的短视频应用&#xff0c;怎样保护用户的数字隐私&#xff0c;成为一个备受关注的话题。本文将深入探讨TikTok在用户权益保护方面的举措&#xff0c;以及它…

13代现场实拍图

1. 2.1寸电子墨水屏显示&#xff1b; 2. 无线通信868M&#xff0c;跳频通信&#xff1b; 3. 自带1个按键及三色高亮LED指示灯指示&#xff1b; 4. 超低功耗&#xff1b; 5. 标签ID码正面显示&#xff1b; 6. 通信速率200K/50K&#xff1b; 7. 覆盖通信半径30米以上&#…

C语言求最大公约数(详解版)

1、问题描述 求任意两个正整数的最大公约数&#xff08;GCD&#xff09;。 2、问题分析 如果有一个自然数a能被自然数b整除&#xff0c;则称a为b的倍数&#xff0c;b为a的约数。几个自然数公有的约数&#xff0c;叫做这几个自然数的公约数。公约数中最大的一个公约数&#x…

DTO/DO/VO分层与拷贝

作者简介&#xff1a;大家好&#xff0c;我是smart哥&#xff0c;前中兴通讯、美团架构师&#xff0c;现某互联网公司CTO 联系qq&#xff1a;184480602&#xff0c;加我进群&#xff0c;大家一起学习&#xff0c;一起进步&#xff0c;一起对抗互联网寒冬 这一篇其实没太多实质内…

我的创作纪念日【512】

机缘 学知识 收获 沉下心来安静学习的能力 日常 创作学习 成就 只要在学习&#xff0c;没有混时间就有成就感 憧憬 早日成为一个健康、漂亮、自律的富婆。

Android Studio(3.6.2版本)安装 java2smali 插件,java2smali 插件的使用方法简述

一、Android Studio&#xff08;3.6.2版本&#xff09;安装 java2smali 插件 1、左上角File—>Setting&#xff0c;如下图 2、Setting界面中&#xff1a;点击Plugins—>选择右侧上方Marketplace—>搜索栏输入java2smali&#xff0c;如下图 3、点击Install按钮—>点…

c语言:指针作为参数传递

探究实参与形参它们相互独立 由于主调函数的变量a&#xff0c;b与被调函数的形参x&#xff0c;y它们相互独立。函数 swap 可以修改变量x&#xff0c;y&#xff0c;但是却无法影响到主调函数中的a&#xff0c;b。 现在利用取地址运算符&#xff0c;分别打印它们的首地址&#x…

枚举enum(学习推荐版,通俗易懂)

定义及特点 第一行的列举名称&#xff08;都是常量&#xff09;&#xff0c;代表每个枚举的对象&#xff08;因为枚举不能创建对象&#xff0c;只能依靠罗列名称确定可使用枚举对象个数&#xff09;&#xff0c;这些名称代表的对象可以使用所在枚举类的所有成员变量、成员方法、…

网络编程day3作业

多进程实现TCP并发服务器 #include<myhead.h>#define PORT 8888 #define IP "192.168.125.130"void hadder(int signo) {if(signo SIGCHLD){while(waitpid(-1,NULL,WNOHANG) > 0);} }int information_exchange(int newfd,struct sockaddr_in cin) {char b…

查验身份证c语言

以下是一个简单的C语言程序&#xff0c;用于验证身份证号码的校验码&#xff1a; #include <stdio.h>#include <string.h>int main() { char id[19]; int i, weight[17] {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; int sum 0; char c…

企业微信自动登录自定义系统

方法一&#xff1a;企业微信构造OAuth2链接跳转登录到自定义系统 企业微信自定义应用配置 构造网页授权链接 如果企业需要在打开的网页里面携带用户的身份信息&#xff0c;第一步需要构造如下的链接来获取code参数&#xff1a; https://open.weixin.qq.com/connect/oauth2/…

Elasticsearch 向量相似搜索

Elasticsearch 向量相似搜索的原理涉及使用密集向量(dense vector)来表示文档,并通过余弦相似性度量来计算文档之间的相似性。以下是 Elasticsearch 向量相似搜索的基本原理: 向量表示文档: 文档的文本内容经过嵌入模型(如BERT、Word2Vec等)处理,得到一个密集向量(den…

在openSUSE-Leap-15.5-DVD-x86_64中使用deepin-wine-6.0.0.62再使用微信3.9.5

在openSUSE-Leap-15.5-DVD-x86_64中使用deepin-wine-6.0.0.62再使用微信3.9.5 参考文章&#xff1a; 《记录-下fedora 33安装deepin qq和微信 &#xff0c;不需要安装deepinwine》 https://tieba.baidu.com/p/7279470269 《opensuse使用virtualbox安装win10》 https://blog.c…
最新文章