资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么使用JavaFluentMybatis验证对数据库的操作

这篇文章主要介绍了怎么使用Java Fluent Mybatis验证对数据库的操作的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么使用Java Fluent Mybatis验证对数据库的操作文章都会有所收获,下面我们一起来看看吧。

10年积累的网站设计制作、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计制作后付款的网站建设流程,更有舟曲免费网站建设让你可以放心的选择与我们合作。

依赖补充

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


            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
        
 
        
            MySQL
            mysql-connector-java
            runtime
        

数据库文件配置

这里我们还是使用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);
  }
}

怎么使用Java Fluent Mybatis验证对数据库的操作

执行测试代码

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

怎么使用Java Fluent Mybatis验证对数据库的操作

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

怎么使用Java Fluent Mybatis验证对数据库的操作

代码如下,增加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();
  }
}

重新执行一下看看效果。

怎么使用Java Fluent Mybatis验证对数据库的操作

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

怎么使用Java Fluent Mybatis验证对数据库的操作

关于“怎么使用Java Fluent Mybatis验证对数据库的操作”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“怎么使用Java Fluent Mybatis验证对数据库的操作”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注创新互联行业资讯频道。


当前名称:怎么使用JavaFluentMybatis验证对数据库的操作
URL分享:http://cdkjz.cn/article/pejhse.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220