SpringBoot 集成Mybatis

文章目录

一、创建SpringBoot项目

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

二、添加Mybatis相关依赖

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

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.1</version>
</dependency>

三、数据源配置

在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/studentname=tom
在这里插入图片描述

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值