SpringBoot MybatisPlus MySQL druid联合基础使用

1 Docker安装MySQL

1.1 安装MySQL

# 下载MySQL5.7
sudo docker pull mysql:5.7

# 在home目录下创建conf,data,logs三个目录
sudo mkdir -p /home/mysql/{conf,data,log}

# 安装MySQL,密码是123456
docker run -itd --name m_mysql -p 3306:3306 \
-v /home/mysql/conf:/etc/mysql/conf.d \
-v /home/mysql/data:/var/lib/mysql \
-v /home/mysql/log:/var/log/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:5.7

1.2 创建表

# 进入容器
sudo docker exec -it m_mysql /bin/bash

# 进入容器后,进入MySQL,输入密码为123456
mysql -u root -p

# !!注意!! 一定要设置编码方式
# 创建数据库
CREATE DATABASE mytest DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

# 创建表
CREATE TABLE article (
	title VARCHAR(50) NOT NULL,
	summary VARCHAR(300) NOT NULL,
	keywords VARCHAR(100) NOT NULL	
)ENGINE=INNODB DEFAULT CHARSET=utf8;

2 pom.xml文件


<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0modelVersion>
	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>2.2.5.RELEASEversion>
		<relativePath/> 
	parent>
	<groupId>com.examplegroupId>
	<artifactId>demoartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<name>demoname>
	<description>Demo project for Spring Bootdescription>
	<properties>
		<java.version>11java.version>
	properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>

		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
		dependency>

		
		<dependency>
			<groupId>com.alibabagroupId>
			<artifactId>fastjsonartifactId>
			<version>1.2.58version>
		dependency>

		
		<dependency>
			<groupId>org.projectlombokgroupId>
			<artifactId>lombokartifactId>
			<version>1.18.20version>
			<scope>providedscope>
		dependency>

		
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-configuration-processorartifactId>
			<optional>trueoptional>
		dependency>

		
		<dependency>
			<groupId>mysqlgroupId>
			<artifactId>mysql-connector-javaartifactId>
			<version>8.0.21version>
		dependency>

		<dependency>
			<groupId>com.baomidougroupId>
			<artifactId>mybatis-plus-boot-starterartifactId>
			<version>3.4.2version>
		dependency>

		
		<dependency>
			<groupId>com.alibabagroupId>
			<artifactId>druid-spring-boot-starterartifactId>
			<version>1.2.4version>
		dependency>

	dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
				<version>2.3.1.RELEASEversion>
			plugin>
		plugins>
	build>

project>

3 application.yml

spring:
  datasource:
    # config mysql
    url: jdbc:mysql://192.168.108.60:3306/mytest?useUnicode=true&&characterEncoding=utf-8&&useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

    # set druid
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      initial-size: 5
      min-idle: 5
      max-active: 20
      max-wait: 6000
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 30000
      test-on-borrow: false
      test-on-return: false
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20

# print logs
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

4 MyBatis-plus中Mapper和IService的区别

(1)Mapper可以实现简单的CRUD,Service可以实现更加丰富的CRUD,但须编写mapper接口。
(2)Mapper需要继承BaseMapper,Service需要继承IService.
(3)BaseMapper只提供了一些基础的CRUD操作,IService除了提供基本的CRUD操作以外,还提供了很多的批处理方法。
(4)IService是在BaseMapper的基础上封装了更多的方法,但最终还是要依赖于BaseMapper。

5 创建Java程序

5.1 工程目录

SpringBoot MybatisPlus MySQL druid联合基础使用_第1张图片

5.2 创建实体

创建article实体

package com.example.demo.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("article")
public class Article {
    private String title;
    private String summary;
    private String keywords;
}

5.3 创建Dao

(1)创建Dao的mpmapper包下的ArticleMapper.java

package com.example.demo.dao.mpmapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.Article;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface ArticleMapper extends BaseMapper<Article> {
}

(2)创建Dao的mpiservice包下的ArticleIService.java

package com.example.demo.dao.mpiservice;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.entity.Article;


public interface ArticleIService extends IService<Article> {
}

(3)创建Dao的mpiserviceimp包下的ArticleIServiceImp.java

ServiceImpl类的作用是系统自动实现IService相对应的接口

package com.example.demo.dao.mpiserviceimp;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dao.mpmapper.ArticleMapper;
import com.example.demo.dao.mpiservice.ArticleIService;
import com.example.demo.entity.Article;
import org.springframework.stereotype.Repository;

@Repository
public class ArticleIServiceImp extends ServiceImpl<ArticleMapper, Article> implements ArticleIService {
}

5.4 创建Service

创建DataService.java

package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.dao.mpiserviceimp.ArticleIServiceImp;
import com.example.demo.dao.mpmapper.ArticleMapper;
import com.example.demo.entity.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DataService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleIServiceImp articleIServiceImp;


    // 添加数据
    public int addMultiData(){
        // 批量添加数据
        List<Article> articleList = new ArrayList<>();
        articleList.add(new Article("河南大学","河南大学在开封","河南,大学"));
        articleList.add(new Article("软件学院","河南大学软件学院","软件,学院"));
        articleList.add(new Article("中国","中国人民最伟大","中国,人民"));

        this.articleIServiceImp.saveBatch(articleList);
        return articleList.size();
    }

    // 获取数据
    public List<Article> getMultiData(){
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("title","河南大学");
        return this.articleMapper.selectList(wrapper);
    }

}

5.5 创建Application

注意添加Mybatis-plus的map包地址,如果不添加应用可能找不到mapper。

package com.example.demo;

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


@SpringBootApplication
@MapperScan("com.example.demo.dao.mpmapper")
public class DemoApplication {

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

}

6 运行

(1)添加数据

SpringBoot MybatisPlus MySQL druid联合基础使用_第2张图片

(2)查询数据

SpringBoot MybatisPlus MySQL druid联合基础使用_第3张图片

你可能感兴趣的:(mysql,spring,boot,java)