mybaits 实现模糊查询
时间: 2024-06-18 16:02:44 浏览: 15
MyBatis是一款优秀的ORM框架,它提供了丰富的查询方式。在MyBatis中,模糊查询可以通过使用通配符来实现。通配符有两种,分别是百分号(%)和下划线(_)。其中,百分号表示匹配0个或多个字符,下划线则表示匹配任意一个字符。
下面是一个使用MyBatis实现模糊查询的示例代码:
```
<select id="getUserByName" parameterType="String" resultMap="userResultMap">
SELECT * FROM user WHERE name LIKE #{name}
</select>
```
在上面的示例中,我们使用了SQL语句的LIKE关键字来实现模糊查询。其中,#{name}表示我们要查询的模糊关键字,这里我们可以在关键字前后添加百分号来实现模糊匹配。比如说,我们想要查询所有姓张的用户,可以将#{name}设置为"张%"。
相关问题
ssm MyBaits模糊查询
在使用 MyBatis 进行模糊查询时,可以使用 SQL 的 LIKE 关键字,结合 % 和 _ 通配符来实现模糊匹配。具体操作如下:
1. 在 mapper.xml 文件中编写 SQL 语句,例如:
```xml
<select id="selectByName" resultType="User">
select * from user where name like #{name}
</select>
```
2. 在 Java 代码中调用该 SQL 语句,并传递模糊匹配的参数,例如:
```java
String name = "%张%";
List<User> userList = sqlSession.selectList("selectByName", name);
```
在这个例子中,% 表示匹配任意字符(包括空字符),而 _ 表示匹配单个字符。因此,"%张%" 表示匹配任意以 "张" 开头和结尾的字符串。
另外,需要注意的是,在使用 LIKE 关键字时,如果查询的数据量较大,可能会影响查询性能,因此可以考虑使用全文检索等技术来优化查询效率。
springboot +mybaits 实现分页查询
Spring Boot是一个快速开发框架,MyBatis是一款优秀的ORM框架,结合两者可以轻松实现分页查询。下面是一个简单的Java代码示例,用于实现分页查询:
1. 首先,在pom.xml文件中添加MyBatis和PageHelper的依赖:
```xml
<!-- MyBatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- PageHelper依赖 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
```
2. 在application.yml文件中配置MyBatis和PageHelper:
```yml
mybatis:
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
```
3. 在DAO层定义分页查询接口:
```java
public interface UserDao {
List<User> findUsersByPage(int pageNum, int pageSize);
}
```
4. 在Mapper XML文件中定义SQL语句:
```xml
<select id="findUsersByPage" resultType="User">
select * from user
</select>
```
5. 在Service层实现分页查询方法:
```java
@Service
public class UserService {
@Autowired
private UserDao userDao;
public PageInfo<User> findUsersByPage(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> userList = userDao.findUsersByPage(pageNum, pageSize);
return new PageInfo<>(userList);
}
}
```
6. 在Controller层调用Service方法:
```java
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users")
public PageInfo<User> getUsers(@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "10") int pageSize) {
return userService.findUsersByPage(pageNum, pageSize);
}
}
```
以上代码使用了PageHelper进行分页操作,最终将分页信息封装在了PageInfo对象中返回给调用者。你可以根据自己的实际情况进行修改和优化。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)