SpringBoot MybatisPlus 环境搭建

SpringBoot MybatisPlus 环境搭建

SpringBoot MybatisPlus 环境搭建

本文介绍IntelliJ IDEA编译工具下,搭建SpringBoot2.1.7 MybatisPlus3.x开发环境

SpringBoot2.1.7

初始化工程

使用 Spring Initializer 快速初始化一个 Spring Boot 工程
SpringBoot MybatisPlus 环境搭建_第1张图片
这里采用Kotlin语言环境,Gradle自动构建工具

点击生产项目按钮
点击生产项目按钮
得到demo.zip项目压缩文件,将其解压

导入工程

打开IntelliJ IDEA

点击Import Project
SpringBoot MybatisPlus 环境搭建_第2张图片
选择demo.zip解析根目录下的build.gradle.kts
SpringBoot MybatisPlus 环境搭建_第3张图片
勾选Use auto-import自动导入文件,**Use default Gradle wrapper (recommended)**自动使用默认的gradle(一般为在线下载,本文Gradle版本为5.4,JDK版本1.8
SpringBoot MybatisPlus 环境搭建_第4张图片
项目文件夹结构
SpringBoot MybatisPlus 环境搭建_第5张图片
引入依赖
SpringBoot MybatisPlus 环境搭建_第6张图片

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
	id("org.springframework.boot") version "2.1.7.RELEASE"
	id("io.spring.dependency-management") version "1.0.8.RELEASE"
	kotlin("jvm") version "1.2.71"
	kotlin("plugin.spring") version "1.2.71"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
	mavenCentral()
}

dependencies {
	implementation("org.springframework.boot:spring-boot-starter")
	implementation("org.jetbrains.kotlin:kotlin-reflect")
	implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
	implementation("org.springframework.boot:spring-boot-starter-jdbc")
	implementation("org.springframework.boot:spring-boot-starter-web")
	implementation("org.springframework.boot:spring-boot-starter-web-services")
	implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
	implementation("org.jetbrains.kotlin:kotlin-reflect")
	implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
	implementation("org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.0")
	testImplementation("org.springframework.boot:spring-boot-starter-test")
	testImplementation("org.springframework.boot:spring-boot-starter-test")
	compileOnly("org.projectlombok:lombok")
	runtimeOnly("mysql:mysql-connector-java")
	annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
	annotationProcessor("org.projectlombok:lombok")
	implementation("com.baomidou:mybatis-plus-boot-starter:3.2.0")
}

tasks.withType {
	kotlinOptions {
		freeCompilerArgs = listOf("-Xjsr305=strict")
		jvmTarget = "1.8"
	}
}

application.yml
SpringBoot MybatisPlus 环境搭建_第7张图片

SpringBoot MybatisPlus 环境搭建_第8张图片
代码片段

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useInformationSchema=true&autoReconnect=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8
    username: root
    password: root

mybatis-plus:
  mapper-locations: classpath:mapper/xml/*.xml

测试SpringBoot MybatisPlus 环境搭建_第9张图片

package com.example.demo

import com.example.demo.mapper.UserMapper
import com.example.demo.model.User
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner
import java.util.function.Consumer

@RunWith(SpringRunner::class)
@SpringBootTest
class DemoApplicationTests {

	@Test
	fun contextLoads() {
	}

	@Autowired
	private val userMapper: UserMapper? = null

	@Test
	fun testSelect() {
		println("----- selectAll method test ------")
		val userList = userMapper!!.selectList(null)
		Assert.assertEquals(5, userList.size)
		userList.forEach(Consumer { System.out.println(it) })
	}

}

Application
SpringBoot MybatisPlus 环境搭建_第10张图片

package com.example.demo

import org.mybatis.spring.annotation.MapperScan
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
class DemoApplication

fun main(args: Array) {
	runApplication(*args)
}

UserMapper
SpringBoot MybatisPlus 环境搭建_第11张图片

package com.example.demo.mapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper
import com.example.demo.model.User

interface UserMapper : BaseMapper

User
SpringBoot MybatisPlus 环境搭建_第12张图片

package com.example.demo.model

import lombok.Data

@Data
class User {
    private val id: Long? = null
    private val name: String? = null
    private val age: Int? = null
    private val email: String? = null
}

使用Navicat连接本地MySql数据库,新建test数据库,运行schema.sqluser.sql脚本进行建表和导入数据

schema.sql

DROP TABLE IF EXISTS user;

CREATE TABLE user
(
	id BIGINT(20) NOT NULL COMMENT '主键ID',
	name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
	age INT(11) NULL DEFAULT NULL COMMENT '年龄',
	email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
	PRIMARY KEY (id)
);

user.sql

DELETE FROM user;

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');

进行测试
SpringBoot MybatisPlus 环境搭建_第13张图片
出现以下结果表示环境搭建成功
SpringBoot MybatisPlus 环境搭建_第14张图片

你可能感兴趣的:(环境搭建)