MYBATIS学习笔记:.如何编写模糊查询语句(like 语句)?
两种方法:
1,在java代码中用SQL通配符追加(推荐首选),如下例:
[java] view plain copy
String wildcardName = "%Smi%";
[java] view plain copy
List<Name> names = mapper.selectLike(wildcardName);
[java] view plain copy
[html] view plain copy
<select id="selectLike">
select * from foo where bar like #{value}
<
lect>
2.在sql语句中拼接通配符, 由于可能的sql注入,所以此种方法的安全性低于方法①, 如下:
[java] view plain copy
String wildcardName = "Smi";
List<Name> names = mapper.selectLike(wildcardName);
[html] view plain copy
<select id="selectLike">
select * from foo where bar like '%' || '${value}' || '%'
<
lect>
[html] view plain copy
[html] view plain copy
怎么查询自增长的key值呢?(即怎么查刚刚插入的那条数据的id).
[html] view plain copy
insert方法总是返回一个int类型的数字,这个数字就是刚刚插入数据的行号,也就是id. 自增长的key值先放入参数对象中,待插入操作完成后可见.如下例子:
[html]view plaincopy
<insert id="insertName" useGeneratedKeys="true" keyProperty="id">
insert into names (name) values (#{name})
</insert>
[java]view plaincopy
Name name = new Name();
name.setName("Fred");
int rows = mapper.insertName(name);
System.out.println("rows inserted = " + rows);
System.out.println("generated key value = " + name.getId());