SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫(kù)訪問(wèn)
創(chuàng)建數(shù)據(jù)庫(kù)表
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 項(xiàng)目,并添加 Maven 依賴
可以使用 Spring Initializer 快速初始化一個(gè) 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>
對(duì) Spring Boot 項(xiàng)目進(jìn)行配置
在 application.yml 中配置數(shù)據(jù)源:
# DataSource Config spring: datasource: url: jdbc:mysql://localhost:3306/flex_test username: root password: 12345678
在 Spring Boot 啟動(dòng)類中添加 @MapperScan 注解,掃描 Mapper 文件夾:
@SpringBootApplication @MapperScan("com.mybatisflex.test.mapper") public class MybatisFlexTestApplication { public static void main(String[] args) { SpringApplication.run(MybatisFlexTestApplication.class, args); } }
編寫(xiě)實(shí)體類和 Mapper 接口
這里使用了 Lombok 來(lái)簡(jiǎn)化代碼。
@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è)置實(shí)體類與表名的映射關(guān)系
- 使用 @Id(keyType = KeyType.Auto) 標(biāo)識(shí)主鍵為自增
Mapper 接口繼承 BaseMapper 接口:
public interface AccountMapper extends BaseMapper<Account> {}
開(kāi)始使用
添加測(cè)試類,進(jìn)行功能測(cè)試:
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); } }
控制臺(tái)輸出:
Account(id=1, userName=張三, age=18, birthday=Sat Jan 11 00:00:00 CST 2020)
以上的 示例 中, ACCOUNT 為 MyBatis-Flex 通過(guò) APT 自動(dòng)生成,只需通過(guò)靜態(tài)導(dǎo)入即可,無(wú)需手動(dòng)編碼。更多查看 APT 文檔。
注意:在構(gòu)建Queruwrapper的時(shí)候ACCOUNT這個(gè)地方會(huì)報(bào)紅,是因?yàn)轫?xiàng)目沒(méi)有編譯,編譯一下就好了。
到此這篇關(guān)于SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫(kù)訪問(wèn)的文章就介紹到這了,更多相關(guān)SpringBoot MyBatisFlex數(shù)據(jù)庫(kù)訪問(wèn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis-Flex實(shí)現(xiàn)分頁(yè)查詢的示例代碼
- Mybatis-flex整合達(dá)夢(mèng)數(shù)據(jù)庫(kù)的實(shí)現(xiàn)示例
- Spring Boot整合MyBatis-Flex全過(guò)程
- mybatis-flex實(shí)現(xiàn)鏈?zhǔn)讲僮鞯氖纠a
- mybatis-flex實(shí)現(xiàn)多數(shù)據(jù)源操作
- MyBatis-Flex實(shí)現(xiàn)多表聯(lián)查(自動(dòng)映射)
- Springboot集成Mybatis-Flex的示例詳解
- mybatis-flex與springBoot整合的實(shí)現(xiàn)示例
- MyBatis-Flex 邏輯刪除的用法小結(jié)
相關(guān)文章
HttpServletRequestWrapper干預(yù)Request處理流程解析
這篇文章主要分析在?Tomcat的處理?http?請(qǐng)求的流程中干預(yù)?Request對(duì)象,?通過(guò)基于HttpServletRequestWrapper和?Filter組合進(jìn)行干預(yù),有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-09-09一文了解SpringBoot是如何連接數(shù)據(jù)庫(kù)的
Spring Boot提供了一系列的開(kāi)箱即用的功能和特性,使得開(kāi)發(fā)人員可以快速構(gòu)建和部署應(yīng)用程序,下面這篇文章主要給大家介紹了關(guān)于SpringBoot是如何連接數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下2023-06-06Java中 ? extends T 和 ? super&nb
本文主要介紹了Java中 ? extends T 和 ? super T的理解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05MyBatis動(dòng)態(tài)<if>標(biāo)簽的使用
本文主要介紹了MyBatis動(dòng)態(tài)<if>標(biāo)簽的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Java如何通過(guò)ssh遠(yuǎn)程連接主機(jī)并執(zhí)行命令
這篇文章主要介紹了Java如何通過(guò)ssh遠(yuǎn)程連接主機(jī)并執(zhí)行命令問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07SpringBoot整合Druid數(shù)據(jù)源過(guò)程詳解
這篇文章主要介紹了SpringBoot整合Druid數(shù)據(jù)源過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Java數(shù)據(jù)結(jié)構(gòu)順序表用法詳解
順序表是計(jì)算機(jī)內(nèi)存中以數(shù)組的形式保存的線性表,線性表的順序存儲(chǔ)是指用一組地址連續(xù)的存儲(chǔ)單元依次存儲(chǔ)線性表中的各個(gè)元素、使得線性表中在邏輯結(jié)構(gòu)上相鄰的數(shù)據(jù)元素存儲(chǔ)在相鄰的物理存儲(chǔ)單元中,即通過(guò)數(shù)據(jù)元素物理存儲(chǔ)的相鄰關(guān)系來(lái)反映數(shù)據(jù)元素之間邏輯上的相鄰關(guān)系2021-10-10