Java Fluent Mybatis實戰(zhàn)之構(gòu)建項目與代碼生成篇下
前言
上一篇文章已經(jīng)介紹了fluent-mybatis項目的構(gòu)建,文章地址:Java Fluent Mybatis實戰(zhàn)之構(gòu)建項目與代碼生成篇上
這篇文章繼續(xù)之前的項目,對代碼進(jìn)行基本調(diào)試。驗證代碼操作數(shù)據(jù)庫情況。
依賴補(bǔ)充
按照官方給的代碼依賴是不夠的,這里需要對maven的pom文件進(jìn)行補(bǔ)充。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
數(shù)據(jù)庫文件配置
這里我們還是使用mysql作為測試數(shù)據(jù)庫,fm(fluent mybatis的簡稱)可以支持很多種數(shù)據(jù)庫,暫時我們不考慮其他的數(shù)據(jù)庫。
在application.properties文件中添加mysql數(shù)據(jù)庫配置,至于druid連接池的使用后面的篇章用到再說。也可以用application.yml,這個隨意。
spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://192.168.0.108:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
測試代碼
再測試包中加入測試代碼,主要是做一個簡單的插入數(shù)據(jù)測試。
代碼如下:
package com.hy.fmp.test;
import com.hy.fmp.Application;
import com.hy.fmp.fluent.entity.TestFluentMybatisEntity;
import com.hy.fmp.fluent.mapper.TestFluentMybatisMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Date;
@SpringBootTest(classes = Application.class)
public class InsertTest {
@Autowired TestFluentMybatisMapper testFluentMybatisMapper;
@Test
public void testInsertDefaultValue() {
// 插入數(shù)據(jù)
testFluentMybatisMapper.insert(
new TestFluentMybatisEntity()
.setAge(18)
.setName("法外狂徒張三")
.setCreateTime(new Date())
.setDelFlag(0));
}
}
說明:
1、注意TestFluentMybatisMapper是target包內(nèi)的mapper類。
2、表實體TestFluentMybatisEntity可以通過鏈?zhǔn)降拇a寫法。
@Accessors(
chain = true
)
增加掃描mapper注解
掃描的mapper也是target包內(nèi)的mapper目錄
@SpringBootApplication
@MapperScan({"com.hy.fmp.fluent.mapper"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

執(zhí)行測試代碼
下面我們測試一下插入代碼

發(fā)現(xiàn)這里報了個異常,調(diào)整代碼,增加配置類。

代碼如下,增加MapperFactory注入。
package com.hy.fmp.config;
import cn.org.atool.fluent.mybatis.spring.MapperFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ApplicationConfig {
// @Bean("dataSource")
// public DruidDataSource newDataSource() {
// return DataSourceCreator.create("datasource");
// }
//
// @Bean
// public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
// SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
// bean.setDataSource(newDataSource());
// ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
// // 以下部分根據(jù)自己的實際情況配置
// // 如果有mybatis原生文件, 請在這里加載
// bean.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml"));
// /* bean.setMapperLocations(
// /* new ClassPathResource("mapper/xml1.xml"),
// /* new ClassPathResource("mapper/xml2.xml")
// /* );
// */
// org.apache.ibatis.session.Configuration configuration =
// new org.apache.ibatis.session.Configuration();
// configuration.setLazyLoadingEnabled(true);
// configuration.setMapUnderscoreToCamelCase(true);
// bean.setConfiguration(configuration);
// return bean;
// }
// 定義fluent mybatis的MapperFactory
@Bean
public MapperFactory mapperFactory() {
return new MapperFactory();
}
}
重新執(zhí)行一下看看效果。

執(zhí)行成功,看看表里的數(shù)據(jù)。ok,完美。

總結(jié)
到這里fluent-mybatis組件的代碼構(gòu)建、項目搭建、以及簡單代碼測試已經(jīng)完成。后面我會慢慢研究fm的增刪改查等功能,繼續(xù)工程化這個項目。
代碼GitHub地址: GitHub倉庫
如果本文對你有幫助,請點個贊支持一下吧。

到此這篇關(guān)于Java Fluent Mybatis實戰(zhàn)之構(gòu)建項目與代碼生成篇下的文章就介紹到這了,更多相關(guān)Java Fluent Mybatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java Fluent Mybatis實戰(zhàn)之構(gòu)建項目與代碼生成篇上
- Fluent Mybatis實現(xiàn)環(huán)境隔離和租戶隔離
- Fluent Mybatis 批量更新的使用
- springboot 整合fluent mybatis的過程,看這篇夠了
- Fluent MyBatis實現(xiàn)動態(tài)SQL
- Fluent Mybatis快速入門詳細(xì)教程
- Fluent Mybatis零xml配置實現(xiàn)復(fù)雜嵌套查詢
- Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一
- Java Fluent Mybatis 聚合查詢與apply方法詳解流程篇
相關(guān)文章
10k+點贊的 SpringBoot 后臺管理系統(tǒng)教程詳解
這篇文章主要介紹了10k+點贊的 SpringBoot 后臺管理系統(tǒng)教程詳解,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Centos 7 安裝 OpenJDK 11 兩種方式及問題小結(jié)
這篇文章主要介紹了Centos 7 安裝 OpenJDK 11 兩種方式,第一種方式使用yum安裝,第二種方式使用tar解壓安裝,每種方法給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
如何通過Maven倉庫安裝Spire系列的Java產(chǎn)品
這篇文章主要介紹了如何通過Maven倉庫安裝Spire系列的Java產(chǎn)品,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07
MyBatis-Plus輸出完整SQL(帶參數(shù))的三種方案
當(dāng)我們使用 mybatis-plus 時,可能會遇到SQL 不能直接執(zhí)行,調(diào)試也不方便的情況,那么,如何打印完整 SQL(帶參數(shù))呢?本篇文章將介紹 3 種實現(xiàn)方式,并對比它們的優(yōu)缺點,需要的朋友可以參考下2025-02-02
Java IO文件編碼轉(zhuǎn)換實現(xiàn)代碼
這篇文章主要介紹了Java IO文件編碼轉(zhuǎn)換實現(xiàn)代碼,有需要的朋友可以參考一下2013-12-12

