溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Mybatis-Plus全局配置無效怎么解決

發(fā)布時間:2022-01-12 18:08:57 來源:億速云 閱讀:825 作者:iii 欄目:開發(fā)技術

這篇“Mybatis-Plus全局配置無效怎么解決”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Mybatis-Plus全局配置無效怎么解決”文章吧。

全局配置無效

依賴

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

配置文件修改

mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  #實體掃描,多個package用逗號或者分號分隔
  typeAliasesPackage: com.hz.waste.entity.model
  global-config:
     #id-type: 3    #這種配置是不生效的
     #field-strategy: 2 #這種配置是不生效的
    db-config:
      #主鍵類型 AUTO:"數(shù)據(jù)庫ID自增" INPUT:"用戶輸入ID",ID_WORKER:"全局唯一ID (數(shù)字類型唯一ID)", UUID:"全局唯一ID UUID";
      id-type: ID_WORKER  #改為這種可以
      #字段策略 IGNORED:"忽略判斷"  NOT_NULL:"非 NULL 判斷")  NOT_EMPTY:"非空判斷"
      field-strategy: NOT_EMPTY #改為這種可以
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false
    #配置JdbcTypeForNull
    jdbc-type-for-null: 'null'
    call-setters-on-nulls: true
    #打印語句
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

Mybatis-plus簡單配置及應用

mybatis-plus是由中國大神寫的mybatis增強版,可以自動生成代碼。

配置過程比較簡單。首先引入兩個maven依賴

    <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>

一個是mybatis-plus,另一個是它自動成成代碼所依賴的模板引擎velocity。

然后將mybatis的sqlSeassionFactoryBean替換成plus增強版的,插件可以選擇性配置

    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property value="classpath:/mybatis-config.xml" name="configLocation" />
        <!-- 自動掃描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
        <!-- MP 全局配置注入 -->
        <property name="globalConfig" ref="globalConfig" />
        <!-- 插件配置 -->
        <property name="plugins">
            <array>
                <bean class="com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor" />
            </array>
        </property>
    </bean>

mybatis-plus全局配置,主要是配置id生成策略,依賴數(shù)據(jù)庫類型,

<bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
        <!-- 主鍵策略配置 -->
        <!-- 可選參數(shù) AUTO->`0`("數(shù)據(jù)庫ID自增") INPUT->`1`(用戶輸入ID") ID_WORKER->`2`("全局唯一ID") 
            UUID->`3`("全局唯一ID") -->
        <property name="idType" value="2" />
        <!-- 數(shù)據(jù)庫類型配置 -->
        <property name="dbType" value="mysql" />
        <!-- 全局表為下劃線命名設置 true -->
        <property name="dbColumnUnderline" value="true" />
    </bean>

生成代碼

public class MpGenerator {
    /**
     * <p>
     * MySQL 生成演示
     * </p>
     */
    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置\\Begin\\src\\main\\java
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir("G:\\workspace");
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        gc.setEnableCache(false);// XML 二級緩存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(true);// XML columList
        gc.setOpen(false);
        gc.setAuthor("XuWei");
        // 自定義文件命名,注意 %s 會自動填充表實體屬性!
        gc.setMapperName("%sDao");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setControllerName("%sController");
        mpg.setGlobalConfig(gc);
        // 數(shù)據(jù)源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUrl("jdbc:mysql://localhost:3306/begin?useUnicode=true&amp;characterEncoding=UTF-8&amp;generateSimpleParameterMetadata=true");
        dsc.setUsername("root");
        dsc.setPassword("123");
        mpg.setDataSource(dsc);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // strategy.setCapitalMode(true);// 全局大寫命名 ORACLE 注意
        strategy.setTablePrefix(new String[] { "t_", "tsys_" });// 此處可以修改為您的表前綴
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setInclude(new String[] { "dept" }); // 需要生成的表
        // strategy.setExclude(new String[]{"test"}); // 排除生成的表
        mpg.setStrategy(strategy);
        //默認是service、serviceImpl、controller都生成。在這里關閉他們
        TemplateConfig tc = new TemplateConfig();
        tc.setController(null);
        mpg.setTemplate(tc);
        // 生成文件路徑
//      PackageConfig pc = new PackageConfig();
//      pc.setParent("com.xu");
//      pc.setEntity("entity.plus");
//      pc.setMapper("dao.plus");
//      pc.setXml("mapper.plus");
//      pc.setService("service.plus");
//      pc.setServiceImpl("service.plus.impl");
//      mpg.setPackageInfo(pc);
        // 執(zhí)行生成
        mpg.execute();
    }

這樣代碼生成到G:\workspace目錄下面

和mybayis generator相比plus生成的代碼映射文件xml,和dao層更加干凈,通用的CRUD都通過dao類繼承的BaseMapper來實現(xiàn)。

但是缺點也很明顯,條件構(gòu)造器不能像generator那樣直接將表中的字段名稱和pojo映射,所以需要自己寫查詢條件對應的字段名稱。

如果要拼接這樣一個查詢條件( user_name = ? and password = ? ) or( id = ? and state = ? )

mybatis-plus條件構(gòu)造

        EntityWrapper<User> ew = new EntityWrapper<>();
        ew.eq("user_name", "向問天").eq("password", "sde");
        ew.orNew("id", 3).eq("state", 2);

mybatis generator條件構(gòu)造

        UserExample userExample = new UserExample();
        userExample.createCriteria()
        .andUserNameEqualTo("向問天")
        .andPasswordEqualTo("sde");
        userExample.or()
        .andIdEqualTo(3)
        .andStateEqualTo(2);
        userExample.isDistinct();

以上就是關于“Mybatis-Plus全局配置無效怎么解決”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關的知識內(nèi)容,請關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI