2020-11-13总结

学习内容:

SpringBoot + Gradle + MybatisPlus 配置和使用代码生成器

实践步骤:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<!-- mybatis逆向生成xml配置 -->
<generatorConfiguration>
    <!-- 需要指明数据库连接器的绝对路径 -->
    <classPathEntry
            location="D:\Oracle\ojdbc8.jar"/>
    <context id="sqlserverTables" targetRuntime="MyBatis3">
        <!-- 生成的pojo,将implements Serializable-->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:-->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!-- 数据库链接URL、用户名、密码 -->

        <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
                        connectionURL="jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST =  10.5.14.14)(PORT = 1521)))(CONNECT_DATA =(SID = oratest)(SERVER = DEDICATED)))"
                        userId="mtbf"
                        password="mtbf"/>

        <!--
        默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
            true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
        -->
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!--
        生成model模型,对应的包路径,以及文件存放路径(targetProject),targetProject可以指定具体的路径,./src/main/java,
        也可以使用“MAVEN”来自动生成,这样生成的代码会在target/generatord-source目录下
        -->
        <!--<javaModelGenerator targetPackage="com.auo.entity" targetProject="MAVEN">-->
        <javaModelGenerator targetPackage="com.auo.owls.admin.domain" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"/>
            <!-- 从数据库返回的值被清理前后的空格  -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--对应的mapper.xml文件  -->
        <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 对应的Mapper接口类文件 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.auo.owls.admin.mapper" targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>


        <!-- 要生成的表tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <table tableName="c_ops_tornado" domainObjectName="OpsTornado"
               enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false" >
            <property name="useActualColumnNames" value="false"/>
        </table>
    </context>
</generatorConfiguration>

问题描述:

SpringBoot 项目配置 MybatisPlus时 JdbcType为null


原因分析:

JdbcType为空时 会报出此错误


解决方案:

1.application.properties

mybatis-plus.configuration.jdbc-type-for-null= ‘null’ # 注意单引号

方法2:

查看mp-starter-源码, MybatisPlusAutoConfiguration, 可以发现,第119行有一个configurationCustomizers,可以修改configuration

自定义一个,配上就完工

@Bean
public ConfigurationCustomizer configurationCustomizer(){
    return new MybatisPlusCustomizers();
}

class MybatisPlusCustomizers implements ConfigurationCustomizer {

    @Override
    public void customize(org.apache.ibatis.session.Configuration configuration) {
        configuration.setJdbcTypeForNull(JdbcType.NULL);
    }
方法3:

第一步:把 可更新为空的 javabean 属性前加上注解:@TableField(el = “username, jdbcType=VARCHAR”)

@Email
@TableField(el = “email, jdbcType=VARCHAR”)
private String email;

第二步: 使用updateAllColumnById方法,而不是updateById. 如:
this.baseMapper.updateAllColumnById(user);


### Mybatis的mapper标签 namespace属性说明 namespace配置的是mapper接口所在的包

项目场景:

提示:这里简述项目相关背景:
例如:项目场景:示例:通过蓝牙芯片(HC-05)与手机 APP 通信,每隔 5s 传输一批传感器数据(不是很大)


Invalid bound statement (not found) 终极解决办法 解决方案:

网上已经有很多文章说明可能导致这个报错的原因,无非是以下几种:

1.检查xml文件的namespace是否正确

2.Mapper.java的方法在Mapper.xml中没有,然后执行Mapper的方法会报此

3.xxxMapper.java的方法返回值是List,而select元素没有正确配置ResultMap,或者只配置ResultType

4.如果你确认没有以上问题,请任意修改下对应的xml文件,比如删除一个空行,保存.问题解决

5.看下mapper的XML配置路径是否正确

如果全部检查了一遍,还发现没有问题,最好看下自己的配置文件,那时候很有可能是配置少了扫描mapper的东西:

在创建SqlSessionFactory的时候,加了以下配置:

sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“classpath:mapper/.xml"));
sqlSessionFactoryBean.setTypeAliasesPackage(“com.test.maper”);
而且注意sqlSessionFactoryBean.setTypeAliasesPackage参数不支持通配符
,如果有多个包可以通过”,"等分割

如果需要加载依赖传递过来的jar包中的mapper目录下的xml,classpath:mapper/.xml 修改为classpath:mapper/*.xml

聚合函数查询,mybatis用List<Map<Object,Object>>去接收数据

1.先遍历List
2.在依次取出map中的数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值