开发者社区> 问答> 正文

坑爹了的ibatis,硬是报错,就没看出来?报错

就是看出来什么问题,用其他的可以查询,很简单的一下问题:

spring通过dao注入,加入了default-autowire="byName"


<bean id="dao" class="com.ssm.common.CommonDaoImpl">
    <property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
<bean id="menuManageService" class="com.ssm.service.MenuManageServiceImpl" scope="prototype"></bean>
ibatis配置文件
<sqlMapConfig>
	<settings cacheModelsEnabled="true" enhancementEnabled="true"
		lazyLoadingEnabled="true" useStatementNamespaces="true" />

	<sqlMap resource="com/ssm/pojo/menu.xml" />
</sqlMapConfig>
menu.xml文件
<sqlMap namespace="menu">
	<select id="getMax" resultClass="java.lang.String">
		select max(menuId) from sys_menu
	</select>
</sqlMap>

com.ssm.service.MenuManageServiceImpl.java

private CommonDao dao;
public String getMaxOrder() throws SQLException {
		String i = (String) dao.queryObject("menu.getMax", null);
		return i;
	}
public void setCommonDao(CommonDao dao) {
 this.dao = dao;
 }
提示错误是这一行
String i = (String) dao.queryObject("menu.getMax", null);
	com.ssm.service.MenuManageServiceImpl.getMaxOrder(MenuManageServiceImpl.java:23)
	com.ssm.action.MenuManageAction.menuMax(MenuManageAction.java:22)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	java.lang.reflect.Method.invoke(Method.java:597)

这是我的一个测试代码,我写其他的action没问题,数据库是正常的,如果我注解这一行,这个dao运行也是正常的,蛋痛了。。。

public Object queryObject(String sqlId, Object parameter)
			throws SQLException {
		return getSqlMapClient().queryForObject(sqlId, parameter);
	}

展开
收起
爱吃鱼的程序员 2020-06-22 18:20:01 397 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    已经搞定了,具体什么原因没找到,但是这一句没有变

    <selectid="getMax"resultClass="String">
    selectmax(menuId)fromsys_menu
    </select>

    看上去是类型转换问题<selectid="getMax"resultClass="java.lang.String">,这个mendid为类型为uuid,返回类型我用String,一样出错 <selectid="getMax"resultType="string">你这是myibatis写法能不能不要传null呢应该是你传入值为null引起的uuid是long值吧,也有可能为nulllong?你确定?传null变为传空字符应该可以不是传null的问题,我写了一个其他的,一样的测试还成功了,坑啊 publicObjectquerySql(Stringsql)throwsSQLException{returngetSqlMapClient().queryForObject(sql);}我使用这个方法一样错误

    selectmax(menuId)fromsys_menu

    这种要取别名的


    selectmax(menuId)  maxId fromsys_menu

    2020-06-22 18:20:17
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载