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 項目進(jìn)行配置
在 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> {}
開始使用
添加測試類,進(jìn)行功能測試:
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)文章
HttpServletRequestWrapper干預(yù)Request處理流程解析
這篇文章主要分析在?Tomcat的處理?http?請求的流程中干預(yù)?Request對象,?通過基于HttpServletRequestWrapper和?Filter組合進(jìn)行干預(yù),有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-09-09一文了解SpringBoot是如何連接數(shù)據(jù)庫的
Spring Boot提供了一系列的開箱即用的功能和特性,使得開發(fā)人員可以快速構(gòu)建和部署應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于SpringBoot是如何連接數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下2023-06-06Java中 ? extends T 和 ? super&nb
本文主要介紹了Java中 ? extends T 和 ? super T的理解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Java如何通過ssh遠(yuǎn)程連接主機并執(zhí)行命令
這篇文章主要介紹了Java如何通過ssh遠(yuǎn)程連接主機并執(zhí)行命令問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07SpringBoot整合Druid數(shù)據(jù)源過程詳解
這篇文章主要介紹了SpringBoot整合Druid數(shù)據(jù)源過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12Java數(shù)據(jù)結(jié)構(gòu)順序表用法詳解
順序表是計算機內(nèi)存中以數(shù)組的形式保存的線性表,線性表的順序存儲是指用一組地址連續(xù)的存儲單元依次存儲線性表中的各個元素、使得線性表中在邏輯結(jié)構(gòu)上相鄰的數(shù)據(jù)元素存儲在相鄰的物理存儲單元中,即通過數(shù)據(jù)元素物理存儲的相鄰關(guān)系來反映數(shù)據(jù)元素之間邏輯上的相鄰關(guān)系2021-10-10