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

關于mybatis-plus-generator的簡單使用示例詳解

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

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

<!-- 引入mybatis-plus依賴 -->
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.1.2</version>
</dependency>
<!-- 引入mybatis-plus-generator依賴 -->
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-generator</artifactId>
	<version>3.3.2</version>
</dependency>
<!-- 引入freemarker依賴 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- 引入mysql依賴 -->
<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>

我這邊呢習慣把mapper文件放在java源碼路徑下,而不是放在默認的resources目錄下,項目啟動有導致mapper注入不了,所以還得在pom的<build></build>標簽之中加入如下配置,并在啟動類上加上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);
	}
}

基本依賴和配置結束了,我參考了plus的官方文檔,根據自己的需求稍微修改了一下它的生成器代碼,官網地址

代碼生成器 | MyBatis-Plus

/**
 * mybatis-plus代碼生成器,生成實體,mapper,mapper.xml,service,serviceImpl,controller
 * 演示例子,執(zhí)行 main 方法控制臺輸入表名回車自動生成對應項目目錄中(目錄要需要自行修改)
 */
public class CodeGenerator {
    /**
     * <p>
     * 讀取控制臺內容
     * </p>
     */
    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) {
        // 代碼生成器
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("YuanXing");
        //是否打開輸出的目錄,默認true
        gc.setOpen(false);
        //覆蓋已有的文件,默認false(第一次生成時放開)
//        gc.setFileOverride(true);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(true);
        // 設置日期類型為Date(若不設置時間類型都會變成LocalDateTime部分連接池例如druid是無法識別的)
        gc.setDateType(DateType.ONLY_DATE);
        mpg.setGlobalConfig(gc);
        // 數據源配置
        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");
        //自定義實體包名(不同的模塊自己手動修改)
        pc.setEntity("mapper.goods.entity");
        //自定義mapper包名(不同的模塊自己手動修改)
        pc.setMapper("mapper.goods");
        //自定義mapper.xml包名(不同的模塊自己手動修改)
        pc.setXml("mapper.goods");
        //自定義service包名(不同的模塊自己手動修改)
        pc.setService("service.goods");
        //自定義serviceImpl包名(不同的模塊自己手動修改)
        pc.setServiceImpl("service.goods.impl");
        //自定義controller包名(不同的模塊自己手動修改)
        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<>();
        // 自定義配置會被優(yōu)先輸出
        focList.add(new FileOutConfig(xmlPath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定義輸出文件名 , 如果你 Entity 設置了前后綴、此處注意 xml 的名稱會跟著發(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模型,默認為false
        strategy.setEntityLombokModel(true);
        //前后端分離時可開啟
//        strategy.setRestControllerStyle(true);
        strategy.setInclude(scanner("表名,多個英文逗號分割").split(","));
        //RequestMapping駝峰轉連字符
//        strategy.setControllerMappingHyphenStyle(true);
        //生成實體時生成生成數據庫字段注解
        strategy.setEntityTableFieldAnnotationEnable(true);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}

然后運行即可

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

更多Java內容,請點擊下方名片。

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

相關文章

  • 詳解在Spring中如何使用AspectJ來實現AOP

    詳解在Spring中如何使用AspectJ來實現AOP

    這篇文章主要介紹了詳解在Spring中如何使用AspectJ來實現AOP,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • 解決子線程中獲取不到HttpServletRequest對象的問題

    解決子線程中獲取不到HttpServletRequest對象的問題

    這篇文章主要介紹了解決子線程中獲取不到HttpServletRequest對象的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • shiro實現單點登錄(一個用戶同一時刻只能在一個地方登錄)

    shiro實現單點登錄(一個用戶同一時刻只能在一個地方登錄)

    這篇文章主要介紹了shiro實現單點登錄(一個用戶同一時刻只能在一個地方登錄)的相關資料,非常不錯,具有參考借鑒價值,感興趣的朋友一起學習吧
    2016-08-08
  • WebDriver中實現對特定的Web區(qū)域截圖方法

    WebDriver中實現對特定的Web區(qū)域截圖方法

    這篇文章主要介紹了WebDriver中實現對特定的Web區(qū)域截圖方法,本文直接給出實現代碼,需要的朋友可以參考下
    2015-06-06
  • 在springboot中使用攔截器的步驟詳解

    在springboot中使用攔截器的步驟詳解

    攔截器Interceptor,是SpringMVC中的核心內容,在SpringBoot中使用Interceptor,同時采用全注解開發(fā),這篇文章主要介紹了在springboot中使用攔截器的步驟,需要的朋友可以參考下
    2022-01-01
  • spring aop實現接口超時處理組件的代碼詳解

    spring aop實現接口超時處理組件的代碼詳解

    這篇文章給大家介紹了spring aop實現接口超時處理組件,文中有詳細的實現思路,并通過代碼示例給大家介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-02-02
  • SpringBoot升級3.2報錯Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.String的解決方案

    SpringBoot升級3.2報錯Invalid value type for 

    這篇文章給大家介紹了SpringBoot升級3.2報錯Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.String的解決方案,文中有詳細的原因分析,需要的朋友可以參考下
    2023-12-12
  • MyBatis批量插入的五種方式小結(MyBatis以集合方式批量新增)

    MyBatis批量插入的五種方式小結(MyBatis以集合方式批量新增)

    本文主要介紹了MyBatis批量插入的五種方式小結(MyBatis以集合方式批量新增),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-01-01
  • 詳解Maven profile配置管理及激活profile的幾種方式

    詳解Maven profile配置管理及激活profile的幾種方式

    這篇文章主要介紹了詳解Maven profile配置管理及激活profile的幾種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • SpringBoot配置攔截器實現過程詳解

    SpringBoot配置攔截器實現過程詳解

    在系統中經常需要在處理用戶請求之前和之后執(zhí)行一些行為,例如檢測用戶的權限,或者將請求的信息記錄到日志中,即平時所說的"權限檢測"及"日志記錄",下面這篇文章主要給大家介紹了關于在SpringBoot項目中整合攔截器的相關資料,需要的朋友可以參考下
    2022-10-10

最新評論