springboot+mybatisplus查询指定表的数据
时间: 2023-12-25 09:05:37 浏览: 30
可以使用Mybatis-plus提供的Wrapper来指定查询的条件,具体的方法如下:
1. 在pom.xml文件中添加mybatis-plus的依赖:
```
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.x.x</version>
</dependency>
```
2. 在启动类上添加@EnableTransactionManagement和@MapperScan注解,分别启用事务管理和Mapper扫描:
```
@EnableTransactionManagement
@MapperScan("com.example.demo.mapper")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
3. 在mapper接口中定义查询方法,如下面的例子:
```
public interface UserMapper extends BaseMapper<User> {
@Select("select * from user where name = #{name}")
User selectByName(String name);
@Select("select * from user ${ew.customSqlSegment}")
List<User> selectWithWrapper(@Param(Constants.WRAPPER) Wrapper<User> wrapper);
}
```
4. 在service中调用mapper接口的查询方法,如下面的例子:
```
public List<User> selectWithWrapper(String name) {
QueryWrapper<User> wrapper = new QueryWrapper<User>().eq("name", name);
return userMapper.selectWithWrapper(wrapper);
}
```
通过上述步骤,即可使用Mybatis-plus和Spring Boot来查询指定表的数据。