MyBatis Dynamic Table name meets there is no getter for property named ' TableName ' in ' class java.lang.String ' __web base

Source: Internet
Author: User

(1) First mybatis The SQL statement that implements the dynamic table name

<select id= "GetData" resulttype= "Java.util.HashMap" parametertype= "String" >
        select * from ${tablename} ORDER BY Data_index Desc limit 0,1
    </select>

The SQL table name passed into the MVC controller, which enables you to query data in different tables.
(2) But don't want to, halfway off the chain

Org.mybatis.spring.MyBatisSystemException:nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ' TableName ' in ' Class java.lang.String ' at
    Org.mybatis.spring.MyBatisExceptionT Ranslator.translateexceptionifpossible (mybatisexceptiontranslator.java:77) at
    Org.mybatis.spring.sqlsessiontemplate$sqlsessioninterceptor.invoke (sqlsessiontemplate.java:446) at
    Com.sun.proxy. $Proxy 15.selectOne (Unknown Source) at
    Org.mybatis.spring.SqlSessionTemplate.selectOne ( sqlsessiontemplate.java:166) at
    Org.apache.ibatis.binding.MapperMethod.execute (mappermethod.java:82)
    at Org.apache.ibatis.binding.MapperProxy.invoke (mapperproxy.java:53) at
    com.sun.proxy. $Proxy 16.getData (Unknown Source) at
    TestCase.test.test (test.java:37)

There is no getter for property named ' TableName ' class java.lang.String ', meaning that the String class does not have an attribute named ' tablename '

(3) Reasons:
MyBatis defaults to the use of Ongl parsing parameters, so will automatically take the form of object tree String.tablename value, resulting in an error.

(4) Solution:
In the DAO layer interface, add the parameter description to the corresponding method, as follows:

MyBatis The SQL statement for the mapping file, as follows:


NOTE: Loading SQL statements in MyBatis can be divided into precompilation and precompilation, default precompilation, and Statementtype= "STATEMENT" as not precompiled, and must also use ${} to get incoming parameter values.

Reference blog: Mybtis statement properties of dynamic table names

MyBatis Dynamic incoming tablename– non-precompiled (STATEMENT)

(4) Implementation of the results, as follows: Test procedures: Package TestCase;

Import Java.util.Map;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.test.context.ContextConfiguration;

Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner; Import DAO.

Usermapper; @RunWith (Springjunit4classrunner.class) @ContextConfiguration (locations = "classpath:spring/
    Applicationcontext.xml ")//," Classpath:mapper/mapper.xml public class Test {@Autowired usermapper usermapper; @Test public void Test () {try {map<string, object> paramap = Usermapper.getparametersbymo
            Delid (1);
            For (Object Key:paraMap.keySet ()) {System.out.println (key+ "::" +paramap.get (key));
            String tablename = (string) paramap.get ("Para_url");


            System.out.println ("**************" +tablename+ "**************************"); Map<string, object> DAtamap = Usermapper.getdata (tablename);
            For (Object Key:dataMap.keySet ()) {System.out.println ("key=" +key+ ", value=" +datamap.get (key));
        } catch (Exception e) {//Todo:handle Exception e.printstacktrace ();
 }
    }
}

Console results:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.