由于我电脑连spring.io连不上,所以我用的阿里的
整个项目的目录结构如下,请先观看再从2进行操作
mysql,lombok,springboot启动器,mybatis-plus
pom.xml
3.在application.properties中配置数据源4.0.0 com.example springboot_mybatis_plus0.0.1-SNAPSHOT springboot_mybatis_plus springboot_mybatis_plus 1.8 UTF-8 UTF-8 2.3.7.RELEASE org.springframework.boot spring-boot-startercom.baomidou mybatis-plus-boot-starter3.4.2 mysql mysql-connector-javaruntime org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest org.junit.vintage junit-vintage-engineorg.springframework.boot spring-boot-dependencies${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin3.8.1 1.8 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin2.3.7.RELEASE com.example.springboot_mybatis_plus.SpringbootMybatisPlusApplication repackage repackage
## 应用名称 #spring.application.name=springboot_mybatis_plus # 数据库驱动: spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ## 数据源名称 #spring.datasource.name=defaultDataSource # 数据库连接地址 spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8 # 数据库用户名&密码: spring.datasource.username=root spring.datasource.password=root3.创建User实体类,和UserMapper接口
User.class
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("user")//注意要加上@TableName("user")注解,我数据库的表名为user, 不加上这条注解会出现找不到表的错误。
public class User {
private Integer id;
private String username;
private String password;
}
UserMapper.class
@Mapper public interface UserMapper extends BaseMapper{ }
UserMapper继承自Mybatisplus里的BaseMapper包,里面几乎包含对单表的所有操作
SpringbootMybatisPlusApplicationTests.class
@SpringBootTest
class SpringbootMybatisPlusApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() {
Object o = userMapper.selectById(1);
System.out.println(o);
}
}



