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

Mybatis-Plus 全局配置無效的解決方案

 更新時(shí)間:2022年01月12日 15:32:57   作者:Fan YC  
這篇文章主要介紹了Mybatis-Plus 全局配置無效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

全局配置無效

依賴

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

配置文件修改

mybatis-plus:
? mapper-locations: classpath:mapper/*.xml
? #實(shí)體掃描,多個(gè)package用逗號(hào)或者分號(hào)分隔
? 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簡單配置及應(yīng)用

mybatis-plus是由中國大神寫的mybatis增強(qiáng)版,可以自動(dòng)生成代碼。

配置過程比較簡單。首先引入兩個(gè)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>

一個(gè)是mybatis-plus,另一個(gè)是它自動(dòng)成成代碼所依賴的模板引擎velocity。

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

? ? <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
? ? ? ? <property name="dataSource" ref="dataSource" />
? ? ? ? <property value="classpath:/mybatis-config.xml" name="configLocation" />
? ? ? ? <!-- 自動(dòng)掃描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" />
? ? ? ? <!-- 全局表為下劃線命名設(shè)置 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 二級(jí)緩存
? ? ? ? gc.setBaseResultMap(true);// XML ResultMap
? ? ? ? gc.setBaseColumnList(true);// XML columList
? ? ? ? gc.setOpen(false);
? ? ? ? gc.setAuthor("XuWei");
? ? ? ? // 自定義文件命名,注意 %s 會(huì)自動(dòng)填充表實(shí)體屬性!
? ? ? ? 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);
? ? ? ? //默認(rèn)是service、serviceImpl、controller都生成。在這里關(guān)閉他們
? ? ? ? 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來實(shí)現(xiàn)。

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

如果要拼接這樣一個(gè)查詢條件( 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();

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論