关于这个@Param()什么时候写什么时候不写的问题,本来觉得反正写着也不麻烦,要不都写吧[笑哭] 》》》
今天心血来潮、、忽然想具体试验下被N多文章提到过必须写@Param() 注解的情况,到底那些是真的有必要的??、、、
由于涉及测试,先列下此次测试项目中引入的与MyBatis相关的jar包版本,特别强调、以下测试都是在这些版本基础上进行的
(((说在前面的话、文章结果并不是说其他文章有问题(因为版本会更新嘛[无奈脸]),结果是可能存在差别的、个人感觉不谈版本的测试都是耍流氓= =
,,提到较多的要添加@Param() 注解的有下面几种情况
1. 多个参数是要写的
2. 要是想给参数起个别名,那得写
3. XML 中的 sql 语句使用了 $ 符号也要写
4. 动态 sql 语句中有参数作为变量,那么该参数就要写
没没没错[突然结巴、、] 看到比较多的就是这几中情况
全写的情况下,当然可以正常运行[机智脸]
Dao层接口处声明如下三个函数:
int getRowCount(@Param("username") String username); //获取数据库总数据数
List<T> findPageObjects(// 查询数据
@Param("username")String username,
@Param("startIndex")Integer startIndex,
@Param("pageSize")Integer pageSize);
int deleteObjects(@Param("ids") Integer...ids); //删除数据
映射文件是这么写的:
<select id="getRowCount" resultType="int">
select count(*) from 数据表
<where>
<if test="username!=null and username!='' ">
username like concat("%",#{username},"%")
</if>
</where>
</select>
<select id="findPageObjects" resultType="com.hehe.pj.sys.entity.T">
select * from 数据表
<where>
<if test="username!=null and username!='' ">
username like concat("%",#{username},"%")
</if>
</where>
limit #{startIndex},#{pageSize}
</select>
<delete id="deleteObjects">
delete from 数据表
<choose>
<when test="ids!=null and ids.length>0">
<where>
<foreach collection="ids" item="id" separator="or">
id=#{id}
</foreach>
</where>
</when>
<otherwise>
where id=-1
</otherwise>
</choose>
</delete>
测试开始
页面加载结果如下:
可以看出来,getRowCount() 可以正常执行~
执行查询操作:
findPageObjects() 可以正常执行~
最后,删除操作:
OK,deleteObjects() 正常执行~~~
SO、加了注解@Param()运行都没啥问题。。。那么我要开始搞事情了、
Test①
Dao层接口处改成下面这样:
int getRowCount(@Param("username") String username); //获取数据库总数据数
List<T> findPageObjects(// 查询数据
String username,
Integer startIndex,
Integer pageSize);
int deleteObjects(@Param("ids") Integer...ids); //删除数据
就去掉了含有多个参数的findPageObjects()方法中各种参数前的注解
结果、、一切正常= =
不想截图了。。跟最开始全加注解的三个结果一毛一样(请自行脑补……
故第一条非必须
Test②
映射文件的findPageObjects方法中的#改成$:
<select id="findPageObjects" resultType="com.hehe.pj.sys.entity.T">
select * from 数据表
<where>
<if test="username!=null and username!='' ">
username like concat("%",${username},"%")
</if>
</where>
limit ${startIndex},${pageSize}
</select>
一切正常= =,so第三条非必须(由于#方式生成的SQL语句变量是带引号的,能够很大程度防止sql注入,建议能用#就用#把)
Test③
Dao层接口处改成下面这样:
int getRowCount(String username); //获取数据库总数据数
List<T> findPageObjects(// 查询数据
String username,
Integer startIndex,
Integer pageSize);
int deleteObjects(@Param("ids") Integer...ids); //删除数据
这次把getRowCount()中的参数的注解也删了
额。。还是一切正常,,由于这两个方法都是用的动态SQL语句,语句中均含有“username”这个变量,因此,第四条非必须
Test④
全删了:
int getRowCount(String username); //获取数据库总数据数
List<T> findPageObjects(// 查询数据
String username,
Integer startIndex,
Integer pageSize);
int deleteObjects(Integer...ids); //删除数据
啊啊啊——终于报错啦(这可怕的心态[笑哭]。。)
翻译下啊
嵌套异常,绑定异常,找不到 “ids” 参数,参数 “array” 是可用的
于是将映射文件中的 “ids” 改为了 “array”
<delete id="deleteObjects">
delete from sys_logs
<where>
<choose>
<when test="array!=null and array.length>0">
<foreach collection="array" separator="or" item="id">
id=#{id}
</foreach>
</when>
<otherwise>
id=-1
</otherwise>
</choose>
</where>
</delete>
测试成功~~
SO、、、有没有 @Param注解 并没有什么直接影响
》》》话说第二条,起了个别名= =当然要加啊,不然咋起别名、、话说、没事起个别名干啥[无奈脸]、、))
总结
mybatis这个版本貌似不怎么需要注解了呀~~只要参数名一致就可以识别,建议(就是开头句→反正写着也不麻烦,都写吧)
初学者的个人理解,,有不对的地方欢迎提出[认真脸]