14-Mybatis中like使用的两种情况
程序员文章站
2022-05-09 17:21:05
...
前一篇:13-Mybatis封装输出结果之二 – resultMap
https://blog.csdn.net/fsjwin/article/details/109674185
模糊查询,在mybaits中有两种实现方式
1.在java代码中准备好字符串
1.1StudentDao
//使用名字like
public Student selectStudentlike1(String name);
1.2StudentDao.xml
<select id="selectStudentlike1" resultType="com.yuhl.domain.Student">
select id,name,email,age from student where name like #{name}
</select>
1.3测试
@Test
public void test16() {
SqlSession sqlsession = MybatisUtil.getSqlsession();
StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
//可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
Student student = studentDao.selectStudentlike1("%张%");
System.out.println(student);
}
1.4测试结果
Checking to see if class com.yuhl.vo.StudentVo matches criteria [is assignable to Object]
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 188576144.
==> Preparing: select id,name,email,age from student where name like ?
==> Parameters: %张%(String)
<== Columns: id, name, email, age
<== Row: 1001, 张三, zhangsan@qq.com, 20
<== Total: 1
Student{id=1001, name='张三', email='aaa@qq.com', age=20}
2.在mapper中进行拼接
2.1StudentDao
//使用名字like
public Student selectStudentlike2(String name);
2.2StudentDao.xml
<select id="selectStudentlike2" resultType="com.yuhl.domain.Student">
select id,name,email,age from student where name like "%" #{name} "%"
</select>
2.3测试
@Test
public void test17() {
SqlSession sqlsession = MybatisUtil.getSqlsession();
StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
//可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
Student student = studentDao.selectStudentlike2("张");
System.out.println(student);
}
2.4测试结果
Checking to see if class com.yuhl.vo.StudentVo matches criteria [is assignable to Object]
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 188576144.
==> Preparing: select id,name,email,age from student where name like "%" ? "%"
==> Parameters: 张(String)
<== Columns: id, name, email, age
<== Row: 1001, 张三, zhangsan@qq.com, 20
<== Total: 1
Student{id=1001, name='张三', email='aaa@qq.com', age=20}
3.总结
使用这两种方式均可以处模糊查询,推荐第一种
- 在java代码中准备好字符串
- 在mapper中进行拼接
脑图https://www.processon.com/view/link/5fae2e01f346fb2d03b384d3
下一篇:15-Mybatis动态sql之一 – <if>标签https://blog.csdn.net/fsjwin/article/details/109675572
推荐阅读
-
Mysql中SQL语句不使用索引的情况
-
双冒号 ::在PHP中的使用情况
-
JavaScript中定义对象原型的两种使用方法
-
使用jquery获取网页中图片高度的两种方法
-
[日常] 使用TCPDUMP和Ethereal抓包分析HTTP请求中的异常情况
-
Linux中在不破坏磁盘的情况下使用dd命令
-
MySQL中关于ORDERBY、DISTINCT、ALTER、LIKE/NOTLIKE、REGEXP/NOTREGEXP、COUNT、MAX的使用介绍
-
this使用的两种情况
-
this在Java中的必须使用和不推荐使用的情况
-
【转载】Sqlserver数据库中无自增Id的情况下使用ROW_NUMBER()函数进行数据分页