Git Product home page Git Product logo

querywarpperdemo's Introduction

QueryWarpperDemo

此代码主要用于复现mybatis-plus的QueryWarpper查询不出结果的情况

Issue URL:baomidou/mybatis-plus#4918

此问题的主要原因是自己写的@TableField(typeHandler = ObjectToJson.class)存在解析问题,导致QueryWarpper查询不出结果的情况

下方是ObjectToJson.class代码:

@Component
public class ObjectToJson extends BaseTypeHandler<Object> {
    @Resource
    private ObjectMapper objectMapper;

    private <T> String ObjectToJson(T o){
        try {
            return objectMapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            throw new ControllerException(RCode.SYSTEM_ERROR,e.getMessage());
        }
    }

    private <T> T jsonToObject(String json, TypeReference<T> typeReference){
        if (json==null)return null;
        try {
            return objectMapper.readValue(json,typeReference);
        }catch (JsonProcessingException e) {
            throw new ControllerException(RCode.SYSTEM_ERROR,e.getMessage());
        }
    }



    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {
        String json=ObjectToJson(o);
        preparedStatement.setString(i,json);
    }

    @Override
    public Object getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String str=resultSet.getString(s);
        return jsonToObject(str, new TypeReference<>() {
        });
    }

    @Override
    public Object getNullableResult(ResultSet resultSet, int i) throws SQLException {
        String str=resultSet.getString(i);
        return jsonToObject(str, new TypeReference<>() {
        });
    }

    @Override
    public Object getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        String str=callableStatement.getString(i);
        return jsonToObject(str, new TypeReference<>() {
        });
    }
}

官方指定提供JacksonTypeHandler.class解析

本项目中使用的是@TableField(typeHandler = JacksonTypeHandler.class)

使用原生mybatis-plus可以正常得到结果

尝试添加druid来靠近接近配置相关配置

使用druid+mybatis-plus也可以正常得到结果

返现过程中出现的2个问题,需要确保main/java下的目录和test/java下的目录保持一致。

├── main
│   ├── java
│       └── redcrazyghost
│           └── querywarpperdemo
│               └── QueryWarpperDemoApplication.java
└─ test
   └── java
       └── redcrazyghost
           └── querywarpperdemo
               └── QueryWarpperTest.java

1.使用@SpringBootTest

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

  at org.springframework.util.Assert.state(Assert.java:76)
  at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:236)
  at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:152)
  at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:393)
  at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:309)
  at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:262)
  at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:107)
  at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:102)

2.使用@SpringBootTest(classes= redcrazyghost.querywarpperdemo.QueryWarpperTest.class)

java.lang.IllegalStateException: Failed to load ApplicationContext

  at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
  at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
  at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
  at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
  at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43)
  at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:248)
  at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138)

querywarpperdemo's People

Contributors

redcrazyghost avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.