spring boot+mybatis-plus整理

Posted lideqiang0909

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot+mybatis-plus整理相关的知识,希望对你有一定的参考价值。

简介MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

springboot+maven+mybatis-plus+mysql

项目整体结构:

 技术图片

 

 

Mybatis-plus需要的以来文件:

<!--mybatis-plus自动的维护了mybatis以及mybatis-spring的依赖,
  springboot中这三者不能同时的出现,避免版本的冲突,表示:跳进过这个坑-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.0.1</version>
</dependency>
<!--集成mybatis -->
<!-- 与数据库操作相关的依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

 

 

<!-- mybatisplus 代码生成器 -->
<!-- 模板引擎 -->
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.0</version>
</dependency>
<!-- 模板引擎,需要指定 mpg.setTemplateEngine(new FreemarkerTemplateEngine()); -->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.23</version>
</dependency>

 

 

 

 

编写mybatisplus  自动生成器MybatisPlusGenerator.java(基于单列模式进行创建)

在MybatisPlusGenerator中配置生成的文件存放位置以及数据库连接

运行Main方法生成全套文件

 

package com.lee.springbootdemo.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
* @author Administrator
* @title: MybatisPlusGenerator
* @projectName springboot-demo
* @description: TODO
* @date 2019/9/16 0016下午 14:49
*/
public class MybatisPlusGenerator
private static MybatisPlusGenerator single = null;

private MybatisPlusGenerator()
super();


private static MybatisPlusGenerator getSingle()
if (single == null)
single = new MybatisPlusGenerator();

return single;


public void autoGeneration()
GlobalConfig config = new GlobalConfig();
String dbUrl = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8";
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL)
.setUrl(dbUrl)
.setUsername("root")
.setPassword("123456")
.setDriverName("com.mysql.jdbc.Driver");
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig
.setCapitalMode(true)
.setEntityLombokModel(false)
// .setDbColumnUnderline(true)
.setNaming(NamingStrategy.underline_to_camel);
config.setActiveRecord(false)
.setEnableCache(false)
.setAuthor("admin")
//指定输出文件夹位置
.setOutputDir("D:\\\\project\\\\springboot-demo\\\\src\\\\main\\\\java")
.setFileOverride(true)
.setServiceName("%sService");
new AutoGenerator().setGlobalConfig(config)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setPackageInfo(
new PackageConfig()
.setParent("com.lee.springboot")
.setController("controller")
.setEntity("entity")
).execute();


public static void main(String[] args)
// TODO Auto-generated method stub
MybatisPlusGenerator generator = MybatisPlusGenerator.getSingle();
generator.autoGeneration();



 

项目的配置信息

MybatisPlusConfig 中配置数据源,分页插件,事务管理,sql执行性能分析

配置MapperScan扫描dao/mapper接口

 

package com.lee.springbootdemo.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;

/**
* @author Administrator
* @title: MybatisPlusConfig
* @projectName springboot-demo
* @description: TODO
* @date 2019/9/18 0018下午 17:09
*/
@Configuration
@MapperScan("com.lee.springboot.mapper")
public class MybatisPlusConfig
/***
* plus 的性能优化
* @return
*/
@Bean
public PerformanceInterceptor performanceInterceptor()
PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
/*<!-- SQL 执行性能分析,开发环境使用,线上不推荐。 maxTime 指的是 sql 最大执行时长 -->*/
performanceInterceptor.setMaxTime(1000);
/*<!--SQL是否格式化 默认false-->*/
performanceInterceptor.setFormat(true);
return performanceInterceptor;


/**
* @Description : mybatis-plus分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor()
return new PaginationInterceptor();


// 配置数据源
@Bean(name="dataSource")
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource()
return new DruidDataSource();


// 配置事物管理器
@Bean(name="transactionManager")
public DataSourceTransactionManager transactionManager()
return new DataSourceTransactionManager(dataSource());




配置application.properties

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
mybatis-plus.mapper-locations=classpath:/mapper/*Mapper.xml
mybatis-plus.typeAliasesPackage=com.zzg.springboot.entity


做完以上就可以做一个简单地测试了。
package com.lee.springbootdemo;
import com.lee.springboot.entity.ASCategory;
import com.lee.springboot.mapper.ASCategoryMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootDemoApplicationTests

@Resource
private ASCategoryMapper asCategoryMapper;

@Test
public void contextLoads()
System.out.println("run in ----------------->test");
List<ASCategory> asCategoryList = asCategoryMapper.selectList(null);
asCategoryList.forEach(System.out::println);


技术图片

 原文地址:https://blog.csdn.net/zhouzhiwengang/article/details/81059086

以上是关于spring boot+mybatis-plus整理的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis-Plus详细讲解(整合spring Boot)

spring boot 整合Mybatis-plus

264.Spring Boot MyBatis集成MyBatis-Plus

spring boot整合mybatis+mybatis-plus

spring boot整合mybatis+mybatis-plus

spring boot整合mybatis+mybatis-plus