How to use Java Fluent Mybatis to verify operations on the database
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>
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
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)); } }
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 )
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); } }
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(); } }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
