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ù)庫情況。
依賴補充
按照官方給的代碼依賴是不夠的,這里需要對maven的pom文件進(jìn)行補充。
<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)文章
Spring中@Value設(shè)置默認(rèn)值問題解決
本文主要介紹了Spring中@Value設(shè)置默認(rèn)值問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Spring如何消除代碼中的if-else/switch-case
這篇文章主要給大家介紹了關(guān)于Spring如何消除代碼中if-else/switch-case的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Java反射機制詳解_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java反射機制的相關(guān)資料,主要包括反射的概念、作用2017-06-06微信支付java版V3驗證數(shù)據(jù)合法性(Deom)
這篇文章主要介紹了微信支付java版V3驗證數(shù)據(jù)合法性(Deom)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09