springboot集合mybatis-plus,mysql 1.引入依赖 2.创建数据库和数据表 3.创建user表对应的实体类(id,username,password) 4.整合mp,创建接口,继承mp的接口 5.用UserMapper查询username
时间: 2024-05-02 12:20:14 浏览: 143
springboot整合mybatis-plus实现多表分页查询
1. 在pom.xml文件中引入以下依赖:
```
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
```
2. 创建数据库和数据表(假设数据库名为test,数据表名为user):
```
CREATE DATABASE test;
USE test;
CREATE TABLE user (
id INT(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
username VARCHAR(20) DEFAULT NULL COMMENT '用户名',
password VARCHAR(20) DEFAULT NULL COMMENT '密码',
PRIMARY KEY (id)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
```
3. 创建User实体类:
```
public class User {
private Long id;
private String username;
private String password;
// getter/setter略
}
```
4. 整合mp,创建接口,继承mp的接口:
```
@Repository
public interface UserMapper extends BaseMapper<User> {
}
```
5. 在需要使用UserMapper的地方注入它,然后调用它的selectOne方法查询username:
```
@Autowired
private UserMapper userMapper;
public User getUserByUsername(String username) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", username);
return userMapper.selectOne(queryWrapper);
}
```
阅读全文