栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot 集成Mybatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot 集成Mybatis

文章目录
  • 一、创建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


三、数据源配置

在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

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

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

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;
    }
}

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


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

这个类主要是和数据进行交互联系的,需要配置好实体类和数据库字段的映射关系。由于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);
}

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


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

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

package com.springboottest.sql.service;

import com.springboottest.bean.StudentBean;

public interface StudentService {

    StudentBean getStudentInfoByName(String name);
}

实现类如下图:

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);
    }
}

@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";
        }
    }
}

八、请求验证

请求地址:http://localhost:8991/mysql/student?name=tom



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/838749.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号