您好!欢迎来到爱源码

爱源码

热门搜索: 抖音快手短视频下载   

还在写CRUD代码?这个开源框架帮助你解放双手。 [网站源码]

  • 时间:2022-08-30 00:52 编辑: 来源: 阅读:296
  • 扫一扫,手机访问
摘要:还在写CRUD代码?这个开源框架帮助你解放双手。 [网站源码]
我相信项目中很多朋友用的ORM框架是MyBatis。如果只使用MyBatis操作数据库,需要编写很多单表查询的SQL实现。 这个时候我们往往会选择一个增强的工具来实现这些单表CRUD操作。这里推荐一个好用的工具MyBatis-Plus!MyBatis-Plus简介MyBatis-Plus(简称MP)是MyBatis的增强工具。基于MyBatis,只增强不改变,为简化开发,提高效率而生。 MyBatis-Plus提供了代码生成器,可以一键生成controller、service、mapper、model、mapper.xml代码。同时提供丰富的CRUD操作方法,帮助我们解放双手!MyBatis-Plus集成首先我们需要在SpringBoot项目中集成MyBatis-Plus,然后我们会详细介绍它的用法!在pom.xml中添加相关依赖,主要是MyBatis-Plus、MyBatis-Plus生成器和Velocity模板引擎;& lt依赖关系& gt& lt!- Mybatis-Plus依赖项-->:& lt;依赖性& gt& ltgroupId & gtcom.baomidou & lt/groupId & gt;& ltartifactId & gtmy batis-plus-boot-starter & lt;/artifact id & gt;& lt版本& gt3 . 3 . 2 & lt;/version & gt;& lt/dependency & gt;& lt!- Mybatis-Plus代码生成器-->:& lt;依赖性& gt& ltgroupId & gtcom.baomidou & lt/groupId & gt;& ltartifactId & gtmybatis加发电机& lt/artifact id & gt;& lt版本& gt3 . 3 . 2 & lt;/version & gt;& lt/dependency & gt;& lt!- Velocity模板生成引擎-->:& lt;依赖性& gt& ltgroupId & gtorg . Apache . velocity & lt;/groupId & gt;& ltartifactId & gt速度引擎核心& lt/artifact id & gt;& lt版本& gt2.2 & lt/version & gt;& lt/dependency & gt;& lt/dependencies & gt;将以下配置添加到SpringBoot配置文件application.yml中,配置数据源和MyBatis-Plus;;spring:data source:URL:JDBC:MySQL://localhost:3306/mall?useUnicode = true & amp字符编码= utf-8 & amp;serverTimezone=Asia/Shanghai用户名:root密码:rootmybatis-plus:mapper-locations:class path:/mapper/* */*。xml #指定mapper.xml路径Global-Config:d b-Config:ID-Type:Auto #全局默认主键类型设置为Auto-Mapping-Behavior: Partial #仅自动映射非嵌套的result map-下划线-to-camel-case: true #打开自动hump命名规则映射以增加MyBatis-Plus的Java配置,并使用@mapperScan注释来配置需要代码扫描的Mapper接口路径。MyBatis-Plus自带分页功能,需要配置分页插件PaginationInterceptor。 /* * * 2019/4/8宏创建的MyBatis配置类*。*/@ configuration @ mappers can(" com . macro . mall . tiny . modules . *。mapper ")公共类MyBatisConfig { @ Bean public pagination interceptor pagination interceptor(){ pagination interceptor pagination interceptor = new pagination interceptor();pagination interceptor . setcountsqlparser(new jsqlparsercountoprize(true));返回paginationInterceptor}}代码生成器MyBatis-Plus提供了一个代码生成器,可以一键生成controller、service、mapper、model、mapper.xml代码。很方便!首先我们创建代码生成器类MyBatisPlusGenerator,直接运行其main方法生成相关代码;/** * MyBatisPlus代码生成器*由宏在2020/8/20创建。*/public class MyBatisplus generator { public static void main(String[]args){ String project path = system . getproperty(" user . dir ")+"/mall-tiny-plus ";String moduleName = scanner("模块名");String[] tableNames = scanner("表格名称,用多个英文逗号分隔")。拆分(“,”);//代码生成器autogenerator autogenerator = new auto generator();autogenerator . setglobalconfig(initGlobalConfig(project path));auto generator . set data source(initDataSourceConfig());auto generator . setpackageinfo(initPackageConfig(moduleName));auto generator . set CFG(initInjectionConfig(project path,moduleName));auto generator . set template(initTemplateConfig());auto generator . set strategy(initStrategyConfig(tableNames));auto generator . settemplatengine(new velocity template engine());auto generator . execute();}/* * *读取控制台内容信息*/私有静态字符串扫描器(字符串提示){ scanner scanner = new scanner(system . in);system . out . println(" Please enter "+tip+":));if(scanner . has next()){ String next = scanner . next();if(strutil . isnotempty(next)){ return next;}}抛出新MybatisPlusException("请输入正确的"+tip+"!");}/* * *初始化全局配置*/private静态全局配置initglobal config(字符串项目路径){ global config global config = new global config();global config . setoutputdir(project path+"/src/main/Java ");global config . set author(" macro ");global config . set open(false);global config . setswagger 2(true);global config . setbaseresultmap(true);global config . setfile override(true);global config . set date type(date type。ONLY _ DATE);global config . setentityname(" % s ");global config . setmappername(" % s mapper ");global config . setxml name(" % s mapper ");global config . set service name(" % s service ");global config . setserviceimplname(" % sServiceImpl ");global config . setcontrollername(" % s controller ");返回globalConfig}/* * *初始化数据源配置*/私有静态数据源配置initdatasourceconfig(){ props = new props(" generator . properties ");data source config data source config = new data source config();data source config . seturl(props . getstr(" data source . URL "));data source config . set driver name(props . getstr(" data source . driver name "));data source config . set username(props . getstr(" data source . username "));data source config . set password(props . getstr(" data source . password "));返回dataSourceConfig}/* * *初始化包配置*/私有静态包配置init package config(string modulename){ props props = new props(" generator . properties ");package config package config = new package config();package config . setmodulename(moduleName);package config . set parent(props . getstr(" package . base "));package config . setentity(" model ");返回包配置;}/* * *初始化模板配置*/private static template config inittemplate config(){ template config template config = newtemplateconfig();//可以配置控制器、服务和实体模板。//mapper.xml模板需要单独配置template config . setxml(null);返回templateConfig}/* * *初始化策略配置*/私有静态策略配置init strategy config(string[]tablenames){ strategy config strategy config = new strategy config();strategy config . set naming(namingstrategy . underline _ to _ camel);strategy config . set column naming(namingstrategy . underline _ to _ camel);strategy config . setentitylombokmodel(true);strategy config . setrestcontrollerstyle(true);//通配符模式if(tablenames . length = = 1 & amp;表名[0]。包含(" * "){ String[]likeStr = tableNames[0]。拆分(“_”);string like prefix = likeStr[0]+" _ ";strategy config . setliketable(new like table(like prefix));} else { strategy config . set include(tableNames);}返回strategyConfig}/* * *初始化自己的设置configuration */private static injection config initinjection config(String project path,String moduleName) {//设置自己的配置injection config injection config = new injection config(){ @ override public void init map(){//可以用来自己设置属性} };//模板引擎是Velocity String Template Path = "/templates/mapper . XML . VM ";//设置自己的输出配置列表< FileOutConfig >focList = new ArrayList & lt& gt();//自己设置的配置会优先输出到focl ist . add(new file out config(template path){ @ override public string output file(tableinfo tableinfo){//自己设置输出文件名。如果设置了实体的前缀和后缀,这里请注意,xml的名称会相应改变!!返回project path+"/src/main/resources/Mapper/"+moduleName+"/"+tableinfo . getentityname()+" Mapper "+string pool。DOT _ XML} });injection config . setfileoutconfiglist(foc list);返回injectionConfig}}然后,在resources目录下添加配置文件generator.properties,增加代码生成器的数据源配置和存储业务代码的基础包名称;data source . URL = JDBC:MySQL://localhost:3306/mall?useUnicode = true & amp字符编码= utf-8 & amp;server time zone = Asia/Shanghai data source . driver name = com . MySQL . CJ . JDBC . driver data source . username = root data source . password = root package . base = com . macro . mall . tiny . modules细心的朋友可以发现MyBatisPlusGenerator中的很多配置代码都没有添加注释。事实上,MyBatis-Plus源代码中的中文注释非常完美。只需检查源代码。下面是DataSourceConfig中源代码的摘录。/* * *数据库配置* * @ AuthorYanghu,HCl * @ since 2016/8/30 */@ data @ accessors(chain = true)公共类数据源配置{/* * *数据库信息查询*/私有IDbQuery dbQuery/* * *数据库类型*/私有DbType dbType/** * PostgreSQL schemaName */私有字符串schemaName;/* * *类型转换*/privateitypeconvert类型转换;/* * *关键字解析器* @ sinc 3 . 3 . 2 */PrivateIKeywordHandler关键字处理程序;/* * *驱动连接的URL */private string URL;/* * *驱动程序名称*/私有字符串Driver Name/* * *数据库连接客户名称*/私有字符串用户名;/* * *数据库连接密码*/私有字符串密码;//省略一些代码......}代码生成器支持两种模式,一种是生成单个表代码,比如只生成pms_brand表代码,可以先输入pms再输入PMS _ brand;还在写CRUD代码?这个开源框架帮助您解放双手,生成单表代码结构列表;还在写CRUD代码?这个开源框架帮助你解放双手。另一个直接生成整个模块的代码需要通配符。比如生成ums模块代码,可以先输入ums,然后输入UMS _;还在写CRUD代码?这个开源框架帮助您解放双手,生成整个模块代码结构的列表。 还在写CRUD代码?这个开源框架可以帮助你解放双手来建立你自己的生成模板。MyBatis-Plus使用模板引擎生成代码,支持Velocity(默认)、Freemarker和Beetl模板引擎。在这里,如何为了速度而设置自己的生成模板。 首先,我们可以从MyBatis-Plus生成器依赖包的源代码中找到默认模板,复制到项目的resources/templates目录下;还在写CRUD代码?这个开源框架帮助您解放双手,在MyBatisPlusGenerator类中配置TemplateConfig,并配置每个模板的路径;/** * MyBatisPlus代码生成器*由宏在2020/8/20创建。*/PublicClass MyBatisplus生成器{/* * *初始化模板配置*/private static template config inittemplate config(){ template config template config = new template config();//可以为控制器、服务、实体的模板配置模板config . setentity(" templates/entity . Java ");template config . set mapper(" templates/mapper . Java ");template config . set controller(" templates/controller . Java ");template config . set service(" templates/service . Java ");template config . setserviceimpl(" templates/service impl . Java ");//mapper.xml模板需要单独配置template config . setxml(null);返回templateConfig}}}自定义模板。在定制的过程中,我们可以找到许多内置变量用于输出到模板。这里以service.java.vm模板为例,比如包和表变量;包${package。服务};导入${package。实体}。$ { entity };导入$ { superServiceClassPackage };/* * * & lt;p & gt* $!{ table.omment }服务等级*


  • 全部评论(0)
