Mybatis-Plus
概要
MyBatis-Plus(略称MP)はMyBatisのエンハンスメントツールであり、MyBatisをベースにエンハンスメントのみを行い変更せず、開発を簡素化し、効率を向上させるために生まれた.
とくせい
インストール
springboot
com.baomidou
mybatis-plus-boot-starter
3.1.1
SpringMVC
com.baomidou
mybatis-plus
3.1.1
コード生成
entity service mapper
package com.lp.springbootmybatisplus;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CodeGenerator
{
/**
*
*/
private static final String PARENT_PATH = "com.lp";
/**
*
*/
private static final String AUTHOR = "lipeng";
/**
*
*
*
*/
public static String scanner(String tip)
{
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append(" " + tip + ":");
System.out.println(help.toString());
if(scanner.hasNext())
{
String ipt = scanner.next();
if(StringUtils.isNotEmpty(ipt))
{
return ipt;
}
}
throw new MybatisPlusException(" " + tip + "!");
}
public static void main(String[] args)
{
//
AutoGenerator mpg = new AutoGenerator();
//
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor(AUTHOR);
gc.setOpen(false);
gc.setEntityName("%s");
gc.setDateType(DateType.ONLY_DATE);
// gc.setSwagger2(true); Swagger2
mpg.setGlobalConfig(gc);
//
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
//
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner(" "));
pc.setParent(PARENT_PATH);
mpg.setPackageInfo(pc);
//
InjectionConfig cfg = new InjectionConfig()
{
@Override
public void initMap()
{
}
};
// freemarker
//String templatePath = "/templates/mapper.xml.ftl";
// velocity
String templatePath = "/templates/mapper.xml.vm";
//
List focList = new ArrayList<>();
//
focList.add(new FileOutConfig(templatePath)
{
@Override
public String outputFile(TableInfo tableInfo)
{
// , Entity 、 xml !!
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
//
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
//
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//strategy.setSuperEntityClass("com.lp.common.BaseEntity");
strategy.setEntityLombokModel(false);
strategy.setRestControllerStyle(false);
// strategy.setSuperControllerClass("com.lp.common.BaseController");
strategy.setInclude(scanner(" , ").split(","));
//strategy.setSuperEntityColumns("id");
strategy.setControllerMappingHyphenStyle(true);
//strategy.setEntityTableFieldAnnotationEnable(true);
strategy.setRestControllerStyle(false);
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
内蔵mapperメソッド
/**
* Mapper , mapper.xml , CRUD
* Mapper id
*
* @author hubin
* @since 2016-01-23
*/
public interface BaseMapper extends Mapper {
/**
*
*
* @param entity
*/
int insert(T entity);
/**
* ID
*
* @param id ID
*/
int deleteById(Serializable id);
/**
* columnMap ,
*
* @param columnMap map
*/
int deleteByMap(@Param(Constants.COLUMN_MAP) Map columnMap);
/**
* entity ,
*
* @param wrapper ( null)
*/
int delete(@Param(Constants.WRAPPER) Wrapper wrapper);
/**
* ( ID )
*
* @param idList ID ( null empty)
*/
int deleteBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList);
/**
* ID
*
* @param entity
*/
int updateById(@Param(Constants.ENTITY) T entity);
/**
* whereEntity ,
*
* @param entity (set , null)
* @param updateWrapper ( null, entity where )
*/
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper updateWrapper);
/**
* ID
*
* @param id ID
*/
T selectById(Serializable id);
/**
* ( ID )
*
* @param idList ID ( null empty)
*/
List selectBatchIds(@Param(Constants.COLLECTION) Collection extends Serializable> idList);
/**
* ( columnMap )
*
* @param columnMap map
*/
List selectByMap(@Param(Constants.COLUMN_MAP) Map columnMap);
/**
* entity ,
*
* @param queryWrapper ( null)
*/
T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);
/**
* Wrapper ,
*
* @param queryWrapper ( null)
*/
Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper);
/**
* entity ,
*
* @param queryWrapper ( null)
*/
List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper);
/**
* Wrapper ,
*
* @param queryWrapper ( null)
*/
List