IDEA 搭建SpringBoot项目:SpringBoot + MyBatis + Druid +MyBatis-Generator

今天第一次使用IDEA开发工具,搭建SpringBoot项目,记录下重点步骤。

1、IDEA 创建基于Maven管理的Java项目。

IDEA 搭建SpringBoot项目:SpringBoot + MyBatis + Druid +MyBatis-Generator_第1张图片

2、pom.xml 文件添加

jar包依赖:SpringBoot版本、MyBatis、MySQL8驱动和Alibaba Druid 数据库连接池。

插件依赖:添加maven 插件和MyBatis-Generator 代码生产工具。



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.2.RELEASE
    

    org.zzg
    SmartHouse
    1.0-SNAPSHOT

    
        1.8
        8
        8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            junit
            junit
            RELEASE
            compile
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        
        
        
            mysql
            mysql-connector-java
            8.0.12
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.6.6
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.5
                
                    src/main/resources/mybatis-generator/mybatis-generator-cfg.xml
                    
                    true
                    true
                
                
                
                
                    
                        org.mybatis.generator
                        mybatis-generator-core
                        1.3.5
                    
                    
                    
                        mysql
                        mysql-connector-java
                        8.0.12
                    
                
            

        
    


 3、添加application.properties 配置文件,主要包含如下配置:项目端口、项目名称设置、数据库连接、druid 连接配置参数、mybatis扫描配置和日志输出级别设置。

server.prot=8080
server.servlet.context-path=/smartHouse
# 数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/house?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# druid 配置
# 初始化时建立物理连接的个数
spring.datasource.druid.initial-size=5
# 最大连接池数量
spring.datasource.druid.max-active=30
# 最小连接池数量
spring.datasource.druid.min-idle=5
# 获取连接时最大等待时间,单位毫秒
spring.datasource.druid.max-wait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druid.time-between-eviction-runs-millis=60000
# 连接保持空闲而不被驱逐的最小时间
spring.datasource.druid.min-evictable-idle-time-millis=300000
# 用来检测连接是否有效的sql,要求是一个查询语句
spring.datasource.druid.validation-query=SELECT 1 FROM DUAL
# 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
spring.datasource.druid.test-while-idle=true
# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
spring.datasource.druid.test-on-borrow=false
# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
spring.datasource.druid.test-on-return=false
# 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。
spring.datasource.druid.pool-prepared-statements=true
# 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=50
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计
spring.datasource.druid.filters=stat,wall
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
# 合并多个DruidDataSource的监控数据
spring.datasource.druid.use-global-data-source-stat=true
# mybatis 配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.zzg.model
# 日志记录输出配置
logging.level.com.zzg.mapper=debug

温馨提示:目前Springboot中默认支持的连接池有dbcp,dbcp2, tomcat, hikari三种连接池。由于Druid暂时不在Springboot中的直接支持,所以需要通过配置文件直接指定加载连接池类型或者自定义配置对象设置连接池。

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

4、在项目的resource目录下,新增mybatis-generator 文件夹主要用于存放mybatis-generator-cfg.xml 代码生成配置文件。





    
    
        
        
        
        
        
        

        
        
            
             
        

        
        
        

        
        
            
            
        

        
        
            
            
        

        
        
            
        

        
        
            
        

        

5、执行MyBatis-Generator 代码生成插件。

IDEA 搭建SpringBoot项目:SpringBoot + MyBatis + Druid +MyBatis-Generator_第2张图片

选中需要执行代码生成的项目,按照截图标记的顺序执行:1->2->3->4->5

执行效果展示:

IDEA 搭建SpringBoot项目:SpringBoot + MyBatis + Druid +MyBatis-Generator_第3张图片

 6、编写SpringBoot程序入口和Controller实现

SpringBoot程序入口主要实现:1、添加SpringBoot程序标记@SpringBootApplication.

                                                  2、扫描Mapper 接口。

 

package com.zzg;


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

/**
 * 1.0.0	默认:1.0.0
 * zzg	作者信息,可在通用配置里修改作者信息
 * 2022/04/19	日期信息,格式可在通用配置中修改
 * 应用程序开始	注释信息
 **/
@SpringBootApplication
@MapperScan("com.zzg.mapper")
public class ApplicationStart {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationStart.class, args);
    }
}

FirstController:主要实现Mapper 接口调用。

package com.zzg.controller;

import com.zzg.mapper.UserMapper;
import com.zzg.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class FirstController {
    @Autowired
    private UserMapper mapper;

    @RequestMapping("/hello")
    public String get() {
        List list = mapper.selectAll();
        return "Hello Spring Boot!";
    }
}

你可能感兴趣的:(IDEA,编译工具,intellij-idea)