springboot + maven + mybatis-plus + mysql8 整合

springboot 2.4.1
JDK 1.8
mybatis-plus 3.2.0
mysql-connector-java 8.0.11
druid 1.1.18

1、假数据

springboot + maven + mybatis-plus + mysql8 整合_第1张图片
sql脚本

/*
Navicat MySQL Data Transfer

Source Server         : mysql
Source Server Version : 80022
Source Host           : localhost:3306
Source Database       : mytest

Target Server Type    : MYSQL
Target Server Version : 80022
File Encoding         : 65001

Date: 2021-01-11 19:32:11
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `name` varchar(32) DEFAULT NULL COMMENT '姓名',
  `alisa` varchar(32) DEFAULT NULL,
  `age` int DEFAULT NULL COMMENT '年龄',
  `hall` varchar(16) DEFAULT NULL COMMENT '身高',
  `sex` varchar(16) DEFAULT NULL COMMENT '性别',
  `weight` double DEFAULT NULL COMMENT '体重',
  `email` varchar(64) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('张三', 'zhangsan', '18', '180', 'male', '70', '[email protected]');
INSERT INTO `user` VALUES ('李四', 'lisi', '19', '181', 'female', '60', '[email protected]');
INSERT INTO `user` VALUES ('王五', 'wangwu', '20', '182', 'male', '80', '[email protected]');
INSERT INTO `user` VALUES ('赵六', 'zhaoliu', '21', '183', 'male', '75', '[email protected]');

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.4.1version>
        <relativePath/> 
    parent>
    <groupId>com.ilyucgroupId>
    <artifactId>easy_excelartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>easy_excelname>
    <description>EasyExcel project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.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.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.2.0version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.11version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.1.18version>
        dependency>
        
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-coreartifactId>
        dependency>

    dependencies>

    <build>
  











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

project>

3、application.yml

注意 mybatis 的 xml 的位置配置

server:
  port: 9090

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=UTC&allowMultiQueries=true&useAffectedRows=true
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource  # 使用 DruidDataSource作为数据源
  druid:
    initialSize: 5 #初始化连接大小
    minIdle: 5     #最小连接池数量
    maxActive: 20  #最大连接池数量
    maxWait: 60000 #获取连接时最大等待时间,单位毫秒
    timeBetweenEvictionRunsMillis: 60000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    minEvictableIdleTimeMillis: 300000   #配置一个连接在池中最小生存的时间,单位是毫秒
    validationQuery: SELECT 1 from DUAL  #测试连接
    testWhileIdle: true                  #申请连接的时候检测,建议配置为true,不影响性能,并且保证安全性
    testOnBorrow: false                  #获取连接时执行检测,建议关闭,影响性能
    testOnReturn: false                  #归还连接时执行检测,建议关闭,影响性能
    poolPreparedStatements: false       #是否开启PSCache,PSCache对支持游标的数据库性能提升巨大,oracle建议开启,mysql下建议关闭
    maxPoolPreparedStatementPerConnectionSize: 20 #开启poolPreparedStatements后生效
    filters: stat,wall,slf4j   #配置扩展插件,常用的插件有=>stat:监控统计  slf4j:日志  wall:防御sql注入
    connectionProperties: 'druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000'

# MyBatis配置
mybatis-plus:
  # xml文件位置
  mapper-locations: classpath:com/ilyuc/easy_excel/dao/*.xml
  # 别名类型包
  type-aliases-package: com.ilyuc.easy_excel.entity
  configuration:
    # 开启驼峰转换
    map-underscore-to-camel-case: true
    # MyBatis日志打印类
    log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl

# 配置logback日志组件
logging:
  config: classpath:logback-spring.xml
4、entity
package com.ilyuc.easy_excel.entity;

/**
 * @author ilyuc
 * @version v 1.0.0
 * @Description
 * @date 2021/1/6 13:50
 */
public class UserEntity {

    private String name;
    private String alisa;
    private int age;
    private String hall;
    private String sex;
    private double weight;
    private String email;

    public void setName(String name) {
        this.name = name;
    }

    public void setAlisa(String alisa) {
        this.alisa = alisa;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setHall(String hall) {
        this.hall = hall;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public String getAlisa() {
        return alisa;
    }

    public int getAge() {
        return age;
    }

    public String getHall() {
        return hall;
    }

    public String getSex() {
        return sex;
    }

    public double getWeight() {
        return weight;
    }

    public String getEmail() {
        return email;
    }

    @Override
    public String toString() {
        return "UserEntity{" +
                "name='" + name + '\'' +
                ", alisa='" + alisa + '\'' +
                ", age='" + age + '\'' +
                ", hall='" + hall + '\'' +
                ", sex='" + sex + '\'' +
                ", weight='" + weight + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}
5、mapper 接口
package com.ilyuc.easy_excel.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ilyuc.easy_excel.entity.UserEntity;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * @author ilyuc
 * @version v 1.0.0
 * @Description
 * @date 2021/1/6 19:23
 */
@Mapper
public interface UserMapper extends BaseMapper<UserEntity> {

    List<UserEntity> getUser(UserEntity userEntity);

}
5、mapper.xml

namespace 是 mapper 接口


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ilyuc.easy_excel.dao.UserMapper">

    

    <resultMap type="UserEntity" id="CountDataResult">
        <result property="name" column="name" javaType="String"/>
        <result property="alisa" column="alisa" javaType="String"/>
        <result property="age" column="age" javaType="int"/>
        <result property="hall" column="hall" javaType="String"/>
        <result property="sex" column="sex" javaType="String"/>
        <result property="weight" column="weight" javaType="double"/>
        <result property="email" column="email" javaType="String"/>
    resultMap>

    <select id="getUser" parameterType="UserEntity" resultMap="CountDataResult">
        select
        *
        from user t where 1=1

        <if test="name!=null and name!=''">
            and instr(t.name, #{name} ) ]]>0
        if>
        <if test="alisa!=null and alisa!=''">
            and instr(t.alisa, #{alisa} ) ]]>0
        if>
        <if test="email!=null and email!=''">
            and instr(t.email, #{email} ) ]]>0
        if>

    select>

mapper>

项目路径图
springboot + maven + mybatis-plus + mysql8 整合_第2张图片
(完)

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