mybatis中传入String类型参数的问题
2017-11-22 11:38
579 查看
1. 出现的问题
需求是想写一个按公司名字查询公司列表的功能,最开始的代码如下Dao层接口如下:
@MyBatisDao public interface OfficeDao extends TreeDao<Office> { List<Office> findCompanyNameList(String name); }
mybatis的xml代码:
<select id="findCompanyNameList" parameterType="java.lang.String" resultType="com.pds.modules.sys.entity.Office"> SELECT id,name FROM sys_office where o.del_flag = '1' <if test="name!= null and name!= ''"> AND name LIKE concat('%',#{name},'%') </if> </select>
这样写会报错,大体意思是name没有Getter方法
2. 解决办法
2.1 解决办法1
在接口参数里加上mybatis中的@param注解@MyBatisDao public interface OfficeDao extends TreeDao<Office> { List<Office> findCompanyNameList(@Param("name")String name); }
<select id="findCompanyNameList" parameterType="java.lang.String" resultType="com.pds.modules.sys.entity.Office"> SELECT id,name FROM sys_office where o.del_flag = '1' <if test="name!= null and name!= ''"> AND name LIKE concat('%',#{name},'%') </if> </select>
2.2 解决办法2
在xml的if里用”_parameter” 代表参数<select id="findCompanyNameList" parameterType="java.lang.String" resultType="com.pds.modules.sys.entity.Office"> SELECT id,name FROM sys_office where o.del_flag = '1' <if test="_parameter!= null and _parameter!= ''"> AND name LIKE concat('%',#{name},'%') </if> </select>
2.3 两种方法区别
可以看出,_parameter不能区分多个参数,而@param能。所以@param能传多个这样的参数相关文章推荐
- MyBatis 参数类型为String时常见问题解决方法
- java mybatis 框架下多种类型的参数传入到xml问题
- mybatis中传入String类型参数异常
- mybatis传入List参数或者单个String 参数等问题
- MyBatis 参数类型为String时常见问题及解决方法
- Mybatis进行id类型、String类型、map类型、ben类型参数传入Sql查询
- mybatis 传入String 类型的参数
- MyBatis: SQL语句中传入单个String类型的参数的获取
- MyBatis sqlMapper.xml 传入String类型参数报错,invalid comparison: java.util.LinkedHashMap and java.lang.Strin
- MyBatis 传入单个基本类型参数所需要注意的问题
- 在使用mybaitis传参数的时候,仅传入一个类型为String的参数所遇到的问题
- Mybatis进行id类型、String类型、map类型、ben类型参数传入Sql查询
- 关于mybatis 传入基本参数类型使用if test 判断出错的问题
- MyBatis 参数类型为String时常见问题及解决方法
- MyBatis sqlMapper.xml 传入String类型参数报错,invalid comparison: java.util.LinkedHashMap and java.lang.Strin
- 解决使用MyBatis只传入一个String类型的参数时出现异常
- 对于mybatis传入string类型的参数
- mybatis查询参数为0且类型不是string的小问题
- mybatis中传入String类型参数异常
- Mybatis 传入String 类型的动态参数如何写?