記錄是為了更好的成長!
mybatisPlus生成工具,這里以spgingBoot構建項目說明
1、引入jar
(如果使用 https://start.spring.io/ 構建springBoot項目只需選 web 和 mysql即可,然后加入下面的依賴到pom文件中)
<!-- 阿里巴巴druid數據庫連接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!-- mybatisplus與springboot整合 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.1</version> </dependency>
2、引入一個工具類
package com.share.common.utils; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.ClassUtils; /** * @Description:通用工具類 */ public class PropertiesUtil { private static Logger log = LoggerFactory.getLogger(PropertiesUtil.class); /** * @Description: 獲取根路徑下的properties文件 * @param fileName * @return * @throws FileNotFoundException Properties */ public static Properties getProperties(String fileName) throws FileNotFoundException { String rootPath = ClassUtils.getDefaultClassLoader().getResource("").getPath().substring(1); String filePath = rootPath + fileName; log.info("加載的properties屬性文件是:" + filePath); Properties properties = new Properties(); // 讀取屬性文件a.properties InputStream in = new BufferedInputStream(new FileInputStream(filePath)); try { properties.load(in); } catch (IOException e) { log.info("讀取文件失敗"); } return properties; } }
3、生成代碼的java文件
public class MybatisplusGenerator { public static void main(String[] args) throws FileNotFoundException { //讀取配置文件 String fileName = "application-dev.properties"; Properties properties = PropertiesUtil.getProperties(fileName); //獲取DB連接信息 String drivername = (String) properties.get("spring.datasource.driverClassName"); String url = properties.getProperty("spring.datasource.url"); String username = properties.getProperty("spring.datasource.username"); String password = properties.getProperty("spring.datasource.password"); AutoGenerator mpg = new AutoGenerator(); /* * 配置路徑 */ String projectPath = System.getProperty("user.dir"); // 獲取項目路徑 String objPath = projectPath + "/src/main/java"; // 獲取java目錄 String parentPackage = "com.share.modules.biz"; // 配置包路徑 /* * 全局配置 */ GlobalConfig gc = new GlobalConfig(); gc.setOutputDir(objPath); // 配置路徑 gc.setOpen(false); // 是否打開生成的文件夾 gc.setAuthor("author"); // author /* 自定義文件命名,注意 %s 會自動填充表實體屬性! */ gc.setMapperName("%sMapper"); // 設置mapper接口后綴 gc.setServiceName("%sService"); // 設置Service接口后綴 gc.setServiceImplName("%sServiceImpl"); // 設置Service實現類后綴 gc.setControllerName("%sController"); // 設置controller類后綴 gc.setXmlName("%sMapper"); // 設置sql映射文件后綴 gc.setFileOverride(false); // 是否覆蓋同名文件,默認是false gc.setActiveRecord(false); // 不需要ActiveRecord特性的請改為false gc.setEnableCache(false); // XML 二級緩存 gc.setBaseResultMap(true); // XML ResultMap gc.setBaseColumnList(false); // XML columList mpg.setGlobalConfig(gc); /* * 數據源配置 */ DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL).setDriverName(drivername).setUrl(url).setUsername(username).setPassword(password); mpg.setDataSource(dsc); /* * 策略配置 */ StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel) // 表名生成策略 .setRestControllerStyle(true); // 設置controller自動加RestController注解 //.setInclude(new String[] {"rs_user"}); //修改替換成你需要的表名,多個表名傳數組,如果注釋掉就生成庫中所有的表 // .setTablePrefix(new String[] { "t_" }) // 此處可以修改為您的表前綴 ; mpg.setStrategy(strategy); // 包配置 PackageConfig packageConfig = new PackageConfig(); packageConfig.setParent(parentPackage).setController("controller") /* * .setService("service") //服務接口 .setServiceImpl("service.impl") //服務實現 * .setMapper("mapper") //dao層 .setXml("mapper") //dao層對應的xml文件 * .setEntity("entity") */; // pojo對象 mpg.setPackageInfo(packageConfig); // 自定義配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; // 自定義xml的存放路徑 List<FileOutConfig> focList = new ArrayList<>(); focList.add(new FileOutConfig("/templates/mapper.xml.vm") { @Override public String outputFile(TableInfo tableInfo) { // 自定義Mapper.xml文件存放的路徑 return projectPath + "/src/main/resources/mappers/" + tableInfo.getEntityName() + "-mapper.xml"; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置生成的資源模板 TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setController(null); //不生成controller templateConfig.setService(null); //不生成service templateConfig.setServiceImpl(null);//不生成service實現 templateConfig.setXml(null); // 關閉默認 xml 生成,調整生成 至 根目錄 mpg.setTemplate(templateConfig); // 執行生成 mpg.execute(); } }
4、運行java文件,刷新項目即可
5、啟動springBoot項目需要在啟動類加@MapperScan注解,並指定mapper的目錄,否則會報錯ibatis綁定異常
6、生成的項目並不一定滿足我們的需要,需要自定義sql查詢,直接在生成的mapper接口和mapper映射文件中追加即可
7、 我習慣生成entity和mapper, 自己寫service接口和實現類
注意: 如果需要自定義sql語句,就必須要在配置文件中指定 mybatis-plus.mapper-locations=classpath:mappers/*.xml 不能寫成 mybatis.mapper-locaitons=classpath:mappers/*.xml
github示例: https://github.com/kh5218/DemoForSpringBoot/tree/master/mybatisplus
以上內容代表個人觀點,僅供參考,不喜勿噴。。。