FluentMybatis 项目构建、代码生成(二) | FluentMybatis实践

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: FluentMybatis 项目构建、代码生成(二) | FluentMybatis实践

依赖补充

按照官方给的代码依赖是不够的,这里需要对maven的pom文件进行补充。

        <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>

数据库文件配置

这里我们还是使用mysql作为测试数据库,fm(fluent mybatis的简称)可以支持很多种数据库,暂时我们不考虑其他的数据库。


在application.properties文件中添加mysql数据库配置,至于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

测试代码

再测试包中加入测试代码,主要是做一个简单的插入数据测试。

代码如下:

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() {
    // 插入数据
    testFluentMybatisMapper.insert(
        new TestFluentMybatisEntity()
            .setAge(18)
            .setName("法外狂徒张三")
            .setCreateTime(new Date())
            .setDelFlag(0));
  }
}

说明:


1、注意TestFluentMybatisMapper是target包内的mapper类。


2、表实体TestFluentMybatisEntity可以通过链式的代码写法。


@Accessors(

   chain = true

)

增加扫描mapper注解

扫描的mapper也是target包内的mapper目录

@SpringBootApplication
@MapperScan({"com.hy.fmp.fluent.mapper"})
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

image.png


执行测试代码

下面我们测试一下插入代码


image.png


发现这里报了个异常,调整代码,增加配置类。



image.png

代码如下,增加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();
  //    // 以下部分根据自己的实际情况配置
  //    // 如果有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();
  }
}

重新执行一下看看效果。


image.png


执行成功,看看表里的数据。ok,完美。


image.png


总结

到这里fluent-mybatis组件的代码构建、项目搭建、以及简单代码测试已经完成。后面我会慢慢研究fm的增删改查等功能,继续工程化这个项目。


代码GitHub地址: GitHub仓库


如果本文对你有帮助,请点个赞支持一下吧。


相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
SQL Java 关系型数据库
FluentMybatis 项目构建、代码生成(一) | FluentMybatis实践(2)
FluentMybatis 项目构建、代码生成(一) | FluentMybatis实践
FluentMybatis 项目构建、代码生成(一) | FluentMybatis实践(2)
|
SQL Java Maven
FluentMybatis 项目构建、代码生成(一) | FluentMybatis实践(1)
FluentMybatis 项目构建、代码生成(一) | FluentMybatis实践
FluentMybatis 项目构建、代码生成(一) | FluentMybatis实践(1)
FluentMybatis Where语法(一) | FluentMybatis实践(3)
FluentMybatis Where语法(一) | FluentMybatis实践
FluentMybatis Where语法(一) | FluentMybatis实践(3)
FluentMybatis Where语法(一) | FluentMybatis实践(2)
FluentMybatis Where语法(一) | FluentMybatis实践
FluentMybatis Where语法(一) | FluentMybatis实践(2)
|
API 数据库 网络安全
jfinal想用到中大型项目中的项目经验分享
jfinal 用在大项目中更加方便实用,节省无数的开发时间,代码量相对 SSH 减少 75% 至 90%,对于项目结构来说,简单提以下几点: 1:先分大模块,大模块内部可以根据划分的model分成子包。
1111 0
FluentMybatis 项目工程化、常规操作(增删改查)(二) | FluentMybatis实践
FluentMybatis 项目工程化、常规操作(增删改查)(二) | FluentMybatis实践
FluentMybatis 项目工程化、常规操作(增删改查)(二) | FluentMybatis实践
FluentMybatis 项目工程化、常规操作(增删改查)(一) | FluentMybatis实践(2)
FluentMybatis 项目工程化、常规操作(增删改查)(一) | FluentMybatis实践
FluentMybatis 项目工程化、常规操作(增删改查)(一) | FluentMybatis实践(2)
|
数据库
FluentMybatis 项目工程化、常规操作(增删改查)(二) | FluentMybatis实践(2)
FluentMybatis 项目工程化、常规操作(增删改查)(二) | FluentMybatis实践
FluentMybatis 项目工程化、常规操作(增删改查)(二) | FluentMybatis实践(2)
|
druid Java Maven
FluentMybatis 项目工程化、常规操作(增删改查)(一) | FluentMybatis实践(1)
FluentMybatis 项目工程化、常规操作(增删改查)(一) | FluentMybatis实践

热门文章

最新文章