CREATE DATABASE `mybatis_plus`
USE `mybatis_plus`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` bigint NOT NULL COMMENT '主键ID',
`name` varchar(30) DEFAULT NULL COMMENT '姓名',
`age` int DEFAULT NULL COMMENT '年龄',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
insert into `user`(`id`,`name`,`age`,`email`) values
(1,'Jone',18,'test1@qq.com'),
(2,'Jack',20,'test2@qq.com'),
(3,'Tom',28,'test3@qq.com'),
(4,'Sandy',21,'test4@qq.com'),
(5,'Billie',24,'test5@qq.com'),
(6,'HeXi',20,'test6@qq.com');
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.1version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
spring:
# 配置数据源信息
datasource:
# 配置数据源类型
type: com.zaxxer.hikari.HikariDataSource
# 配置连接数据库的各个信息
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
username: root
password: 111111
# 日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
package com.sgz.pojo;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
package com.sgz.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sgz.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Repository
//@Mapper // 这里使用@Mapper后,引导类的MapperScan就可以去掉了
public interface UserMapper extends BaseMapper<User> {
}
package com.sgz;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
// 用于扫描mapper接口所在的包
@MapperScan("com.sgz.mapper") // mapper中使用@Mapper,这里的注解就可以去掉了
public class MybaitsplusSpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybaitsplusSpringbootDemoApplication.class, args);
}
}
package com.sgz;
import com.sgz.mapper.UserMapper;
import com.sgz.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class MyBatisPlusTest {
@Autowired
private UserMapper userMapper;
// 查询全部
@Test
void testSelectList() {
// SELECT id,name,age,email FROM user
// 通过条件查询全部数据,调用BaseMapper,如果没有条件则设置为null
userMapper.selectList(null);
}
}