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ù)庫情況。
依賴補(bǔ)充
按照官方給的代碼依賴是不夠的,這里需要對(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ù)庫文件配置
這里我們還是使用mysql作為測試數(shù)據(jù)庫,fm(fluent mybatis的簡稱)可以支持很多種數(shù)據(jù)庫,暫時(shí)我們不考慮其他的數(shù)據(jù)庫。
在application.properties文件中添加mysql數(shù)據(jù)庫配置,至于druid連接池的使用后面的篇章用到再說。也可以用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
測試代碼
再測試包中加入測試代碼,主要是做一個(gè)簡單的插入數(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、表實(shí)體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)這里報(bào)了個(gè)異常,調(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ù)自己的實(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)目搭建、以及簡單代碼測試已經(jīng)完成。后面我會(huì)慢慢研究fm的增刪改查等功能,繼續(xù)工程化這個(gè)項(xiàng)目。
代碼GitHub地址: GitHub倉庫
如果本文對(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)境隔離和租戶隔離
- Fluent Mybatis 批量更新的使用
- springboot 整合fluent mybatis的過程,看這篇夠了
- Fluent MyBatis實(shí)現(xiàn)動(dòng)態(tài)SQL
- Fluent Mybatis快速入門詳細(xì)教程
- Fluent Mybatis零xml配置實(shí)現(xiàn)復(fù)雜嵌套查詢
- Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一
- Java Fluent Mybatis 聚合查詢與apply方法詳解流程篇
相關(guān)文章
Spring中@Value設(shè)置默認(rèn)值問題解決
本文主要介紹了Spring中@Value設(shè)置默認(rèn)值問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
使用Netty快速實(shí)現(xiàn)一個(gè)群聊功能的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用?Netty?框架開發(fā)一個(gè)?WebSocket?服務(wù)端,從而實(shí)現(xiàn)一個(gè)簡單的在線聊天功能,感興趣的小伙伴可以了解下2023-11-11
Spring如何消除代碼中的if-else/switch-case
這篇文章主要給大家介紹了關(guān)于Spring如何消除代碼中if-else/switch-case的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Java反射機(jī)制詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java反射機(jī)制的相關(guān)資料,主要包括反射的概念、作用2017-06-06
微信支付java版V3驗(yàn)證數(shù)據(jù)合法性(Deom)
這篇文章主要介紹了微信支付java版V3驗(yàn)證數(shù)據(jù)合法性(Deom)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
JavaWeb實(shí)現(xiàn)文件上傳下載功能實(shí)例詳解
這篇文章主要介紹了JavaWeb中的文件上傳和下載功能的實(shí)現(xiàn),在開發(fā)中,文件上傳和下載功能是非常常用的功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

