遇到分表后的场景,访问不同表的数据(#{} 、${})

**首先场景是这样的,公司分表后把一张表的数据分成表后面接年份_季度的方式,于是就要使用动态的表名,也就是将表名当成一个参数传给mybatis。

  1. 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 &lt;= 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 &lt;= 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 &lt;= 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值