(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: