Mybatis-Plus全局配置无效怎么解决
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这篇"Mybatis-Plus全局配置无效怎么解决"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起
千家信息网最后更新 2024年09月22日Mybatis-Plus全局配置无效怎么解决
这篇"Mybatis-Plus全局配置无效怎么解决"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"Mybatis-Plus全局配置无效怎么解决"文章吧。
全局配置无效
依赖
com.baomidou mybatis-plus-boot-starter 3.1.0
配置文件修改
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:"数据库ID自增" INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一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依赖
com.baomidou mybatis-plus 2.0.6 org.apache.velocity velocity 1.7
一个是mybatis-plus,另一个是它自动成成代码所依赖的模板引擎velocity。
然后将mybatis的sqlSeassionFactoryBean替换成plus增强版的,插件可以选择性配置
mybatis-plus全局配置,主要是配置id生成策略,依赖数据库类型,
生成代码
public class MpGenerator { /** ** MySQL 生成演示 *
*/ 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); // 数据源配置 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); // 策略配置 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); // 执行生成 mpg.execute(); }
这样代码生成到G:\workspace目录下面
和mybayis generator相比plus生成的代码映射文件xml,和dao层更加干净,通用的CRUD都通过dao类继承的BaseMapper来实现。
但是缺点也很明显,条件构造器不能像generator那样直接将表中的字段名称和pojo映射,所以需要自己写查询条件对应的字段名称。
如果要拼接这样一个查询条件( user_name = ? and password = ? ) or( id = ? and state = ? )
mybatis-plus条件构造
EntityWrapperew = new EntityWrapper<>(); ew.eq("user_name", "向问天").eq("password", "sde"); ew.orNew("id", 3).eq("state", 2);
mybatis generator条件构造
UserExample userExample = new UserExample(); userExample.createCriteria() .andUserNameEqualTo("向问天") .andPasswordEqualTo("sde"); userExample.or() .andIdEqualTo(3) .andStateEqualTo(2); userExample.isDistinct();
以上就是关于"Mybatis-Plus全局配置无效怎么解决"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。
配置
生成
全局
代码
内容
条件
数据
文件
策略
字段
数据库
类型
名称
实体
文章
用户
知识
篇文章
查询
输入
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
服务器管理口什么意思
重庆扬笛网络技术有限公司
navicat复制数据库
南京软件开发有哪些公司
云服务器能挂魔兽争霸么
新疆关于网络安全的内容
手机软件开发电脑配置要求
微端服务器租用
网络安全学硕考公
维普数据库网站
小学生网络安全教育主题黑板报
深圳网络服务器
陕西省公安厅网络安全等级保护
网络安全教育视频网站
配送类软件开发解决方案
我的世界整蛊好友服务器
实用计算机网络技术结课作业
江苏互联网软件开发费用
网络安全的本质
oracle数据库破解版
河南国资委华为服务器
数据库查询含有王字的人
杰斯视频软件开发
河北红达人网络技术公司
计算机网络技术所有缩写
河北大学网络安全专业导师
如何上传阿里云服务器
魔兽世界 服务器人多
预防电信网络安全教育
游戏数据库技术技巧