Mybatis plus

引入maven依賴

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.1</version>
</dependency>

<!--使用mybatis-plus-generator自動(dòng)生成代碼-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.4.1</version>
    <scope>test</scope>
</dependency>

自動(dòng)生成代碼

public class MybatisPlusGenerateTest {
    @Test
    public void autoGenerate() {
        // Step1:代碼生成器
        AutoGenerator mpg = new AutoGenerator();

        // Step2:全局配置
        GlobalConfig gc = new GlobalConfig();
        // 填寫代碼生成的目錄(需要修改)
        String projectPath = "D:\\project\\xxx";
        // 拼接出代碼最終輸出的目錄
        gc.setOutputDir(projectPath + "/src/main/java");
        // 配置開發(fā)者信息(可選)(需要修改)
        gc.setAuthor("xxx");
        // 配置是否打開目錄,false 為不打開(可選)
        gc.setOpen(false);
        // 實(shí)體屬性 Swagger2 注解,添加 Swagger 依賴,開啟 Swagger2 模式(可選)
        //gc.setSwagger2(true);
        // 重新生成文件時(shí)是否覆蓋,false 表示不覆蓋(可選)
        gc.setFileOverride(false);
        gc.setBaseResultMap(true);//XML ResultMap
        gc.setBaseColumnList(true);//XML columList  在xml中生產(chǎn)基礎(chǔ)列
        // 配置主鍵生成策略,此處為 ASSIGN_ID(可選)
        gc.setIdType(IdType.AUTO);
        // 配置日期類型,此處為 ONLY_DATE(可選)
        gc.setDateType(DateType.ONLY_DATE);
        // 默認(rèn)生成的 service 會(huì)有 I 前綴
        gc.setServiceName("%sService");
        mpg.setGlobalConfig(gc);

        //模板配置,可以配置不生成xml等
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setController(null);
        //templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // Step3:數(shù)據(jù)源配置(需要修改)
        DataSourceConfig dsc = new DataSourceConfig();
        // 配置數(shù)據(jù)庫 url 地址
        dsc.setUrl("jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=utf8");
        // dsc.setSchemaName("testMyBatisPlus"); // 可以直接在 url 中指定數(shù)據(jù)庫名
        // 配置數(shù)據(jù)庫驅(qū)動(dòng)
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        // 配置數(shù)據(jù)庫連接用戶名
        dsc.setUsername("clcmopr");
        // 配置數(shù)據(jù)庫連接密碼
        dsc.setPassword("TQEf2HBau38n");
        mpg.setDataSource(dsc);

        // Step:4:包配置
        PackageConfig pc = new PackageConfig();
        // 配置父包名(需要修改)
        pc.setParent("com.xxx.dao");
        // 配置模塊名(需要修改)
        pc.setModuleName("");
        // 配置 entity 包名
        pc.setEntity("entity");
        // 配置 mapper 包名
        pc.setMapper("mapper");
        // 配置 service 包名
        pc.setService("service");
        // 配置 controller 包名
        pc.setController("controller");
        pc.setXml("xml");
        mpg.setPackageInfo(pc);

        // Step5:策略配置(數(shù)據(jù)庫表配置)
        StrategyConfig strategy = new StrategyConfig();
        // 指定表名(可以同時(shí)操作多個(gè)表,使用 , 隔開)(需要修改)
        strategy.setInclude("t_travel_node");
        // 配置數(shù)據(jù)表與實(shí)體類名之間映射的策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        // 配置數(shù)據(jù)表的字段與實(shí)體類的屬性名之間映射的策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // 配置 lombok 模式
        strategy.setEntityLombokModel(true);
        // 配置 rest 風(fēng)格的控制器(@RestController)
        strategy.setRestControllerStyle(true);
        // 配置駝峰轉(zhuǎn)連字符
        strategy.setControllerMappingHyphenStyle(true);
        // 配置表前綴,生成實(shí)體時(shí)去除表前綴
        // 此處的表名為 test_mybatis_plus_user,模塊名為 test_mybatis_plus,去除前綴后剩下為 user。
        strategy.setTablePrefix("t_");
        mpg.setStrategy(strategy);

        // Step6:執(zhí)行代碼生成操作
        mpg.execute();
    }
}

生成代碼

entity類:

@Data
@EqualsAndHashCode(callSuper = false)
@TableName("t_cust_travel")
public class CustTravel implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * ID
     */
    @TableId(value = "ID", type = IdType.AUTO)
    private Integer id;

    /**
     * 編號(hào)
     */
    @TableField("CODE")
    private String code;
}

mapper類:

public interface CustTravelMapper extends BaseMapper<CustTravel> {

}

sevice類:

public interface CustTravelService extends IService<CustTravel> {

}

分頁插件

/**
 * 分頁插件
 * @return
 */
@Bean
public MybatisPlusInterceptor paginationInterceptor() {
    MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
    mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
    return mybatisPlusInterceptor;
}

參考

https://www.cnblogs.com/l-y-h/p/12859477.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容