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

Basic usage of Mybatis Plus

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "the basic use of Mybatis Plus". In the daily operation, I believe many people have doubts about the basic use of Mybatis Plus. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "the basic use of Mybatis Plus"! Next, please follow the editor to study!

Mybatis-Plus is a Mybatis dynamic SQL automatic injection Mybatis add, delete, modify and check CRUD operation middleware, reduce your development cycle, optimize dynamic maintenance of XML entity fields, non-invasive all-round ORM auxiliary layer allows you to spend more time with your family.

The following is modelled on Mybatis-Plus version 3.0.1

See the official document for details:

Https://mp.baomidou.com/guide/#%E7%89%B9%E6%80%A7

Springboot2.0 integrated mybatis-plus

Pom introduces the required jar package

Com.baomidou mybatis-plus-boot-starter 3.1.0 com.baomidou mybatis-plus-extension 3.1.0 com.baomidou mybatis-plus-generator 3.1.0 org.freemarker freemarker

Configure automatic generation tool classes

Package org.xx.xx.db.util;import com.baomidou.mybatisplus.core.toolkit.StringPool;import com.baomidou.mybatisplus.generator.AutoGenerator;import com.baomidou.mybatisplus.generator.InjectionConfig;import com.baomidou.mybatisplus.generator.config.*;import com.baomidou.mybatisplus.generator.config.po.TableInfo;import com.baomidou.mybatisplus.generator.config.rules.DateType;import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;import java.util.ArrayList;import java.util.List / * * @ Description: * @ Auther: wuxw * @ Date: 14:27 * / public class CodeGeneratorUtil {public static void main (String [] args) {/ / Code Generator AutoGenerator mpg = new AutoGenerator (); / / Global configuration GlobalConfig gc = new GlobalConfig (); String projectPath = System.getProperty ("user.dir") + "/ litemall-db/" Gc.setOutputDir (projectPath + "/ src/main/java"); gc.setAuthor ("wuxw"); gc.setServiceName ("% sService"); / / the file name generated by custom Service API gc.setOpen (false); gc.setBaseResultMap (true); gc.setDateType (DateType.ONLY_DATE); mpg.setGlobalConfig (gc); / / data source configuration DataSourceConfig dsc = new DataSourceConfig () Dsc.setUrl ("jdbc:mysql://127.0.0.1:3306/litemall?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8"); dsc.setDriverName ("com.mysql.cj.jdbc.Driver"); dsc.setUsername ("root"); dsc.setPassword ("123456"); mpg.setDataSource (dsc); / / package configuration PackageConfig pc = new PackageConfig () Pc.setParent ("org.xxx.xxx.db") .setMapper ("dao"); mpg.setPackageInfo (pc); / / Custom configuration InjectionConfig cfg = new InjectionConfig () {@ Override public void initMap () {/ / to do nothing}} / / Custom output configuration List focList = new ArrayList () / / Custom configuration will give priority to output focList.add (new FileOutConfig ("/ templates/mapper.xml.ftl") {@ Override public String outputFile (TableInfo tableInfo) {/ / Custom output file name. If you set the prefix and suffix for Entity, note here that the name of xml will change accordingly! Return projectPath + "/ src/main/resources/mappers/" + tableInfo.getEntityName () + "Mapper" + StringPool.DOT_XML;}}); cfg.setFileOutConfigList (focList); mpg.setCfg (cfg); / / configuration template TemplateConfig templateConfig = new TemplateConfig () / / configure custom output template / / specify custom template path, be careful not to bring .ftl / .vm, it will automatically identify / / templateConfig.setEntity ("templates/entity2.java") according to the template engine used; / / templateConfig.setService (); / / templateConfig.setController (); templateConfig.setXml (null); mpg.setTemplate (templateConfig) / / configure policy StrategyConfig strategy = new StrategyConfig (); strategy.setNaming (NamingStrategy.underline_to_camel); strategy.setColumnNaming (NamingStrategy.underline_to_camel); / / strategy.setSuperControllerClass ("com.example.demo.model.BaseEntity"); strategy.setEntityLombokModel (false); / / default is false / / strategy.setRestControllerStyle (true) / / Public parent class / / strategy.setSuperControllerClass ("com.example.demo.controller.BaseController"); / / Public fields written in the parent class / / strategy.setSuperEntityColumns ("id"); strategy.setInclude ("tb_forum_replay"); / / generate only a single table strategy.setControllerMappingHyphenStyle (true); strategy.setTablePrefix ("tb_"); mpg.setStrategy (strategy) Mpg.setTemplateEngine (new FreemarkerTemplateEngine ()); mpg.execute (); System.out.println ("- automatically generated--");}}

Actual development

@ Api (tags = "forum home page") @ RestController@RequestMapping ("/ admin/forum/") @ Validatedpublic class AdminForumController {} @ Servicepublic class ForumServiceImpl extends ServiceImpl implements ForumService {} @ Mapperpublic interface ForumMapper extends BaseMapper {} @ Data@TableName ("tb_forum") public class Forum implements Serializable {}

The first kind of Select used in actual development is selectCountQueryWrapper qw = new QueryWrapper (); qw.eq ("user_id", userId); qw.eq ("readed", 0); baseMapper.selectCount (qw)

Equivalent to

Select count (*) from tb where use_id = # {userId} and readed = 0

The second kind of selectOneQueryWrapper qw = new QueryWrapper (); qw.eq ("user_id", userId); qw.eq ("readed", 0); qw.last ("limit 1"); baseMapper.selectOne (qw)

Equivalent to

Select count (*) from tb where use_id = # {userId} and readed = 0 limit 1

The first kind of update setUpdateWrapper uw = new UpdateWrapper (); uw.eq ("user_id", userId); uw.eq ("id", id); Forum f = new Forum (); f.setDeleted (1); return forumMapper.update (fMagneuw) > 0

Equivalent to

Update forum set delete = 1 where user_id = # {userId} and id = # {id}

The second kind of insqlUpdateWrapper uw = new UpdateWrapper (); String [] idsStr = new String ["1", "2", "3"]; String id = StringUtils.strip (idsStr.toString (), "[]"); uw.inSql ("id", id); Forum f = new Forum (); f.setDeleted (1); return forumMapper.update (fMagneuw) > 0

Equivalent to

Update forum set deleted = 1 where id in (1,2,3)

Where is the madam? let's take a look at the official documents.

Conditional constructor

All kinds of sql semantics make you use mybatisPlus to fly.

AllEqeqne...

The most important thing is

MybatisX Rapid Development plug-in

Java and XML call back to jump

Automatic generation of XML by Mapper method

At this point, the study on the "basic usage of Mybatis Plus" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report