Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of invalid global configuration of Mybatis-Plus

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Most people do not understand the knowledge points of this article "Mybatis-Plus global configuration invalid how to solve", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Mybatis-Plus global configuration invalid how to solve" article.

Invalid global configuration

Dependence

Com.baomidou mybatis-plus-boot-starter 3.1.0

Profile modification

Mybatis-plus: mapper-locations: classpath:mapper/*.xml # entity scan Multiple package are separated by commas or semicolons: typeAliasesPackage: com.hz.waste.entity.model global-config: # id-type: 3 # this configuration does not take effect # field-strategy: 2 # this configuration is not valid db-config: # primary key type AUTO: "Database ID self-increment" INPUT: "user input ID", ID_WORKER: "globally unique ID (number type unique ID)" UUID: "globally unique ID UUID" Id-type: ID_WORKER # can be changed to # field policy IGNORED: "ignore judgment" NOT_NULL: "non-NULL judgment") NOT_EMPTY: "non-empty judgment" field-strategy: NOT_EMPTY # can be changed to configuration: map-underscore-to-camel-case: true cache-enabled: false # configuration JdbcTypeForNull jdbc-type-for-null: 'null' call-setters -on-nulls: true # print statement log-impl: org.apache.ibatis.logging.stdout.StdOutImplMybatis-plus simple configuration and Application

Mybatis-plus is an enhanced version of mybatis written by the Chinese god, which can generate code automatically.

The configuration process is relatively simple. First introduce two maven dependencies

Com.baomidou mybatis-plus 2.0.6 org.apache.velocity velocity 1.7

One is mybatis-plus, and the other is the template engine velocity on which it automatically becomes code.

Then replace the sqlSeassionFactoryBean of mybatis with the enhanced version of plus, and the plug-in can be selectively configured

Mybatis-plus global configuration, mainly configuring id generation strategy, depending on database type

Generate code

Public class MpGenerator {/ *

* MySQL generation demo *

* / public static void main (String [] args) {AutoGenerator mpg = new AutoGenerator (); / / Global configuration\\ Begin\\ src\\ main\ java GlobalConfig gc = new GlobalConfig (); gc.setOutputDir ("G:\\ workspace"); gc.setFileOverride (true); gc.setActiveRecord (true); gc.setEnableCache (false); / / XML second-tier cache gc.setBaseResultMap (true) / / XML ResultMap gc.setBaseColumnList (true); / / XML columList gc.setOpen (false); gc.setAuthor ("XuWei"); / / Custom file naming, note that% s will automatically populate the table entity attributes! Gc.setMapperName ("% sDao"); gc.setXmlName ("% sMapper"); gc.setServiceName ("% sService"); gc.setServiceImplName ("% sServiceImpl"); gc.setControllerName ("% sController"); mpg.setGlobalConfig (gc); / / data source configuration DataSourceConfig dsc = new DataSourceConfig (); dsc.setDbType (DbType.MYSQL) Dsc.setDriverName ("com.mysql.jdbc.Driver"); dsc.setUrl ("jdbc:mysql://localhost:3306/begin?useUnicode=true&characterEncoding=UTF-8&generateSimpleParameterMetadata=true"); dsc.setUsername ("root"); dsc.setPassword ("123"); mpg.setDataSource (dsc); / / Policy configuration StrategyConfig strategy = new StrategyConfig (); / / strategy.setCapitalMode (true) / / Global uppercase naming ORACLE Note strategy.setTablePrefix (new String [] {"t _", "tsys_"}); / / you can modify it here to your table prefix strategy.setNaming (NamingStrategy.underline_to_camel); / / Table name generation policy strategy.setInclude (new String [] {"dept"}) / / tables to be generated / / strategy.setExclude (new String [] {"test"}); / exclude generated tables mpg.setStrategy (strategy); / / service, serviceImpl and controller are all generated by default. Close them here TemplateConfig tc = new TemplateConfig (); tc.setController (null); mpg.setTemplate (tc); / / generate file path / / 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); / / generate mpg.execute ();}

The code is generated to the G:\ workspace directory

Compared with mybayis generator, the code mapping file xml generated by plus, and the dao layer are cleaner, and the general CRUD is implemented through BaseMapper inherited by the dao class.

But the disadvantage is also obvious that the condition constructor cannot directly map the field names in the table to the pojo as generator does, so you need to write the field names corresponding to the query criteria yourself.

If you want to splice such a query condition (user_name =? And password =?) Or (id =? And state =?)

Mybatis-plus conditional construction EntityWrapper ew = new EntityWrapper (); ew.eq ("user_name", "ask the sky"). Eq ("password", "sde"); ew.orNew ("id", 3). Eq ("state", 2); mybatis generator conditional construction UserExample userExample = new UserExample (); userExample.createCriteria () .andUserNameEqualTo ("ask the sky") .andPasswordEqualTo ("sde") UserExample.or () .andIdEqualTo (3) .andStateEqualTo (2); userExample.isDistinct (); the above is about "how to solve the problem of invalid global configuration of Mybatis-Plus". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report