spring boot + mybatis-plus finishing

Description : MyBatis-Plus (abbreviated MP ) is a MyBatis enhancement tools, in MyBatis only enhance not change based on, to simplify development, increase efficiency and health.

springboot+maven+mybatis-plus+mysql

The overall project structure:

 

 

 

Mybatis-plus needed since the file:

<-! mybatis-plus automatically maintained mybatis and rely mybatis-spring, and
  in can not appear the three springboot, avoiding version conflicts, said: jump into the pit too ->
<dependency>
    <groupId > com.baomidou </ the groupId>
    <the artifactId> MyBatis-PLUS-Boot-Starter </ the artifactId>
    <Version> 3.0.1 </ Version>
</ dependency>
<-! integration MyBatis ->
<-! and related database operations dependent ->
<dependency>
    <the groupId> org.springframework.boot </ the groupId>
    <the artifactId> Boot-Starter-Spring-JDBC </ the 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>

 

 

 

 

Write mybatisplus   automatic generator MybatisPlusGenerator.java ( created based on single mode )

MybatisPlusGenerator configured to generate the database and the file location is connected

Run Main method of generating a full set of documents

 

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();
}


}

 

The project configuration information :

In MybatisPlusConfig configuration data source, pagination plug-ins, transaction management, SQL execution performance analysis

Configuring MapperScan scanning dao / mapper Interface

 

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 PM 0018 17:09
* /
@Configuration
@MapperScan ( "com.lee.springboot.mapper")
public MybatisPlusConfig class {
/ ***
* PLUS performance optimization
* @ return
* /
@Bean
public PerformanceInterceptor performanceInterceptor () {
; PerformanceInterceptor performanceInterceptor new new PerformanceInterceptor = ()
the SQL execution performance analysis, development environment, the line is not recommended - / * <!. maxTime refers to a maximum length when executed sql -> * /
performanceInterceptor.setMaxTime (1000);
/ * <- if the SQL format default to false -> * /!
performanceInterceptor.setFormat (to true);
return performanceInterceptor;
}

/ * *
* @Description: mybatis-plus tab widget
* /
@Bean
public PaginationInterceptor paginationInterceptor () {
return new new PaginationInterceptor ();
}

// configuration data source
@Bean (name = "the dataSource")
@ConfigurationProperties (prefix = "spring.datasource" )
public the dataSource the DataSource () {
return new new DruidDataSource ();
}

// configuration Manager things
@Bean (name = "the transactionManager")
public DataSourceTransactionManager the transactionManager () {
return new new DataSourceTransactionManager (the dataSource ());
}

}

arranged 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


Done more than you can do a simple testing.
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);
}

}

 Original Address: https://blog.csdn.net/zhouzhiwengang/article/details/81059086

Guess you like

Origin www.cnblogs.com/lideqiang0909/p/11544511.html