一:基于xml实现
dao层
@MapKey("executeSql")
List<Map<String,Object>> executeSql (@Param("sqlStr") String sqlStr);
mybatis的xml文件
<select id="executeSql" resultType="java.util.Map">
${sqlStr}
</select>
二:基于注解实现
dao层
@Select("${sqlStr}")
// @MapKey("executeSql")
List<Map<String,Object>> executeSql (@Param("sqlStr") String sqlStr);
controller层:
@GetMapping("executeSql")
public List<Map<String, Object>> executeSql(@RequestParam String sqlStr) {
List<Map<String, Object>> list = autoService.executeSql(sqlStr);
return list;
}
postman测试:
测试类:
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class Test {
@Autowired
private AutoMapper autoMapper ;
@Test
public void get1(){
String sqlStr= "select id,age,name from user where id = 1";
List<Map<String, Object>> list = publicMapper.executeSql(sqlStr);
}
Mybatis官方文档的对该注解的解释