• SpringBoot 集成Mybatis


    文章目录

    一、创建SpringBoot项目

    如何创建详见:IDEA 创建 SpringBoot 项目

    二、添加Mybatis相关依赖

    以前开发Web项目我们都知道要想把数据添加到数据库,不仅必须要数据库的驱动程序,还要有各种各样的配置文件,像java Bean配置,数据源配置,对象和数据库字段的映射配置等等。使用SpringBoot开发,我们只需要加入依赖文件就可以了,SpringBoot已经都帮我配置好了。配置如下图所示:

    
        mysql
        mysql-connector-java
        runtime
    
    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.2.1
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    三、数据源配置

    在application.properties中配置数据库连接的相关信息:

    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://127.0.0.1:18103/db_test?characterEncoding=GBK
    spring.datasource.username=root
    spring.datasource.password=root
    
    • 1
    • 2
    • 3
    • 4

    四、创建事务的模型实体类

    编程是利用面向对象的思想把自然界中的事物抽象成模型,利用模型来解决实际中的问题。如下图:

    package com.springboottest.bean;
    
    public class StudentBean {
    
        private int id;
        private String name;
    
        public StudentBean() {
        }
    
        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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    注:这里的字段名称与数据库表字段名称一致。

    五、创建和数据库交互联系的映射关系类

    这个类主要是和数据进行交互联系的,需要配置好实体类和数据库字段的映射关系。由于SpringBoot已经做了大量的工作,我们只需要做好相关注解就可以使用了。如下图所示:

    package com.springboottest.sql.mapper;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Select;
    
    @Mapper
    public interface StudentMapper {
    
        @Select("select * from tb_student where name=#{name}")
        StudentBean getStudentInfoByName(String name);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    @Mapper 表明该类是一个Mapper接口,使用@Select@Insert等注解我们可以直接在类中书写sql语句来实现我们的目的。

    六、创建业务接口和实现类

    我们在接口类里定义要实现的业务功能接口,在它的实现类里实现接口。接口类如下图:

    package com.springboottest.sql.service;
    
    import com.springboottest.bean.StudentBean;
    
    public interface StudentService {
    
        StudentBean getStudentInfoByName(String name);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    实现类如下图:

    package com.springboottest.sql.service;
    
    import com.springboottest.bean.StudentBean;
    import com.springboottest.sql.mapper.StudentMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    @Service
    public class StudentServiceImpl implements StudentService{
    
        @Autowired
        private StudentMapper studentMapper;
    
        @Override
        @Transactional
        public StudentBean getStudentInfoByName(String name) {
            return studentMapper.getStudentInfoByName(name);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    @Service注解表明它是一个服务类Bean,可以被SpringBoot识别使用,相当于以前在xml里配置的bean。

    七、创建控制器类

    Web项目的请求经过映射找到控制器类里对应的方法,然后再实现完业务返回响应信息。如下图:

    package com.springboottest.controller;
    
    import com.springboottest.bean.StudentBean;
    import com.springboottest.sql.MySQLProcessor;
    import com.springboottest.sql.service.StudentService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/mysql")
    public class SqlController {
    
        @Autowired
        private StudentService studentService;
    
        @RequestMapping(value = "/student")
        public String studentSelect(@RequestParam String name){
            StudentBean bean = studentService.getStudentInfoByName(name);
            if(bean != null){
                return "Name = " + bean.getName();
            } else {
                return "null";
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    八、请求验证

    请求地址:http://localhost:8991/mysql/studentname=tom
    在这里插入图片描述

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    Linux IO重定向及管道
    位移贴图和法线贴图的区别
    【Vue】Non-Props 属性是什么
    迭代器模式(Iterator)
    五号黯区靶场 mysql 注入之limit注入记录
    (免费领源码)java#SSM#Mysql学院教室管理系统81671-计算机毕业设计项目选题推荐
    换个角度入门 K8s
    2、数组、Map+HashMap、Set+Hashset、Char和Character类、String类和Char类、Math类
    使用 Visual Studio 断点调试 DLL
    Linux 命令使用笔记【mapstat】
  • 原文地址:https://blog.csdn.net/web18224617243/article/details/126114323