登录
首页 >  数据库 >  MySQL

SpringBoot 整合 Mybatis(入门版)

来源:SegmentFault

时间:2023-02-24 15:00:24 100浏览 收藏

小伙伴们有没有觉得学习数据库很有意思?有意思就对了!今天就给大家带来《SpringBoot 整合 Mybatis(入门版)》,以下内容将会涉及到MySQL、Java、springboot、mybatis,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!

SpringBoot 整合 Mybatis

基本信息

技术栈

Spring Boot 2.4.5、Mybatis

学习目的

SpringBoot 整合 Mybatis

项目详解

新建一个 Spring Initializr 项目

image-20210519172857294.png

创建项目的文件结构以及选择 jdk 的版本

image-20210519173013426.png

选择项目所需要的依赖

image.png

修改项目名,点击 Finish 完成

image.png

建好项目后生成的 pom.xml

4.0.0org.springframework.bootspring-boot-starter-parent2.4.5com.malfspringboot_mybatis0.0.1-SNAPSHOTspringboot_mybatisDemo project for Spring Boot1.8org.springframework.bootspring-boot-starter-jdbcorg.mybatis.spring.bootmybatis-spring-boot-starter2.1.4mysqlmysql-connector-javaruntimeorg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-maven-plugin

修改配置文件

本文不使用 application.properties 文件,而使用更加简洁的 application.yml 文件。将 resource 文件夹下原有的 application.properties 文件删除,创建 application.yml 配置文件(备注:SpringBoot 底层会把 application.yml 文件解析为application.properties),本文创建了两个 yml 文件(application.yml 和 application-dev.yml)

application.yml

spring:
  profiles:
    active: dev
application-dev.yml

server:
  port: 8000
mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.malf.entity
#showSql
logging:
  level:
    com:
      example:
        mapper : debug
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/springboot_mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

数据库表结构

CREATE TABLE `user` (
  `id` int(32) NOT NULL AUTO_INCREMENT,
  `userName` varchar(32) NOT NULL,
  `passWord` varchar(50) NOT NULL,
  `realName` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

创建实体类实现业务流程

创建包 controller、entity、dao、service,resources 下创建 mapping 文件夹,用于写 sql 语句,也可以用注解的方式直接写在 mapper 文件里。

entity

package com.malf.entity;

/**
 * @author 巅峰小词典
 * @description
 * @date 2021/5/20
 * @project springboot_mybatis
 */
public class User {

    private Integer id;
    private String userName;
    private String passWord;
    private String realName;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord + '\'' +
                ", realName='" + realName + '\'' +
                '}';
    }

}
controller

package com.malf.controller;

import com.malf.service.UserService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 巅峰小词典
 * @description
 * @date 2021/5/20
 * @project springboot_mybatis
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("getUser/{id}")
    public String GetUser(@PathVariable int id) {
        return userService.selectById(id).toString();
    }

}
dao

package com.malf.dao;

import com.malf.entity.User;
import org.springframework.stereotype.Repository;

/**
 * @author 巅峰小词典
 * @description
 * @date 2021/5/20
 * @project springboot_mybatis
 */
@Repository
public interface UserMapper {

    User selectById(int id);

}
service

package com.malf.service;

import com.malf.entity.User;

/**
 * @author 巅峰小词典
 * @description
 * @date 2021/5/20
 * @project springboot_mybatis
 */
public interface UserService {

    public User selectById(int id);

}
service.impl

package com.malf.service.impl;

import com.malf.entity.User;
import com.malf.dao.UserMapper;
import com.malf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 巅峰小词典
 * @description
 * @date 2021/5/20
 * @project springboot_mybatis
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    public User selectById(int id){
        return userMapper.selectById(id);
    }

}
xml

最终框架结构

image.png

在启动类里加上注解用于给出需要扫描的 mapper 文件路径 @MapperScan("com.malf.dao")

package com.malf;

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

@SpringBootApplication
@MapperScan("com.malf.dao") // 扫描的mapper
public class SpringbootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}

最后启动,浏览器输入地址:
http://localhost:8000/user/getUser/1

image.png

测试成功,SpringBoot 整合 Mybatis 基本框架就搭建成功了。

源码参考

springboot_mybatis

今天带大家了解了MySQL、Java、springboot、mybatis的相关知识,希望对你有所帮助;关于数据库的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表