• Springboot集成MyBatis实现查询表操作(二)


    友情提醒

    先看文章目录,大致了解文章知识点结构,点击文章目录可直接跳转到文章指定位置。

    第一章、准备

    1.1)准备数据库表

    ①使用MySQL数据库,在navicat中导入sql,sql文件已经上传了,表结构如下
    在这里插入图片描述

    1.2)创建springboot项目,添加依赖

    创建springboot项目参考这个博客链接:快速构建springboot项目
    创建好后的目录结构如下
    在这里插入图片描述

    添加相关依赖

    <!--MyBatis整合SpringBoot的起步依赖-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>
    
    <!--MySQL的驱动依赖-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.28</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    1.3)使用mybatis逆向工程

    ①添加Generator.xml配置文件到如图目录下,配置文件已经上传了,这里要注意驱动jar包和项目的路径为自己本机实际的路径
    在这里插入图片描述
    ②pom文件中添加mybatis代码自动生成插件

    <!--mybatis代码自动生成插件-->
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.6</version>
                    <configuration>
                        <!--配置文件的位置-->
                        <configurationFile>GeneratorMapper.xml</configurationFile>
                        <verbose>true</verbose>
                        <overwrite>true</overwrite>
                    </configuration>
                </plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    ③双击红色选中命令,生成相关文件
    在这里插入图片描述

    ④成功生成mapper和model,并在生成的StudentMapper接口中手动加上@Mapper注解和@Repository注解
    在这里插入图片描述
    ⑤每次都手动添加@Mapper注解很麻烦,我们可以再Application类中使用@MapperScan注解进行扫描包操作达到自动识别的效果
    在这里插入图片描述

    第二章、代码开发

    2.1)建包并编写代码

    ①在com.example.springboot目录下创建web包和service包。
    在这里插入图片描述

    ②web包下创建StudentController并编写代码

    package com.example.springboot.service;
    
    import com.example.springboot.model.Student;
    import com.example.springboot.web.StudentService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class StudentController {
    
        @Autowired
        private StudentService studentService;
    
        @RequestMapping(value = "/springBoot/student")
        public @ResponseBody
        Object student() {
    
            Student student = studentService.queryStudentById(1);
    
            return student;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    ③service包下创建StudentService 接口并编写代码

    package com.example.springboot.web;
    
    import com.example.springboot.model.Student;
    
    public interface StudentService {
    
        /**
         * 根据学生标识获取学生详情
         * @param id
         * @return
         */
        Student queryStudentById(Integer id);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    ⑤service包下创建StudentServiceImpl并编写代码

    package com.example.springboot.service;
    
    import com.example.springboot.mapper.StudentMapper;
    import com.example.springboot.model.Student;
    import com.example.springboot.web.StudentService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class StudentServiceImpl implements StudentService {
    
        @Autowired
        private StudentMapper studentMapper;
    
        @Override
        public Student queryStudentById(Integer id) {
            return studentMapper.selectByPrimaryKey(id);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2.2)application配置文件

    
    #配置内嵌Tomcat端口号
    server.port=9003
    
    #配置项目上下文根
    server.servlet.context-path=/002-springboot-mybatis
    
    #配置数据库的连接信息
    #注意这里的驱动类有变化
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&characterEncoding=utf-8
    spring.datasource.username=root
    spring.datasource.password=root
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.3)设置编译位置

     <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml
                    
                
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    第三章、测试访问

    3.1)访问浏览器

    访问http://localhost:9003/002-springboot-mybatis/springBoot/student成功查询到数据

    在这里插入图片描述

  • 相关阅读:
    外包干了2个月,技术退步明显.......
    自动翻译 android/res/values/strings.xml
    你真的知道什么是正弦和余弦吗?使用 Python 和 Turtle 可视化数学
    本地CPU搭建知识库大模型来体验学习Prompt Engineering/RAG/Agent/Text2sql
    Python点击exe后报错:Failed to execute script xxxx问题的解决办法
    LINK : fatal error LNK1104: 无法打开文件“python310.lib”解决方案
    Ubuntu LTS 坚持 10 年更新不动摇
    Vue/Vuex入门、Vuex安装 和 Vuex创建导入仓库、Vuex (state )方法、state 辅助函数mapState 方法说明
    dpdk-16.11 virtio 驱动初始化卡住问题定位
    k8s运维管理
  • 原文地址:https://blog.csdn.net/baomingshu/article/details/133720399