在创建 springboot 模块时,依赖勾选时没有 mybaits-plus 选项,原因是其未被加入。所以必须要手动引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.7</version>
</dependency>
将这个依赖引入后,原本的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
这个依赖便可以删除,因为第一个依赖包含了第二个
完整的依赖:
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
在依赖引入后,便成功引入了 mybatis-plus,我们可以继承 baseMapper,这个类实现了很多的常用的数据库操作方法,可以直接使用
@Mapper
public interface UserDao extends BaseMapper<User> {}
完善封装类:
package com.example.springmabatisplus.Pojo;
public class User {
private String username;
private String password;
private int id;
public User() {
}
public User(String username, String password, int id) {
this.username = username;
this.password = password;
this.id = id;
}
/**
* 获取
* @return username
*/
public String getUsername() {
return username;
}
/**
* 设置
* @param username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* 获取
* @return password
*/
public String getPassword() {
return password;
}
/**
* 设置
* @param password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* 获取
* @return id
*/
public int getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(int id) {
this.id = id;
}
public String toString() {
return "User{username = " + username + ", password = " + password + ", id = " + id + "}";
}
}
写测试方法:
@SpringBootTest
class SpringMabatisPlusApplicationTests {
@Autowired
UserDao userDao;
@Test
void contextLoads() {
System.out.println(userDao.selectById(1));
}
}
标签:username,return,springboot,public,plus,mybatis,password,id,String
From: https://blog.csdn.net/m0_75138009/article/details/142102552