springboot、mybatisplus框架整合搭建

springboot、mybatisplus框架整合搭建

目录结构
springboot、mybatisplus框架整合搭建_第1张图片

  1. 通过https://start.spring.io创建spingboot项目并导入idea
  2. maven添加mybatisplus、spring、mysql依赖
 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.7.1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.0.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.23</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>8.0.13</version>
        </dependency>
  1. application.yml配置mybatisplus配置和数据库配置
#MybatisPlus配置
mybatis-plus:
  mapper-locations: classpath*:/mapping/**/*.xml
  type-aliases-package: com.love.entity
  type-aliases-super-type: java.lang.Object
server:
  port: 8081
##数据库url
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/****?characterEncoding=utf8&useSSL=false
    username: ***
    password: ***
    driver-class-name: com.mysql.cj.jdbc.Driver
  1. 主函数配置注解
@SpringBootApplication(scanBasePackages = {"com.dkd.*"})
@EnableCaching
@MapperScan("com.dkd.mapper")// 指定mapper
public class BootShiroApplication {

	public static void main(String[] args) {
		SpringApplication.run(BootShiroApplication.class, args);
		System.out.println("success!");
	}

}
  1. mybatisplus逆向工程

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.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 专用代码生成器
 *
 * @author lizhen
 */
@SuppressWarnings("ALL")
public class CodeGenerator {

    private static final String GENERATOR_PATH = "/";

    /**
     * 

* 读取控制台内容 *

*/
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) { System.out.println("Prepaid MyBatisPlus Generator Application Welcome You"); // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 项目路径 final String projectPath = System.getProperty("user.dir"); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); /*//ORACLE dsc.setUrl("jdbc:oracle:thin:@xx.x.x.x:1521:lvs2"); // dsc.setSchemaName("public"); dsc.setDriverName("oracle.jdbc.driver.OracleDriver"); dsc.setUsername("prepaid_test"); dsc.setPassword("prepaid_test");*/ //MYSQL dsc.setUrl("jdbc:mysql://127.0.0.1:3307/***?characterEncoding=utf8&useSSL=false"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("***"); dsc.setPassword("***"); mpg.setDataSource(dsc); // 包配置 // PackageConfig pc = new PackageConfig(); // pc.setModuleName(scanner("模块名")); // pc.setParent("com.love."); // mpg.setPackageInfo(pc); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; mpg.setGlobalConfig(new GlobalConfig() .setOutputDir(projectPath + GENERATOR_PATH + "/src/main/java/com/love/zz_generator/src/main/java/com/love")//输出目录 .setFileOverride(true)// 是否覆盖文件 .setActiveRecord(true)// 开启 activeRecord 模式 .setEnableCache(false)// XML 二级缓存 .setBaseResultMap(true)// XML ResultMap .setBaseColumnList(true)// XML columList .setOpen(false)//生成后打开文件夹 .setAuthor(System.getenv().get("USERNAME") == null ? System.getenv().get("USER") : System.getenv().get("USERNAME"))//mac-USER win-USERNAME // 自定义文件命名,注意 %s 会自动填充表实体属性! .setMapperName("%sMapper") .setXmlName("%sMapper") .setServiceName("%sService") .setServiceImplName("%sServiceImpl") .setControllerName("%sController") ); // 如果模板引擎是 freemarker String templatePath = "/templates/mapper.xml.ftl"; // 如果模板引擎是 velocity // String templatePath = "/templates/mapper.xml.vm"; // 自定义输出配置 List<FileOutConfig> focList = new ArrayList<>(); // 自定义配置会被优先输出 focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { // 自定义输出文件名 return projectPath + GENERATOR_PATH + "/src/main/java/com/love/zz_generator/src/main/java/com/love/dao/mapper/" //+ pc.getModuleName() + "/" + tableInfo.getEntityName().replaceAll("Abstract","") + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); // 配置自定义输出模板 //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别 // templateConfig.setEntity("templates/entity2.java"); // templateConfig.setService(); // templateConfig.setController(); 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.baomidou.ant.common.BaseEntity"); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); // strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController"); strategy.setInclude(scanner("表名")); strategy.setSuperEntityColumns("id"); strategy.setControllerMappingHyphenStyle(true); // strategy.setTablePrefix(pc.getModuleName() + "_"); // strategy.setTablePrefix("tb_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }

你可能感兴趣的:(springboot、mybatisplus框架整合搭建)