spring mybitis 集成使用时,DAO 中参数能同时使用实体类和参数吗?

夏碌冬藏 发布于 2014/01/11 19:13
阅读 2K+
收藏 0

List<Admin> selectBySelective(@Param("offset") Long offset, @Param("rows") Long rows,Admin record);


总是报错:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'adminid' not found. Available parameters are [2, param1, param2, param3, offset, rows]
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:368)
at com.sun.proxy.$Proxy10.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:198)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:114)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:58)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:43)
at com.sun.proxy.$Proxy12.selectBySelective(Unknown Source)
at com.huacheng.cms.service.AdminService.getListByObj(AdminService.java:60)
at com.test.dao.AdminTest.getAdminListOfObj(AdminTest.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.apache.ibatis.binding.BindingException: Parameter 'adminid' not found. Available parameters are [2, param1, param2, param3, offset, rows]
at org.apache.ibatis.binding.MapperMethod$ParamMap.get(MapperMethod.java:160)
at org.apache.ibatis.scripting.xmltags.DynamicContext$ContextAccessor.getProperty(DynamicContext.java:120)
at org.apache.ibatis.ognl.OgnlRuntime.getProperty(OgnlRuntime.java:1657)
at org.apache.ibatis.ognl.ASTProperty.getValueBody(ASTProperty.java:92)
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:170)
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:210)
at org.apache.ibatis.ognl.ASTNotEq.getValueBody(ASTNotEq.java:49)
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:170)
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:210)
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:333)
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:413)
at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:395)
at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:45)
at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean(ExpressionEvaluator.java:29)
at org.apache.ibatis.scripting.xmltags.IfSqlNode.apply(IfSqlNode.java:30)
at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:29)
at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:37)
at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:275)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:79)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:104)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:98)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:358)
... 36 more


以下是问题补充:

@夏碌冬藏:根据@1哥的说明,顺利通过,xml文件如下 <select id="selectBySelective" resultType="com.huacheng.cms.model.Admin" parameterType="com.huacheng.cms.model.Admin"> select * from `admin` where 1 = 1 <if test="record.adminid != null">AND adminId = #{record.adminid,jdbcType=BIGINT}</if> <if test="record.email != null">AND email = #{record.email,jdbcType=VARCHAR}</if> <if test="record.name != null">AND `name` = #{record.name,jdbcType=VARCHAR}</if> <if test="record.password != null">AND `password` = #{record.password,jdbcType=VARCHAR}</if> <if test="record.status != null">AND `status` = #{record.status,jdbcType=VARCHAR}</if> <if test="record.createtime != null">AND createTime = #{record.createtime,jdbcType=TIMESTAMP}</if> <if test="record.lasttime != null">AND lasttime = #{record.lasttime,jdbcType=TIMESTAMP}</if> <if test="record.ip != null">AND ip = #{record.ip,jdbcType=VARCHAR}</if> ORDER BY adminId DESC limit #{offset},#{rows} </select> (2014/01/14 17:48)
加载中
1
有个二逼领导的程序员
有个二逼领导的程序员

@Param("offset") Long offset, @Param("rows") Long rows,@Param("record") Admin record



然后xml里使用 record.属性 来使用

0
LucEsape
LucEsape
当然可以。把你的mapper 发上来
0
夏碌冬藏

引用来自“烤蕃薯的野孩子”的答案

当然可以。把你的mapper 发上来
public interface AdminMapper {
int deleteByPrimaryKey(Long adminid);


int insert(Admin record);


int insertSelective(Admin record);


Admin selectByPrimaryKey(Long adminid);


int updateByPrimaryKeySelective(Admin record);


List<Admin> getAllList(@Param("offset") long offset, @Param("rows") long rows);


int getAllListCount();


List<Admin> selectBySelective(Admin record, @Param("offset") Long offset, @Param("rows") Long rows);


int selectBySelectiveCount(Admin record);

}



<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.huacheng.cms.dao.AdminMapper">
<resultMap id="BaseResultMap" type="com.huacheng.cms.model.Admin">
<id column="adminId" property="adminid" jdbcType="BIGINT" />
<result column="email" property="email" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="status" property="status" jdbcType="VARCHAR" />
<result column="createTime" property="createtime" jdbcType="TIMESTAMP" />
<result column="lasttime" property="lasttime" jdbcType="TIMESTAMP" />
<result column="ip" property="ip" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List">
adminId, email, `name`, `password`, `status`, createTime,
lasttime, ip
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long">
select
<include refid="Base_Column_List" />
from `admin`
where adminId = #{adminid,jdbcType=BIGINT}
</select>


