Java Fluent Mybatis實(shí)戰(zhàn)之構(gòu)建項(xiàng)目與代碼生成篇下
前言
上一篇文章已經(jīng)介紹了fluent-mybatis項(xiàng)目的構(gòu)建,文章地址:Java Fluent Mybatis實(shí)戰(zhàn)之構(gòu)建項(xiàng)目與代碼生成篇上
這篇文章繼續(xù)之前的項(xiàng)目,對(duì)代碼進(jìn)行基本調(diào)試。驗(yàn)證代碼操作數(shù)據(jù)庫(kù)情況。
依賴(lài)補(bǔ)充
按照官方給的代碼依賴(lài)是不夠的,這里需要對(duì)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ù)庫(kù)文件配置
這里我們還是使用mysql作為測(cè)試數(shù)據(jù)庫(kù),fm(fluent mybatis的簡(jiǎn)稱(chēng))可以支持很多種數(shù)據(jù)庫(kù),暫時(shí)我們不考慮其他的數(shù)據(jù)庫(kù)。
在application.properties文件中添加mysql數(shù)據(jù)庫(kù)配置,至于druid連接池的使用后面的篇章用到再說(shuō)。也可以用application.yml,這個(gè)隨意。
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
測(cè)試代碼
再測(cè)試包中加入測(cè)試代碼,主要是做一個(gè)簡(jiǎn)單的插入數(shù)據(jù)測(cè)試。
代碼如下:
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));
}
}
說(shuō)明:
1、注意TestFluentMybatisMapper是target包內(nèi)的mapper類(lèi)。
2、表實(shí)體TestFluentMybatisEntity可以通過(guò)鏈?zhǔn)降拇a寫(xiě)法。
@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í)行測(cè)試代碼
下面我們測(cè)試一下插入代碼

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

代碼如下,增加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ù)自己的實(shí)際情況配置
// // 如果有mybatis原生文件, 請(qǐng)?jiān)谶@里加載
// 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)建、項(xiàng)目搭建、以及簡(jiǎn)單代碼測(cè)試已經(jīng)完成。后面我會(huì)慢慢研究fm的增刪改查等功能,繼續(xù)工程化這個(gè)項(xiàng)目。
代碼GitHub地址: GitHub倉(cāng)庫(kù)
如果本文對(duì)你有幫助,請(qǐng)點(diǎn)個(gè)贊支持一下吧。

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

