springboot项目中遇到的bug

启动项目的时候报错

情形一

1.Error starting ApplicationContext. 
To display the auto-configuration report re-run your application with 'debug' enabled.

解决方法:
在yml配置文件中加入debug: true,因为默认的话是false

情形二

在集成mybatis时mapper包中的类没被扫描

org.springframework.beans.factory.NoSuchBeanDefinitionException:
 No qualifying bean of type 'com.app.mapper.UserMapper' available: 
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

解决方法:
在springboot的启动类中加入@MapperScan("mapper类的路径")
或者直接在Mapper类上面添加注解@Mapper,建议使用上面那种,不然每个mapper加个注解也挺麻烦的

在向数据库插入数据时报错

"\r\n### Error updating database.  Cause: 
com.mysql.jdbc.MysqlDataTruncation: 
Data truncation: Data too long for column 'password' at row 1\r\n###

数据库表password这个字段太短了,应该设长点

java.lang.ClassCastException: 
com.app.entity.User cannot be cast to java.lang.Integer

用mybatis查询时报错

org.mybatis.spring.MyBatisSystemException: 
nested exception is org.apache.ibatis.binding.BindingException: 
Parameter 'user_type' not found. Available parameters are [2, 1, 0, param1, param2, param3]

其实是忘了使用@Param,当只有一个参数时,Mapper中可以不使用

public User getUser(String name);

但是当你有多个参数时就必须使用

public User getUser(@Param("name") String name,@Param("password") String password);  

Mybatis查询传入一个字符串传参数报错

例如:

mapper接口:
PkRecord findByPkStudentNumber(String pkStudentNumber);
对应的mapper配置文件
<select id="findByPkStudentNumber" resultMap="recordMap" >
        SELECT * FROM pk_record
        <where>
            <if test="pkStudentNumber!=null">
                pk_student_number=#{pkStudentNumber}
            </if>
        </where>
    </select>

然后就会报如下错误

There is no getter for property named 'XXX' in 'class java.lang.String'

原因分析
Mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式取string.num值,引起报错。
解决方法:

  • ①在mapper配置文件中参数名,都要改成_parameter
<select id="findByPkStudentNumber" resultMap="recordMap" >
        SELECT * FROM pk_record
        <where>
            <if test="_parameter!=null">
                pk_student_number=#{_parameter}
            </if>
        </where>
    </select>
  • ②在mapper接口中用@Param在相关方法说明参数值
PkRecord findByPkStudentNumber(@Param("pkStudentNumber") String pkStudentNumber);

mybatis返回值报错

org.apache.ibatis.binding.BindingException: 
Mapper method 'com.hoomsun.mybatis.dao.CostMapperDao.dongtaislq' 
has an unsupported return type: class java.lang.String

dao接口类中对应的方法去掉返回值,用void,例如:

public void dongtaislq(Map map);

mybatis中集合与Stirng类型的比较

报错信息

invalid comparison: java.util.ArrayList and java.lang.String

意思是无法比较这两种类型
我原本是这么写的

<if test="categoryIds!=null and categoryIds!=' ' ">
        AND category_id IN
        <foreach collection="categoryIds" item="categoryIds" open="(" separator="," close=")">
            #{categoryIds}
        </foreach>
</if>

在接收list的时候加了判断 list !=' ',引起了集合与Stirng类型的比较,所以报错,将判断条件改为 : list.size >0就可以了

<if test="categoryIds!=null and categoryIds.size>0" >
        AND category_id IN
        <foreach collection="categoryIds" item="categoryIds" open="(" separator="," close=")">
            #{categoryIds}
        </foreach>
</if>

保存对象数据进数据库后根据ID查询并返回该对象时为null

保存数据我用的是id自动增长

<insert id="saveUser" useGeneratedKeys="true" keyColumn="id" >
        insert into user(username,password,nickname) values (#{username},#{password},#{nickname})
</insert>

这样写的话数据可以保存到数据库,没问题,ID也可以自动增长,不过保存后立刻根据ID查询时返回会为null
解决的方法是把keyColumn换成keyProperty就可以了

<insert id="saveUser" useGeneratedKeys="true" keyProperty="id" >
        insert into user(username,password,nickname) values (#{username},#{password},#{nickname})
</insert>

idea运行项目时报错

//子容器启动失败
ERROR 8760 --- [cat-startStop-1] org.apache.catalina.core.ContainerBase   : A child container failed during start
//未能启动Tomcat组件
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: 
Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]

这里的问题主要是jre环境没选好,可能是由于你之前项目要求改变jre,然后导致之前的项目jre环境也改变了。

idea具有内置tomcat,所以可以不用额外配置tomcat
在idea中点击运行→编辑结构→在配置中选择jre环境

我这里是选用1.8的环境


Paste_Image.png

再次启动项目:


Paste_Image.png

启动成功了

关于对象的判空

如果是返回一个对象时,可以用对象==null来判断,
如果是一个集合的对象,那就要用改集合.size()==0来判断

mybatis插入数据时默认值不生效

你的插入语句是这样的

<insert id="insert" useGeneratedKeys="true" keyProperty="id">
    insert into mmall_category (id, name, status)
    values (#{id}, #{name},#{status})
  </insert>

对应的mapper是

void insert(Category category);

需要传入的是一个对象,假如你在数据库设计时把status设置默认值
在传入对象时只赋值给name,结果你可以发现数据库中status的值是null

这是因为这个对象的其他属性成员你不赋值的话默认为null,并且你在sql语句中#{status},也就是把null赋给了status,但是有时候有需要传status,不能把#{status}去掉,那该怎么办呢?
解决方法:

<insert id="insert" useGeneratedKeys="true" keyProperty="id">
    insert into mmall_category 
      (id, name
        <if test="status != null">
            ,status
        </if>)
    values (#{id}, #{name}
        <if test="status != null">
            ,#{status}
        </if>)
  </insert>

使用mybatis的if test进行值的判断,如果是null的话就不赋值

mybatis的高级结果映射

association – 一个复杂的类型关联;许多结果将包成这种类型
嵌入结果映射 – 结果映射自身的关联,或者参考一个

看起来挺难懂的,看下实例
在resultMap中,有这样的一个映射

<association property="user" column="user_id" select="com.mapper.UserMapper.selectByPrimaryKey"/>

当你用select查询出来对象时想获取userId的值要先获取映射的对象再获取其ID,不然直接获取userId会为空

InterlliJ Debug方式启动特别慢

提示:

Method breakpoints may dramatically slow down debugging

不管你是重启服务器和重启idea还是报这个问题。由该提示语我们可以知道要把方法断点给关掉,查看断点的快捷方式是Ctrl + Shift +F8

image.png

Java Method Breakpoints去掉即可

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值