配置pom文件
<dependencies> <!-- springframework --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.0</version> </dependency> <!-- druid-springboot 数据库连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies>
配置yml文件
spring: datasource: druid: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/自己的数据库?serverTimezone = GMT username: root password: root server: port: 80 servlet: context-path:
创建实体类存储数据库获取的数据
user实体类代码:
@Data @NoArgsConstructor @AllArgsConstructor public class User { @TableId(type= IdType.AUTO) private int uid; private String username; private String pwd; }
步骤导入注解
@Data @NoArgsConstructor @AllArgsConstructor
填写属性,然后给属性加注解
编写Mapper
@Mapper public interface UserMapper extends BaseMapper<User> { }
编写测试类,测试是否链接数据库成功(每走一步测一步,不要全部写完在测试)
@Autowired UserMapper mapper; @Test public void getAllUserTest(){ System.out.println(mapper.selectList(null)); }
mapper有红色波浪线很正常,因为idea检测不到实体类中的getset等方法,但是注解已经自动加载了