<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from
`admin`
where adminId = #{adminid,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="com.huacheng.cms.model.Admin" useGeneratedKeys="true" keyProperty="adminid">
insert into
`admin` (email,
`name`, `password`,
`status`, createTime, lasttime,
ip)
values
(#{email,jdbcType=VARCHAR},
#{name,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{status,jdbcType=VARCHAR},
#{createtime,jdbcType=TIMESTAMP},
#{lasttime,jdbcType=TIMESTAMP},
#{ip,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.huacheng.cms.model.Admin" useGeneratedKeys="true" keyProperty="adminid">
insert into `admin`
<trim prefix="(" suffix=")" suffixOverrides=",">
email,
`name`,
`password`,
`status`,
createTime,
lasttime,
ip,
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
#{email,jdbcType=VARCHAR},
#{name,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{status,jdbcType=VARCHAR},
#{createtime,jdbcType=TIMESTAMP},
#{lasttime,jdbcType=TIMESTAMP},
#{ip,jdbcType=VARCHAR},
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.huacheng.cms.model.Admin">
update `admin`
<set>
<if test="email != null">email = #{email,jdbcType=VARCHAR},</if>
<if test="name != null">`name` = #{name,jdbcType=VARCHAR},</if>
<if test="password != null">`password` = #{password,jdbcType=VARCHAR},</if>
<if test="status != null">`status` = #{status,jdbcType=VARCHAR},</if>
<if test="createtime != null">createTime = #{createtime,jdbcType=TIMESTAMP},</if>
<if test="lasttime != null">lasttime = #{lasttime,jdbcType=TIMESTAMP},</if>
<if test="ip != null">ip = #{ip,jdbcType=VARCHAR},</if>
</set>
where adminId = #{adminid,jdbcType=BIGINT}
</update>


<select id="getAllList" resultType="com.huacheng.cms.model.Admin">
select * from `admin` ORDER BY adminId DESC limit #{offset},#{rows}
</select>


<select id="getAllListCount" resultType="int">select count(*) from `admin`</select>


<select id="selectBySelective" resultType="com.huacheng.cms.model.Admin" parameterType="com.huacheng.cms.model.Admin">
select * from `admin` where 1 = 1
<if test="adminId != null">AND adminId = #{adminid,jdbcType=BIGINT}</if>
<if test="email != null">AND email = #{email,jdbcType=VARCHAR}</if>
<if test="name != null">AND `name` = #{name,jdbcType=VARCHAR}</if>
<if test="password != null">AND `password` = #{password,jdbcType=VARCHAR}</if>
<if test="status != null">AND `status` = #{status,jdbcType=VARCHAR}</if>
<if test="createtime != null">AND createTime = #{createtime,jdbcType=TIMESTAMP}</if>
<if test="lasttime != null">AND lasttime = #{lasttime,jdbcType=TIMESTAMP}</if>
<if test="ip != null">AND ip = #{ip,jdbcType=VARCHAR}</if>
ORDER BY adminId DESC limit #{offset},#{rows}
</select>


<select id="selectBySelectiveCount" resultType="int" parameterType="com.huacheng.cms.model.Admin">
select count(*) from `admin` where 1 = 1
<if test="adminId != null">AND adminId = #{adminid,jdbcType=BIGINT}</if>
<if test="email != null">AND email = #{email,jdbcType=VARCHAR}</if>
<if test="name != null">AND `name` = #{name,jdbcType=VARCHAR}</if>
<if test="password != null">AND `password` = #{password,jdbcType=VARCHAR}</if>
<if test="status != null">AND `status` = #{status,jdbcType=VARCHAR}</if>
<if test="createtime != null">AND createTime = #{createtime,jdbcType=TIMESTAMP}</if>
<if test="lasttime != null">AND lasttime = #{lasttime,jdbcType=TIMESTAMP}</if>
<if test="ip != null">AND ip = #{ip,jdbcType=VARCHAR}</if>
</select>
</mapper>

0
LucEsape
LucEsape
<select id="selectBySelective" resultType="com.huacheng.cms.model.Admin" <!-- 你参数有多个类型的是时候,不要去指定 parameterType="com.huacheng.cms.model.Admin"-->>
select * from `admin` where 1 = 1
<if test="adminId != null">AND adminId = #{adminid,jdbcType=BIGINT}</if>
<if test="email != null">AND email = #{email,jdbcType=VARCHAR}</if>
<if test="name != null">AND `name` = #{name,jdbcType=VARCHAR}</if>
<if test="password != null">AND `password` = #{password,jdbcType=VARCHAR}</if>
<if test="status != null">AND `status` = #{status,jdbcType=VARCHAR}</if>
<if test="createtime != null">AND createTime = #{createtime,jdbcType=TIMESTAMP}</if>
<if test="lasttime != null">AND lasttime = #{lasttime,jdbcType=TIMESTAMP}</if>
<if test="ip != null">AND ip = #{ip,jdbcType=VARCHAR}</if>
ORDER BY adminId DESC limit #{offset},#{rows}
</select>



0
夏碌冬藏

引用来自“烤蕃薯的野孩子”的答案

<select id="selectBySelective" resultType="com.huacheng.cms.model.Admin" <!-- 你参数有多个类型的是时候,不要去指定 parameterType="com.huacheng.cms.model.Admin"-->>
select * from `admin` where 1 = 1
<if test="adminId != null">AND adminId = #{adminid,jdbcType=BIGINT}</if>
<if test="email != null">AND email = #{email,jdbcType=VARCHAR}</if>
<if test="name != null">AND `name` = #{name,jdbcType=VARCHAR}</if>
<if test="password != null">AND `password` = #{password,jdbcType=VARCHAR}</if>
<if test="status != null">AND `status` = #{status,jdbcType=VARCHAR}</if>
<if test="createtime != null">AND createTime = #{createtime,jdbcType=TIMESTAMP}</if>
<if test="lasttime != null">AND lasttime = #{lasttime,jdbcType=TIMESTAMP}</if>
<if test="ip != null">AND ip = #{ip,jdbcType=VARCHAR}</if>
ORDER BY adminId DESC limit #{offset},#{rows}
</select>



搜索了很多,都没找到实体类与limit同时使用的例子。

主要不想写其它不同字段分别查询的语句,试着这样写就节省了。

LucEsape
LucEsape
你想实现分页的功能可以自己封装一个PagerBean啊。里面有你想要的属性(Model,Limit 参数)
0
飞翔的香蕉皮
飞翔的香蕉皮
本人新创建一开发技术群、、、群号:337613367 欢迎热爱技术的朋友加入cc
返回顶部
顶部