mybatis入门使用5:传入表名作参数

时间:2022-09-23 05:14:45

在使用mybatis时有时会使用表名作参数:

1、动态传入表名做参数, 在xml 中 加入 statementType="STATEMENT",使用$ ${tableName},
2、此时需要使用map或者对象才能传入参数,单个参数提示没有get/set

<select id="selectCount" resultType="java.lang.Integer" parameterType="java.lang.String" statementType="STATEMENT">
select
Count(1)
from ${tableName}
</select>


package com.lls.test;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.lls.mapper.EmployeeMapper;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:config/spring-mybatis.xml" })
public class TableNameParamTest {

private static final Logger LOGGER = LoggerFactory.getLogger(TableNameParamTest.class);

@Autowired
private EmployeeMapper employeeMapper;

@Test
public void tableNameParamTest() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("tableName", "t_employee");
int count = employeeMapper.selectCount(map);
LOGGER.info("count " + count);
}
}

代码文档:http://download.csdn.net/download/lanlianhua_luffy/9869769