欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于mybatis-plus-generator的簡(jiǎn)單使用示例詳解

 更新時(shí)間:2024年03月05日 10:58:45   作者:Spirit_NKlaus  
在springboot項(xiàng)目中集成mybatis-plus是很方便開(kāi)發(fā)的,最近看了一下plus的文檔,簡(jiǎn)單用一下它的代碼生成器,接下來(lái)通過(guò)實(shí)例代碼講解關(guān)于mybatis-plus-generator的簡(jiǎn)單使用,感興趣的朋友跟隨小編一起看看吧

在springboot項(xiàng)目中集成mybatis-plus是很方便開(kāi)發(fā)的,最近看了一下plus的文檔,簡(jiǎn)單用一下它的代碼生成器,首先在一個(gè)簡(jiǎn)單的springboot項(xiàng)目中加入如下依賴(lài)

<!-- 引入mybatis-plus依賴(lài) -->
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.1.2</version>
</dependency>
<!-- 引入mybatis-plus-generator依賴(lài) -->
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-generator</artifactId>
	<version>3.3.2</version>
</dependency>
<!-- 引入freemarker依賴(lài) -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- 引入mysql依賴(lài) -->
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>5.1.45</version>
	<scope>runtime</scope>
</dependency>
<!--lombok-->
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<optional>true</optional>
</dependency>

我這邊呢習(xí)慣把mapper文件放在java源碼路徑下,而不是放在默認(rèn)的resources目錄下,項(xiàng)目啟動(dòng)有導(dǎo)致mapper注入不了,所以還得在pom的<build></build>標(biāo)簽之中加入如下配置,并在啟動(dòng)類(lèi)上加上mapper掃描路徑

<resources>
	<resource>
		<directory>src/main/java</directory>
		<includes>
			<include>**/*.xml</include>
		</includes>
		<filtering>false</filtering>
	</resource>
	<resource>
		<directory>src/main/resources</directory>
		<includes>
			<include>**/*.properties</include>
			<include>**/*.xml</include>
			<include>**/*.ftl</include>
		</includes>
		<filtering>false</filtering>
	</resource>
</resources>
@SpringBootApplication
@MapperScan(basePackages = {"com.fengyun.mallmanage"})
public class MallManageSystemApplication {
	public static void main(String[] args) {
		SpringApplication.run(MallManageSystemApplication.class, args);
	}
}

基本依賴(lài)和配置結(jié)束了,我參考了plus的官方文檔,根據(jù)自己的需求稍微修改了一下它的生成器代碼,官網(wǎng)地址

代碼生成器 | MyBatis-Plus

/**
 * mybatis-plus代碼生成器,生成實(shí)體,mapper,mapper.xml,service,serviceImpl,controller
 * 演示例子,執(zhí)行 main 方法控制臺(tái)輸入表名回車(chē)自動(dòng)生成對(duì)應(yīng)項(xiàng)目目錄中(目錄要需要自行修改)
 */
public class CodeGenerator {
    /**
     * <p>
     * 讀取控制臺(tái)內(nèi)容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("請(qǐng)輸入" + tip + ":");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("請(qǐng)輸入正確的" + 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("YuanXing");
        //是否打開(kāi)輸出的目錄,默認(rèn)true
        gc.setOpen(false);
        //覆蓋已有的文件,默認(rèn)false(第一次生成時(shí)放開(kāi))
//        gc.setFileOverride(true);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        // 設(shè)置日期類(lèi)型為Date(若不設(shè)置時(shí)間類(lèi)型都會(huì)變成LocalDateTime部分連接池例如druid是無(wú)法識(shí)別的)
        gc.setDateType(DateType.ONLY_DATE);
        mpg.setGlobalConfig(gc);
        // 數(shù)據(jù)源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/malldata-dev?useUnicode=true&characterEncoding=UTF-8&useSSL=false");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("密碼");
        mpg.setDataSource(dsc);
        // 包配置
        PackageConfig pc = new PackageConfig();
//        pc.setModuleName(scanner("模塊名"));
        pc.setParent("com.fengyun.mallmanage");
        //自定義實(shí)體包名(不同的模塊自己手動(dòng)修改)
        pc.setEntity("mapper.goods.entity");
        //自定義mapper包名(不同的模塊自己手動(dòng)修改)
        pc.setMapper("mapper.goods");
        //自定義mapper.xml包名(不同的模塊自己手動(dòng)修改)
        pc.setXml("mapper.goods");
        //自定義service包名(不同的模塊自己手動(dòng)修改)
        pc.setService("service.goods");
        //自定義serviceImpl包名(不同的模塊自己手動(dòng)修改)
        pc.setServiceImpl("service.goods.impl");
        //自定義controller包名(不同的模塊自己手動(dòng)修改)
        pc.setController("controller.goods");
        mpg.setPackageInfo(pc);
        // 自定義配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        // 如果模板引擎是 freemarker
        String xmlPath = "/templates/mapper.xml.ftl";
        // 自定義輸出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定義配置會(huì)被優(yōu)先輸出
        focList.add(new FileOutConfig(xmlPath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定義輸出文件名 , 如果你 Entity 設(shè)置了前后綴、此處注意 xml 的名稱(chēng)會(huì)跟著發(fā)生變化??!
                return projectPath + "/src/main/java/com/fengyun/mallmanage/mapper/goods"
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        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);
        //是否為lombok模型,默認(rèn)為false
        strategy.setEntityLombokModel(true);
        //前后端分離時(shí)可開(kāi)啟
//        strategy.setRestControllerStyle(true);
        strategy.setInclude(scanner("表名,多個(gè)英文逗號(hào)分割").split(","));
        //RequestMapping駝峰轉(zhuǎn)連字符
//        strategy.setControllerMappingHyphenStyle(true);
        //生成實(shí)體時(shí)生成生成數(shù)據(jù)庫(kù)字段注解
        strategy.setEntityTableFieldAnnotationEnable(true);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}

然后運(yùn)行即可

需要注意的是由于我在代碼生成器代碼中包配置的時(shí)候注釋掉了輸入模塊名(即這一段pc.setModuleName(scanner("模塊名"));),所以會(huì)導(dǎo)致controller中的RequestMapping的路徑有兩個(gè)//表名的駝峰命名,假設(shè)輸入模塊名的話RequestMapping的路徑就會(huì)是/模塊名/表名的駝峰命名,但是這樣的話生成的類(lèi)的包就不是我現(xiàn)在這樣的了,這個(gè)看自己的需求吧,具體可以看一下生成包的源碼

更多Java內(nèi)容,請(qǐng)點(diǎn)擊下方名片。

到此這篇關(guān)于關(guān)于mybatis-plus-generator的簡(jiǎn)單使用的文章就介紹到這了,更多相關(guān)mybatis-plus-generator使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論