目录
Mybatis-Plus官方推荐在SpringBoot工程中使用,Spring过程自然也是可以用的,这里我只介绍SpringBoot的使用
(1)引入依赖
html<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- MyBatisPlus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.0</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
(2)yml文件配置
html# 数据源 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql:///mybatis?serverTimezone=UTC username: root password: root #日志格式 logging: pattern: console: '%d{HH:mm:ss.SSS} %clr(%-5level) --- [%-15thread] %cyan(%-50logger{50}):%msg%n'
(3)编写Mapper接口,继承BaseMapper
javapublic interface UserMapper extends BaseMapper<user> { }
(4)添加#MapperSan注解,扫描Mapper文件夹
java@SpringBootApplication @MapperScan("com.gq.springnootdemo.mapper") public class SpringnootdemoApplication { public static void main(String[] args) { SpringApplication.run(SpringnootdemoApplication.class, args); } }
(5)测试方法
java@SpringBootTest class SpringnootdemoApplicationTests { @Autowired private UserMapper userMapper; @Test void contextLoads() { user users = userMapper.selectById(1); System.out.println(users); } }