资讯专栏INFORMATION COLUMN

SpringBoot+MySQL+MyBatis的入门教程

kycool / 1150人阅读

摘要:历史文章如何在安装最新版安装安装最新版教程内容备注本系列开发工具均为构建项目,选择四个基本的依赖。层与其实现,这个比较简单,一般做过项目的都了解层,我这边构建了一个方法,通过获取信息。

本博客 猫叔的博客,转载请申明出处

本系列教程为HMStrange项目附带。

历史文章

如何在VMware12安装Centos7.6最新版

Centos7.6安装Java8

Centos7.6安装MySQL+Redis(最新版)

教程内容
备注:本系列开发工具均为IDEA
1、构建项目,选择Lombok、Web、MySQL、MyBatis四个基本的Maven依赖。

大家可以看看pom文件



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    
    com.myself.mybatis
    datademo
    0.0.1-SNAPSHOT
    datademo
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.1
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

2、准备MySQL,这里可以参考历史文章的安装MySQL环节,我新建了一个数据库,针对这个项目,构建了一张简单的表。

DDL

CREATE TABLE `t_msg` (
  `id` int(11) NOT NULL,
  `message` varchar(255) DEFAULT NULL COMMENT "信息",
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
3、构建项目目录,我构建了一个经典的web项目目录结构,entity实体类、mapper映射、service接口、impl接口实现、controller业务访问、resources/mapper包用于存放xml

4、填写application.yml,默认生成不是yml,不过我觉得yml视觉效果好一些,就改了一下,我们需要填写数据库信息,还有mybatis的数据库映射地址,实体类地址
spring:
  datasource:
    url: jdbc:mysql://192.168.192.133:3306/datademo?characterEncoding=utf-8&useSSL=false
    username: root
    password: password
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  mapper-locations: classpath*:mapper/*Mapper.xml
  type-aliases-package: com.myself.mybatis.entity
5、构建数据库对应的实体类TMsg,这个类放在entity
package com.myself.mybatis.entity;

import lombok.Data;

import java.io.Serializable;

/**
 * Created by MySelf on 2019/4/9.
 */
@Data
public class TMsg implements Serializable {

    private Integer id;

    private String message;

}
6、构建对应的Mapper接口(其实就类似dao层),这里与TMsgMapper.xml文件对应关系
package com.myself.mybatis.mapper;

import com.myself.mybatis.entity.TMsg;
import org.apache.ibatis.annotations.Mapper;

/**
 * Created by MySelf on 2019/4/9.
 */
@Mapper
public interface TMsgMapper {

    public TMsg findById(Integer id);

}



    

我这边就单纯一个方法,大家可以扩展自己的方法。

7、service层与其实现,这个比较简单,一般做过web项目的都了解
package com.myself.mybatis.service;

import com.myself.mybatis.entity.TMsg;

/**
 * Created by MySelf on 2019/4/9.
 */
public interface TMsgService {

    public TMsg findById(Integer id);

}
package com.myself.mybatis.service.impl;

import com.myself.mybatis.entity.TMsg;
import com.myself.mybatis.mapper.TMsgMapper;
import com.myself.mybatis.service.TMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by MySelf on 2019/4/9.
 */
@Service
public class TMsgServiceImpl implements TMsgService {

    @Autowired
    private TMsgMapper tMsgMapper;

    @Override
    public TMsg findById(Integer id) {
        return tMsgMapper.findById(id);
    }
}
8、controller层,我这边构建了一个get方法,通过id获取信息。
package com.myself.mybatis.controller;

import com.myself.mybatis.entity.TMsg;
import com.myself.mybatis.service.TMsgService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by MySelf on 2019/4/9.
 */
@RestController
@RequestMapping("/msg")
public class TMsgController {

    @Autowired
    private TMsgService tMsgService;

    @GetMapping("/getMsg")
    public String getMsg(@Param("id") Integer id){
        TMsg tMsg = tMsgService.findById(id);
        return tMsg.getMessage();
    }

}
9、启动项目,并使用Postman测试

10、项目下载地址

欢迎到HMStrange项目进行下载:https://github.com/UncleCatMy...

公众号:Java猫说

学习交流群:728698035

现架构设计(码农)兼创业技术顾问,不羁平庸,热爱开源,杂谈程序人生与不定期干货。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77503.html

相关文章

  • SpringBoot非官方教程 | 第七篇:SpringBoot开启声明式事务

    摘要:准备阶段以上一篇文章的代码为例子,即整合,上一篇文章是基于注解来实现的数据访问层,这篇文章基于的来实现,并开启声明式事务。创建实体类数据访问层接口层用户减块用户加块,声明事务,并设计一个转账方法,用户减块,用户加块。 springboot开启事务很简单,只需要一个注解@Transactional 就可以了。因为在springboot中已经默认对jpa、jdbc、mybatis开启了事事...

    tyheist 评论0 收藏0
  • SpringBoot整合MyBatis并使用Redis作为缓存组件Demo

    摘要:本博客猫叔的博客,转载请申明出处本系列教程为项目附带。历史文章如何在安装最新版安装安装最新版的入门教程的入门教程安装教程安装流程安装如果不清楚是什么,请查看的文档和简介,这里给出的安装过程安装虚拟机如果有远程服务器的,请略过此步骤本文推 本博客 猫叔的博客,转载请申明出处本系列教程为HMStrange项目附带。 Auth:HMStrange-TIAN e-mail:zhangqihao...

    mo0n1andin 评论0 收藏0

发表评论

0条评论

kycool

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<