SpringBoot使用MyBatis-Flex實現靈活的數據庫訪問
創(chuàng)建數據庫表
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>
注意: 如果您當前使用的是 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 中配置數據源:
# 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”) 設置實體類與表名的映射關系
- 使用 @Id(keyType = KeyType.Auto) 標識主鍵為自增
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)導入即可,無需手動編碼。更多查看 APT 文檔。
注意:在構建Queruwrapper的時候ACCOUNT這個地方會報紅,是因為項目沒有編譯,編譯一下就好了。
到此這篇關于SpringBoot使用MyBatis-Flex實現靈活的數據庫訪問的文章就介紹到這了,更多相關SpringBoot MyBatisFlex數據庫訪問內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
HttpServletRequestWrapper干預Request處理流程解析
這篇文章主要分析在?Tomcat的處理?http?請求的流程中干預?Request對象,?通過基于HttpServletRequestWrapper和?Filter組合進行干預,有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-09-09Java中 ? extends T 和 ? super&nb
本文主要介紹了Java中 ? extends T 和 ? super T的理解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05