public interface CheckItemDao {//查询public Page<CheckItem> selectByCondition(String queryString); }
错误代码 <select id="selectByCondition" parameterType="String" resultType="checkItem">select * from t_checkitem<if test="queryString != null and queryString.length > 0">where code like '%"#{ queryString}"%' or name like '%"#{ queryString}"%'</if></select>
正确代码
使用mybatis默认的对象名:value
<select id="selectByCondition" parameterType="string" resultType="com.itheima.pojo.CheckItem">select * from t_checkitem<if test="value != null and value.length > 0">where code = #{value} or name = #{value}</if> </select>或 使用_parameter
<if test="_parameter != null">f_name like #{_parameter}"%" </if>或 定义@Param要传入的字符串名
List<User> selectedUser(@Param("userId") String userId,@Param("userName") String userName);<select id="selectedUser" resultType="com.xxx.User" parameterType="String">SELECT user_Id AS userId, user_Name AS userName FROM users WHERE user_id = #{userId} AND user_name = #{userName} </select>
会报错
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘name’ in ‘class java.lang.String’
原因:
mybatis自动调用OGNL寻找String的name属性
mybatis中SQL接受的参数分为:(1)基本类型(2)对象(3)List(4)数组(5)Map
无论传哪种参数给mybatis,他都会将参数放在一个Map中:
如果传入基本类型:变量名作为key,变量值作为value 此时生成的map只有一个元素。
如果传入对象: 对象的属性名作为key,属性值作为value,
如果传入List: "list"作为key,这个List是value (这类参数可以迭代,利用标签实现循环)
如果传入数组: "array"作为key,数组作为value(同上)如果传入Map: 键值不变。