Spring Boot入门4 - MySQL8的使用,基于MyBatis-Plus
新建SpringBoot项目
配置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.20</version>
</dependency>
在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹
@SpringBootApplication
@MapperScan("mybatis.plus.test.mapper")
public class MybatisplustestApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplustestApplication.class, args);
}
}
编写实体类bean.User
表结构根据 baomidou.com/guide/quick-start.html 设计
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
配置application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver # 如果是MySQL5.x则用com.mysql.jdbc.Driver,否则报错
url: jdbc:mysql://ip:port/database_name?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false
username: username
password: password
编写Mapper类 mapper.UserMapper
public interface UserMapper extends BaseMapper<User> {
}
添加测试类,进行功能测试
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
List<User> userList = userMapper.selectList(null);
System.out.print(userList);
}
增删改查的使用,这篇文章写得不错:
《mybatis-plus教程-Mybatis-Plus增删改查》
blog.csdn.net/weixin_44062339/article/details/103078554