**首先场景是这样的,公司分表后把一张表的数据分成表后面接年份_季度的方式,于是就要使用动态的表名,也就是将表名当成一个参数传给mybatis。
- controller代码
String table = "";
if("0".equals(delFlag)) {
table= "tb_plan_result_detail";
}else if("1".equals(delFlag)){
table= "tb_plan_result_detail_history";
}
planResultDeatil.setTable(table);
List<PlanResultDeatil> list = planResultDeatilService.findPlanResultDetailInfo(errorTypeList.get(x));
当del_flag 为 “0” 时,使用 tb_plan_result_detail 表,当del_flag 为 “1” 时,使用 tb_plan_result_detail _history表
2.接口代码
public List<PlanResultDeatil> findPlanResultDetailInfo(PlanResultDeatil planResultDeatil);
3.mybatis 代码
<select id="findPlanResultDetailInfo" resultType="PlanResultDeatil" statementType="STATEMENT">
select * from(
SELECT a.error_detail as "errorDetail",
a.only_id AS "onlyId"
from ${table} a
<where>
<if test="resultId != null and resultId != ''">
a.result_id = '${resultId}'
</if>
<if test="tableName != null and tableName != ''">
AND a.tablename ='${tableName}'
</if>
<if test="fieldName != null and fieldName != ''">
AND a.fieldname = '${fieldName}'
</if>
<if test="ruleType != null and ruleType != ''">
AND a.rule_type = '${ruleType}'
</if>
<if test="errorType != null and errorType != ''">
AND a.error_type = '${errorType}'
</if>
</where>
ORDER BY a.tablename,a.fieldname
)where rownum <= 3
</select>
或者
<select id="findPlanResultDetailInfo" resultType="PlanResultDeatil">
select * from(
SELECT a.error_detail as "errorDetail",
a.only_id AS "onlyId"
from ${table} a
<where>
<if test="resultId != null and resultId != ''">
a.result_id = #{resultId}
</if>
<if test="tableName != null and tableName != ''">
AND a.tablename =#{tableName}
</if>
<if test="fieldName != null and fieldName != ''">
AND a.fieldname = #{fieldName}
</if>
<if test="ruleType != null and ruleType != ''">
AND a.rule_type = #{ruleType}
</if>
<if test="errorType != null and errorType != ''">
AND a.error_type = #{errorType}
</if>
</where>
ORDER BY a.tablename,a.fieldname
)where rownum <= 3
</select>
贴个图片,方便查看重点:
或者:
看了上面,大家就应该要猜到我等下要说什么了,我再贴下我之前的问题代码:
错误代码一:
1.所有参数都用 #{}
错误原因:错误的关键就在于 #{table} ,其实本质还是 #{} 和 ${} 的区别
简单理解就是 #{} 会给 你的实参加个 单引号 ,所以 表名就变成了 ‘tb_plan_result_detail’,然后你执行sql:
SELECT a.error_detail as “errorDetail”,a.only_id AS “onlyId” from ‘tb_plan_result_detail’ a 肯定就会出错了
所以表名 必须使用 ${table}。但是你若是 在条件参数中使用 $,就要加 单引号,因为 ${} 不会默认加个单引号,如上面的
a.tablename =’${tableName}’
<select id="findPlanResultDetailInfo" resultType="PlanResultDeatil">
select * from(
SELECT a.error_detail as "errorDetail",
a.only_id AS "onlyId"
from #{table} a
<where>
<if test="resultId != null and resultId != ''">
a.result_id = #{resultId}
</if>
<if test="tableName != null and tableName != ''">
AND a.tablename = #{tableName}
</if>
<if test="fieldName != null and fieldName != ''">
AND a.fieldname = #{fieldName}
</if>
<if test="ruleType != null and ruleType != ''">
AND a.rule_type = #{ruleType}
</if>
<if test="errorType != null and errorType != ''">
AND a.error_type = #{errorType}
</if>
</where>
ORDER BY a.tablename,a.fieldname
)where rownum <= 3
</select>
错误代码二:
这个错误是关于 接口的,直接看代码:
public List<PlanResultDeatil> findPlanResultDetailInfo(@Param("planResultDetail") PlanResultDeatil planResultDetail);
我加这个注解,planResultDetail , 但我 xml中,直接使用的是 planResultDetail类中它的属性,比如#{tableName},其实不使用
@Param(“planResultDetail”) 反而不会出错,使用了@Param(“planResultDetail”) ,就一定得在 xml中表明你使用的参数,不然xml无法识别它,因为你声明的是@Param(“planResultDetail”) ,你凭什么直接使用 #{tableName}。
正解就是 #{planResultDetail.tableName}
由于只想记录下这个问题,所以自己没有去贴自己的代码,找了个别人弄好的。地址:https://blog.csdn.net/qq_25221835/article/details/86711987