Spring-boot整合mybatis-plus

pom文件需要添加的依赖:

  
        com.baomidou 
        mybatis-plus-boot-starter
        2.3 
    
   
   
      org.apache.velocity 
      velocity-engine-core 
      2.0 
   

xml文件放在了resource根目录下
application.properties配置文件添加xml和实体类扫描:

mybatis-plus.mapper-locations=classpath:/mybatis/*.xml 
mybatis-plus.typeAliasesPackage=cn.com.polycis.entity 
//数据库字段下划线转驼峰 
mybatis-plus.configuration.map-underscore-to-camel-case=true

MybatisPlusConfig配置文件:

import com.baomidou.mybatisplus.plugins.PaginationInterceptor; 
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; 
@Configuration 
//扫描dao或者是Mapper接口
@MapperScan("cn.com.polycis.mapper*") 
public class MybatisPlusConfig { 
       /** * mybatis-plus 分页插件 */ 
      @Bean 
      public PaginationInterceptor paginationInterceptor() { 
            PaginationInterceptor page = new PaginationInterceptor(); 
            page.setLocalPage(true);
           // 开启 PageHelper 的支持
           page.setDialectType("mysql"); 
           return page;
       } 
  } 

启动类需要添加mapper扫描注解:

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 
import org.springframework.context.annotation.PropertySource; 
@PropertySource("application.properties") 
@MapperScan("cn.com.polycis.mapper") 
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class}) 
public class OtaApplication { 
         public static void main(String[] args) {
                  SpringApplication.run(OtaApplication.class, args);
        } 
 }

代码生成器文件

package cn.com.polycis; 
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.DbType; 
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; 
import org.junit.Test; 
import java.util.Collections; import java.util.HashMap; 
import java.util.Map;
/**
 * 

* 测试生成代码 *

* * @author * @date 2018/11/28 */ public class GeneratorServiceEntity { @Test public void generateCode() { String packageName = "cn.com.polycis"; boolean serviceNameStartWithI = true; //user -> UserService, 设置成true: user -> IUserService generateByTables(serviceNameStartWithI, packageName, "lo_app_info", "lo_device_info"); } private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) { GlobalConfig config = new GlobalConfig(); String dbUrl = "jdbc:mysql://xxxxx:3306/lora_level2"; DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig.setUrl(dbUrl); dataSourceConfig.setDbType(DbType.MYSQL); dataSourceConfig.setUsername("xxxx"); dataSourceConfig.setPassword("xxxxx"); dataSourceConfig.setDriverName("com.mysql.jdbc.Driver"); StrategyConfig strategyConfig = new StrategyConfig(); strategyConfig.setCapitalMode(true); ///全局大写命名 strategyConfig.setEntityLombokModel(false); // 【实体】是否为lombok模型(默认 false) strategyConfig.setDbColumnUnderline(true); //数据库表字段采用下划线的 strategyConfig.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略 strategyConfig.setInclude(tableNames);//修改替换成你需要的表名,多个表名传数组 strategyConfig.setTablePrefix(new String[]{"lo_", ""});// 此处可以修改为您的表前缀 // strategyConfig.entityTableFieldAnnotationEnable(true);//生成实体类属性的对应数据库字段注解 config.setActiveRecord(false); config.setOutputDir("D:\\ttttt\\lorawan-ota\\src\\main\\java"); config.setFileOverride(true); if (!serviceNameStartWithI) { config.setServiceName("%sService"); } new AutoGenerator().setGlobalConfig(config) .setDataSource(dataSourceConfig) .setStrategy(strategyConfig) .setPackageInfo( new PackageConfig() . .setParent(packageName) .setController("controller") .setEntity("entity") ) .setCfg( // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值 new InjectionConfig() { @Override public void initMap() { Map map = new HashMap<>(); map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp"); this.setMap(map); } }.setFileOutConfigList(Collections.singletonList(new FileOutConfig("/templates/mapper.xml.vm") { // 自定义输出文件目录 @Override public String outputFile(TableInfo tableInfo) { return "D:/ttttt/lorawan-ota"+"/src/main/resources/mybatis/" + tableInfo.getEntityName() + "Mapper.xml"; } })) ).execute(); } private void generateByTables(String packageName, String... tableNames) { generateByTables(true, packageName, tableNames); } }

至此mybatis-plus整合完成

你可能感兴趣的:(spring)