IDEA搭建mybatis plus的maven项目
搭建环境:
idea2019
maven 3.6.1
mybatis plus
spring boot
lombok插件
mysql
jdk 1.8
第一步
创建spring boot项目
到这springboot项目就创建好了
首先在resources文件夹下创建application.yml文件,添加以下代码
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/数据库名?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: 数据库用户名
password: 密码
添加如下代码
@RestController
@SpringBootApplication
public class SpringbootApplication {
@RequestMapping("/")
public static String index(){
return "hello spring boot";
}
在pom.xml文件中添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.3</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.4</version>
<scope>compile</scope>
</dependency>
</dependencies>
编写用户User类 User.java
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
编写UserMapper文件UserMapper.java
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
在下面的测试类里添加以下代码
@RunWith(SpringRunner.class)
@SpringBootTest
class SpringbootApplicationTests {
@Resource
private UserMapper userMapper;
@Test
void contextLoads() {
//查询全部用户
//参数是一个Mapper,条件构造器
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
}
}
接下来直接运行就可以了 查询功能就已经实现了