SpringBoot+MySQL+MyBatis的入門教程

Java貓說發表於2019-04-10

本部落格 貓叔的部落格,轉載請申明出處

本系列教程為HMStrange專案附帶。

歷史文章

教程內容

備註:本系列開發工具均為IDEA

1、構建專案,選擇Lombok、Web、MySQL、MyBatis四個基本的Maven依賴。

image

大家可以看看pom檔案

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.myself.mybatis</groupId>
	<artifactId>datademo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>datademo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.0.1</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>
複製程式碼

2、準備MySQL,這裡可以參考歷史文章的安裝MySQL環節,我新建了一個資料庫,針對這個專案,構建了一張簡單的表。

image

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

image

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

}
複製程式碼
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.myself.mybatis.mapper.TMsgMapper">
    <select id="findById" resultType="com.myself.mybatis.entity.TMsg">
        SELECT id,message from t_msg WHERE id = #{id}
    </select>
</mapper>
複製程式碼

我這邊就單純一個方法,大家可以擴充套件自己的方法。

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測試

image

image

10、專案下載地址

歡迎到HMStrange專案進行下載:github.com/UncleCatMyS…

公眾號:Java貓說

學習交流群:728698035

現架構設計(碼農)兼創業技術顧問,不羈平庸,熱愛開源,雜談程式人生與不定期乾貨。

Image Text