8:@MapperScan
主要作用于启动类上,用于生成DAO接口的实现类,如果DAO接口比较多,推荐使用@MapperScan注解,写法如@MapperScan(“com.example.demo.dao”).
9:@Controller
主要作用于控制层类上,用于处理http请求等。
10:@PathVariable
主要是用于取url中的变量的值,比如 @RequestMapping(“/student/{studentName}”),那么在对应的方法入参中可以写成:(@PathVariable String studentName).
11:@RequestParam
将请求参数绑定到Controller的方法上面,@RequestParam(value=”参数名”)。
(5) 如何使用mybaits-plus(用法可以到官网查询 https://baomidou.com/ )
引入 Spring Boot Starter 父工程:
org.springframework.boot
spring-boot-starter-parent
undefined
引入 spring-boot-starter、spring-boot-starter-test、mybatis-plus-boot-starter、h2 依赖:
在 application.yml 配置文件中添加 H2 数据库的相关配置:
# DataSource Config
spring:
datasource:
driver-class-name: org.h2.Driver
schema: classpath:db/schema-h2.sql
data: classpath:db/data-h2.sql
url: jdbc:h2:mem:test
username: root
password: test
在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:
@SpringBootApplication
@MapperScan(“com.baomidou.mybatisplus.samples.quickstart.mapper”)
public class Application {
public static void main(String[] args) {
SpringApplication.run(QuickStartApplication.class, args);
}
}
编写实体类 User.java(此处使用了 Lombok (opens new window)简化代码)
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
编写Mapper类 UserMapper.java
public interface UserMapper extends BaseMapper {
}
添加测试类,进行功能测试:
@SpringBootTest
public class SampleTest {
@Autowired
private