Home > Java > javaTutorial > body text

How to use Java Fluent Mybatis to verify operations on the database

PHPz
Release: 2023-04-22 18:43:17
forward
1376 people have browsed it

Dependency supplement

It is not enough to follow the official code dependencies. The maven pom file needs to be supplemented here.

<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>
Copy after login

Database file configuration

Here we still use mysql as the test database. FM (short for fluent mybatis) can support many kinds of databases. We will not consider other databases for the time being.

Add the mysql database configuration in the application.properties file. The use of the druid connection pool will be discussed in later chapters. You can also use application.yml, this is optional.

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
Copy after login

Test code

Add test code to the test package, mainly to do a simple data insertion test.

The code is as follows:

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));
  }
}
Copy after login

Instructions:

1. Note that TestFluentMybatisMapper is the mapper class in the target package.

2. The table entity TestFluentMybatisEntity can be written in chain code.

@Accessors(
    chain = true
)
Copy after login

Add scanning mapper annotation

The scanned mapper is also the mapper directory in the target package

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

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

##Execute the test code

Let’s test the inserted code

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

I found an exception reported here, adjusted the code, and added a configuration class.

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

The code is as follows, adding MapperFactory injection.

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();
  }
}
Copy after login
Execute it again to see the effect.

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

The execution is successful, look at the data in the table. OK, perfect.

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

The above is the detailed content of How to use Java Fluent Mybatis to verify operations on the database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!