用springboot mybatisplus 写一个获取user表列表信息
时间: 2024-03-17 19:43:35 浏览: 49
springMvc+mybatis(查询用户信息)Demo5
好的,以下是一个使用Spring Boot和MyBatis Plus获取user表列表信息的示例代码:
首先,需要在maven中添加MyBatis Plus和MySQL连接器的依赖:
```xml
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
</dependencies>
```
接着,在application.properties文件中添加数据库连接等相关配置信息:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# MyBatis Plus 配置
mybatis-plus.mapper-locations=classpath:/mapper/**/*.xml
mybatis-plus.global-config.db-config.id-type=auto
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0
```
然后,创建一个User实体类,对应user表的结构:
```java
public class User {
private Long id;
private String name;
private Integer age;
private String email;
// getter 和 setter 方法省略
}
```
接下来,创建一个UserMapper接口,继承BaseMapper接口,并定义一个getUserList方法,用于查询user表的所有记录:
```java
@Mapper
public interface UserMapper extends BaseMapper<User> {
List<User> getUserList();
}
```
最后,在UserService中调用UserMapper的getUserList方法,获取user表的所有记录:
```java
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> getUserList() {
return userMapper.getUserList();
}
}
```
这样,就可以成功获取到user表的所有记录了。
阅读全文