资讯详情页最新发布上方横幅
最新发布的资讯信息
【域名/主机/服务器|】qq邮箱提醒在哪里打开(2024-06-04 18:58)
【技术支持|常见问题】1556原创ng8文章搜索页面不齐(2024-05-01 14:43)
【技术支持|常见问题】1502企业站群-多域名跳转-多模板切换(2024-04-09 12:19)
【技术支持|常见问题】1126完美滑屏版视频只能显示10个(2024-03-29 13:37)
【技术支持|常见问题】响应式自适应代码(2024-03-24 14:23)
【技术支持|常见问题】1126完美滑屏版百度未授权使用地图api怎么办(2024-03-15 07:21)
【技术支持|常见问题】如何集成阿里通信短信接口(2024-02-19 21:48)
【技术支持|常见问题】算命网微信支付宝产品名称年份在哪修改?风水姻缘合婚配对_公司起名占卜八字算命算财运查吉凶源码(2024-01-07 12:27)
【域名/主机/服务器|】帝国CMS安装(2023-08-20 11:31)
【技术支持|常见问题】通过HTTPs测试Mozilla DNS {免费源码}(2022-11-04 10:37)

联系我们
Q Q:375457086
Q Q:526665408
电话:0755-84666665
微信:15999668636
联系客服
企业客服1 企业客服2 联系客服
86-755-84666665
手机版
手机版
扫一扫进手机版
返回顶部