SpringBoot使用MyBatis-Flex實現(xiàn)靈活的數(shù)據(jù)庫訪問
創(chuàng)建數(shù)據(jù)庫表
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');
創(chuàng)建 Spring Boot 項目,并添加 Maven 依賴
可以使用 Spring Initializer 快速初始化一個 Spring Boot 工程
需要添加的 Maven 主要依賴示例:
<dependencies>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot-starter</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<!-- for test only -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
注意: 如果您當(dāng)前使用的是 SpringBoot v3.x 版本,需要把依賴 mybatis-flex-spring-boot-starter 修改為:mybatis-flex-spring-boot3-starter, 如下代碼所示:
<dependencies>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-spring-boot3-starter</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<!-- for test only -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
對 Spring Boot 項目進行配置
在 application.yml 中配置數(shù)據(jù)源:
# DataSource Config
spring:
datasource:
url: jdbc:mysql://localhost:3306/flex_test
username: root
password: 12345678
在 Spring Boot 啟動類中添加 @MapperScan 注解,掃描 Mapper 文件夾:
@SpringBootApplication
@MapperScan("com.mybatisflex.test.mapper")
public class MybatisFlexTestApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisFlexTestApplication.class, args);
}
}
編寫實體類和 Mapper 接口
這里使用了 Lombok 來簡化代碼。
@Data
@Table("tb_account")
public class Account {
@Id(keyType = KeyType.Auto)
private Long id;
private String userName;
private Integer age;
private Date birthday;
}
- 使用 @Table(“tb_account”) 設(shè)置實體類與表名的映射關(guān)系
- 使用 @Id(keyType = KeyType.Auto) 標(biāo)識主鍵為自增
Mapper 接口繼承 BaseMapper 接口:
public interface AccountMapper extends BaseMapper<Account> {}
開始使用
添加測試類,進行功能測試:
import static com.mybatisflex.test.entity.table.AccountTableDef.ACCOUNT;
@SpringBootTest
class MybatisFlexTestApplicationTests {
@Autowired
private AccountMapper accountMapper;
@Test
void contextLoads() {
QueryWrapper queryWrapper = QueryWrapper.create()
.select()
.where(ACCOUNT.AGE.eq(18));
Account account = accountMapper.selectOneByQuery(queryWrapper);
System.out.println(account);
}
}控制臺輸出:
Account(id=1, userName=張三, age=18, birthday=Sat Jan 11 00:00:00 CST 2020)
以上的 示例 中, ACCOUNT 為 MyBatis-Flex 通過 APT 自動生成,只需通過靜態(tài)導(dǎo)入即可,無需手動編碼。更多查看 APT 文檔。
注意:在構(gòu)建Queruwrapper的時候ACCOUNT這個地方會報紅,是因為項目沒有編譯,編譯一下就好了。
到此這篇關(guān)于SpringBoot使用MyBatis-Flex實現(xiàn)靈活的數(shù)據(jù)庫訪問的文章就介紹到這了,更多相關(guān)SpringBoot MyBatisFlex數(shù)據(jù)庫訪問內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Arrays.sort和Collections.sort排序?qū)崿F(xiàn)原理解析
這篇文章主要介紹了Java Arrays.sort和Collections.sort排序?qū)崿F(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
SpringBoot加載不出來application.yml文件的解決方法
這篇文章主要介紹了SpringBoot加載不出來application.yml文件的解決方法,文中通過示例代碼講解的非常詳細(xì),對大家的學(xué)習(xí)或者工作有一定的幫助,需要的朋友跟著小編來一起來學(xué)習(xí)吧2023-12-12
springboot2.6.3讀取不到nacos上的配置文件問題
這篇文章主要介紹了springboot2.6.3讀取不到nacos上的配置文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
MyBatis-Plus標(biāo)簽@TableField之fill自動填充方式
這篇文章主要介紹了MyBatis-Plus標(biāo)簽@TableField之fill自動填充方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
Spring中將Service注入到Servlet中的四種方法
在Spring中,如果需要將Service注入到Servlet中,可以通過以下幾種方法實現(xiàn),Spring本身提供了對Servlet的集成能力,因此可以結(jié)合Spring的依賴注入機制,將Service類注入到Servlet中,需要的朋友可以參考下2025-05-05

