1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        Java代碼生成

        共 3391字,需瀏覽 7分鐘

         ·

        2021-02-22 06:35

        ? ? ? ?在很多開源的后臺管理系統(tǒng)當(dāng)中都有代碼生成的工具,幫助開發(fā)者完成通用代碼的生成,比如生成 Controller、Service、Dao 和 XML 文件等,能夠幫助開發(fā)者生成通用的CRUD 代碼。還能幫助開發(fā)者生成統(tǒng)一的后臺列表頁面和表單頁面,以方便開發(fā)者可以快速的完成自己業(yè)務(wù)的開發(fā),而忽略那些重復(fù)而又繁瑣的工作。其實(shí)在開發(fā)框架中也有一些簡單的代碼生成的工具,比如接下來要介紹的 mybatis-plus 提供的代碼生成。


        MyBatis-Plus 代碼生成器的介紹

        ? ? ? ? 引用 MyBatis-Plus 官網(wǎng)的說明,如下:

        AutoGenerator 是 MyBatis-Plus 的代碼生成器,通過 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各個模塊的代碼,極大的提升了開發(fā)效率。

        https://mp.baomidou.com/guide/generator.html#使用教程


        引入依賴

        ? ? ? ?MyBatis-Plus 官網(wǎng)有較為詳細(xì)的說明,不過對于剛開始接觸的話完全可以通過拿來現(xiàn)成的代碼使用。

        ? ? ? ? 使用 SpringBoot 來測試一下 Mybatis-Plus 代碼生成的功能。


        ? ? ? ? SpringBoot 項(xiàng)目中使用代碼生成器需要引入一些依賴,依賴如下:

            mysql    mysql-connector-java    runtime    org.projectlombok    lombok    true    com.baomidou    mybatis-plus-boot-starter    3.0.5    org.apache.velocity    velocity-engine-core    2.0    com.baomidou    mybatis-plus-generator    3.4.1


        生成代碼

        ? ? ? ? 引入上面的依賴以后,下面的代碼其實(shí)進(jìn)行簡單的修改就可以完成自己的代碼生成,非常的方便。

        public class CodeGenerator {    public static void main(String[] args) {
        // 構(gòu)建一個代碼生成對象 AutoGenerator mpg = new AutoGenerator();
        // 1. 全局配置 GlobalConfig gc = new GlobalConfig();
        String separator = File.separator;????????gc.setOutputDir("/Users/xxx/Documents/demo/src/main/java"); gc.setAuthor("碼農(nóng)UP2U"); gc.setOpen(false);//打開目錄 gc.setFileOverride(true);//是否覆蓋 gc.setServiceName("%sService");//去Service的I前綴。 gc.setIdType(IdType.ID_WORKER); gc.setDateType(DateType.ONLY_DATE); gc.setSwagger2(false);
        mpg.setGlobalConfig(gc);
        DataSourceConfig dsc = new DataSourceConfig();????????dsc.setUrl("jdbc:mysql://ip:port/data?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("password"); dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);
        // 包設(shè)置 PackageConfig pc = new PackageConfig();
        pc.setParent("com.example.demo"); pc.setEntity("entity"); pc.setMapper("mapper"); pc.setController("controller");
        mpg.setPackageInfo(pc);
        // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setInclude("ums_member");//表名 strategy.setNaming(NamingStrategy.underline_to_camel);// 下劃線轉(zhuǎn)他駝峰 strategy.setColumnNaming(NamingStrategy.underline_to_camel);// 列 下劃線轉(zhuǎn)脫發(fā) strategy.setEntityLombokModel(true);//lombok 開啟 strategy.setLogicDeleteFieldName("deleted");
        // 自動填充 TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT); TableFill gmtModify = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE); ArrayList tableFills = new ArrayList<>(); tableFills.add(gmtCreate); tableFills.add(gmtModify);
        strategy.setTableFillList(tableFills); // restcontroller strategy.setRestControllerStyle(true); strategy.setControllerMappingHyphenStyle(true);// localhost:xxx/hello_2
        mpg.setStrategy(strategy);
        mpg.execute(); }}

        ? ? ? ? 上面的代碼中,設(shè)置生成文件的路徑,設(shè)置數(shù)據(jù)庫的連接,設(shè)置包名,設(shè)置表相關(guān)的內(nèi)容等。代碼簡單的易懂,就不做過多說明。


        生成結(jié)果

        ? ? ? ? 運(yùn)行上面的代碼,來看一下生成的內(nèi)容,如下圖:

        ? ? ? ?從圖中可以看出,生成的內(nèi)容有 controller、entity、dao 和 service?;旧鲜∪チ宋覀円恍┦謩有枰瓿傻墓ぷ?,還是比較方便的。代碼有一些部分是通用的,一部分是可能是自定義的,可以將整個代碼進(jìn)行封裝,使得使用更加的方便的。


        瀏覽 67
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            在线观看中文字幕一区| 在线观看操逼| 91亚洲精品久久久久久久久久久久 | 青青久久91| 水蜜桃一区二区三区| а天堂中文在线资源| 国产日韩欧美| 成人精品秘久久久按摩下载| 中文字幕日本在线| 淫荡少妇美红久久久久久久久久 |