Java Fluent Mybatis如何验证代码操作数据库情况

这篇文章主要为大家展示了“Java Fluent Mybatis如何验证代码操作数据库情况”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Java Fluent Mybatis如何验证代码操作数据库情况”这篇文章吧。

依赖补充

按照官方给的代码依赖是不够的,这里需要对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);
  }
}

Java Fluent Mybatis如何验证代码操作数据库情况  java 第1张

执行测试代码

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

Java Fluent Mybatis如何验证代码操作数据库情况  java 第2张

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

Java Fluent Mybatis如何验证代码操作数据库情况  java 第3张

代码如下,增加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如何验证代码操作数据库情况  java 第4张

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

Java Fluent Mybatis如何验证代码操作数据库情况  java 第5张

以上是“Java Fluent Mybatis如何验证代码操作数据库情况”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注蜗牛博客行业资讯频道!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接