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

SpringBoot+MySQL+MyBatis的入门教程

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

SpringBoot+MySQL+MyBatis的入门教程

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

本系列教程为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;


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


@Mapper
public interface TMsgMapper {

    public TMsg findById(Integer id);

}



    
 SELECT id,message from t_msg WHERe id = #{id}
    

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

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

import com.myself.mybatis.entity.TMsg;


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;


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


@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/UncleCatMySelf/HMStrange/tree/master/doc/demo/datademo

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

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

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