In the ibatis tutorial, how does the like statement look like? There are many examples on the Internet, so this article will introduce some experiences in writing like statements in ibatis.
For the use of the like statement in the ibatis tutorial, We can first look at the use of ibatis on the Internet.
Select * from users where user_name like '% Wang % ';
How to write this like statement in ibatis? The project uses ibatis as the persistent layer framework.
﹤select id="showOneStudentByName" parameterClass="String" resultMap="studentORM﹥" select * from t_stu where s_name like #name# ﹤/select﹥
Obviously, this cannot be written. In the call, % must be added before and after the parameter, for example:
Return sqlmapper. queryforlist ("student. showonestudentbyname", "%" + name + "% ");
This is feasible, but it seems a little uncoordinated.
Finally, the Oracle database is written as follows:
﹤select id="showOneStudentByName" parameterClass="String" resultMap="studentORM"﹥ select * from t_stu where s_name like '%'||#name#||'%' ﹤/select﹥
You do not need to add % before and after the call.
Note: Do not write SQL statements as select * From t_stu where s_name like '% $ name $ %', which is vulnerable to injection attacks.
Additional instructions:
Different data string connectors. List MySQL and sqlserver as follows:
MySQL:
Select * from user
Where username like Concat ('%', # username #, '% ')
Sqlserver:
Select * from user
Where username like '%' + # username # + '%'