<where> <if test="lenBname != null and lenBname != ''"> and len_bname like concat('%',#{lenBname},'%') </if> </where>不用<where>和<if>标签
时间: 2024-03-24 13:41:38 浏览: 14
如果你不想使用 `<where>` 和 `<if>` 标签,可以使用以下方式实现:
```sql
AND (#{lenBname} IS NULL OR #{lenBname} = '' OR len_bname LIKE CONCAT('%', #{lenBname}, '%'))
```
这里使用了三个条件,如果 `#{lenBname}` 为 `null` 或空字符串,则第一个条件为真,后面两个条件不会执行;如果 `#{lenBname}` 不为空,则后面两个条件中的 `LIKE` 表达式将被执行。
需要注意的是,这种方式可能会对查询性能产生一定影响,因为无论 `#{lenBname}` 是否为空都会执行 `LIKE` 表达式。因此,如果你的查询数据量较大,建议还是使用 `<where>` 和 `<if>` 标签。
相关问题
<where> <if test="lenBname != null and lenBname != ''"> and len_bname like concat('%',#{lenBname},'%') </if> </where>换个模糊查询的办法
你可以使用通配符 '%' 将模糊查询转换为全文搜索,将上面的代码修改为:
```sql
<where>
<if test="lenBname != null and lenBname != ''">
and match(len_bname) against(#{lenBname} in boolean mode)
</if>
</where>
```
这里使用 `match` 和 `against` 函数实现全文搜索,`in boolean mode` 表示使用布尔全文搜索模式。注意,你需要在 `len_bname` 上创建全文索引才能使用此方法。
<select id="queryBookNameAndType" resultType="zjc.bean.Book" parameterType="map"> <where> <if test="name!=null and name!=''"> bname=#{name} </if> <if test="type!= null and type!=0"> and btype=#{type} </if> </where> </select> 根据上面代码写出对应的@Test测试方式
你可以使用JUnit框架来编写对应的@Test测试方式。首先,需要导入相关的依赖,例如JUnit和MyBatis的依赖。然后,创建一个测试类,并使用注解@Test标记该方法为测试方法。在测试方法中,可以使用MyBatis的配置文件和映射文件进行数据库操作,并验证结果是否符合预期。
下面是一个示例的@Test测试方式的代码:
```java
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@MybatisTest
public class BookMapperTest {
@Autowired
private BookMapper bookMapper;
@Test
public void testQueryBookNameAndType() {
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("name", "bookName");
paramMap.put("type", 1);
List<Book> books = bookMapper.queryBookNameAndType(paramMap);
// 验证结果是否符合预期
// TODO: 编写断言逻辑
// 示例断言:验证查询结果数量是否为1
assertEquals(1, books.size());
}
}
```
在上面的示例代码中,我们使用了@RunWith注解来指定测试运行器为SpringRunner,@MybatisTest注解用于自动配置和初始化MyBatis相关的组件。然后,我们注入了BookMapper接口,并在测试方法中调用了queryBookNameAndType方法进行查询操作。最后,我们可以编写相应的断言逻辑来验证查询结果是否符合预期。
请注意,示例中的断言逻辑仅作为示范,你需要根据实际情况编写适合的断言来验证查询结果。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)