第 1 步:创建数据库表
CREATE TABLE IF NOT EXISTS `tb_account`
(
`id` INTEGER PRIMARY KEY auto_increment,
`user_name` VARCHAR(100),
`age` INTEGER,
`birthday` DATETIME
);
INSERT INTO tb_account(id, user_name, age, birthday)
VALUES (1, '张三', 18, '2020-01-11'),
(2, '李四', 19, '2021-03-21');
第 2 步:创建 Spring Boot 项目,并添加 Maven 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-processor</artifactId>
<version>1.8.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
第 3 步:对 Spring Boot 项目进行配置
配置文件配置(application.yml)
# DataSource Config
spring:
datasource:
url: jdbc:mysql://localhost:3306/flex_test
username: root
password: 12345678
在 SpringBootApplication启动类中添加 @MapperScan
注解
@MapperScan("com.mybatisflex.test.mapper")
第 4 步:编写实体类Account和 Mapper 接口
实体类Account:
@Data
@Table("tb_account")
public class Account {
@Id(keyType = KeyType.Auto)
private Long id;
private String userName;
private Integer age;
private Date birthday;
}
接口类:
package com.mybatisflex.mapper;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.enity.Account;
public interface AccountMapper extends BaseMapper<Account> {
}
第 5 步:编写测试MybatisFlexApplicationTests类
@SpringBootTest
class MybatisFlexApplicationTests {
@Autowired
private AccountMapper accountMapper;
@Test
void contextLoads() {
//查看全部
// List<Account> accounts = accountMapper.selectAll();
// accounts.forEach(System.out::println);
//精确查询
QueryWrapper queryWrapper = QueryWrapper.create()
.select()
.where(ACCOUNT.AGE.eq(18));
Account account = accountMapper.selectOneByQuery(queryWrapper);
System.out.println(account);
}
}
进行全部查询和精确查询测试,控制台输出结果
标签:flex,Account,SpringBoot,spring,boot,private,Mybatis,com From: https://blog.csdn.net/weixin_56894944/article/details/142397482