千家信息网

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条件构造

        EntityWrapper ew = 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全局配置无效怎么解决"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。

0