Get the App
SLTechnology News&Howtos  ›  Development  › 

How Java Fluent Mybatis validates how the code operates the database

Shulou Source: shulou.com Published: 2022-06-01 07:16:00 09月29日 Update

This article mainly shows you "Java Fluent Mybatis how to verify the code operation of the database", the content is easy to understand, clear, hope to help you solve the doubt, the following let the editor lead you to study and learn "Java Fluent Mybatis how to verify the code operation of the database" this article.

Rely on supplement

Depending on the official code is not enough, you need to supplement the pom file of maven.

Org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0 mysql mysql-connector-java runtime database file configuration

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

Add the mysql database configuration to the application.properties file, and the later chapters on the use of druid connection pooling will be used later. You can also use application.yml, which is optional.

Spring.datasource.username=rootspring.datasource.password=123456spring.datasource.url=jdbc:mysql://192.168.0.108:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8spring.datasource.driver-class-name=com.mysql.jdbc.Driver test code

Then add the test code to the test package, mainly to do a simple insert data 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 () {/ / insert data testFluentMybatisMapper.insert (new TestFluentMybatisEntity () .setAge (18) .setName) .setCreateTime (new Date ()) .setDelFlag (0);}

Description:

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

2. Table entity TestFluentMybatisEntity can be written by chained code.

@ Accessors (chain = true) adds scanning mapper annotations

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);}}

Execute the test code

Let's test the insertion code.

Found that an exception was reported here, adjust the code, and add configuration classes.

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; @ Configurationpublic 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 (); / the following sections are configured according to your actual situation / if you have native mybatis files, please load / / bean.setMapperLocations (resolver.getResources ("classpath*:mapper/*.xml")) here / / * 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; / /} / / define MapperFactory @ Bean public MapperFactory mapperFactory () {return new MapperFactory ();}} of fluent mybatis

Re-execute it and see how it works.

The execution is successful. Look at the data in the table. Ok, perfect.

The above is all the contents of the article "how Java Fluent Mybatis verifies how the code operates the database". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Code data database test situation file configuration validation content article learning help success not enough writing can be passed official entity actual effect Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MySQL MariaDB Xiaomi Shulou Tech Info