mybatis 动态更换表名,用以动态访问不同表的数据(#{} 、${})

7 篇文章 1 订阅

1月快结束了啦~~~~~~ 时间好快~~~

首先说下使用前景:是这样的,这周有个任务,其中有个部分就是根据一个标识,去判断使用哪一张表的数据,于是就要使用动态的表名,也就是将表名当成一个参数传给mybatis。一开始,认为很简单,不就是传参吗?三下五除二,我就写了出来,结果当然是错误的。后面花了将一下午才将这个sql写出来。

首先,分享成功的代码:

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}

 

总结:

其实,这些都是非常基础的问题,但由于以习惯去认识问题,浪费了很多时间。要多总结,才能多多进步啊~

 

 

 

 

 

  • 15
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Mybatis 是一种流行的 Java 持久层框架,用于与数据库进行交互。在实际开发中,有时需要根据不同的条件动态拼接表名。以下是使用 Mybatis 实现动态拼接表名的方法。 首先,在 Mybatis 的 SQL 映射文件中,我们可以使用动态 SQL 来根据条件拼接表名。例如,使用 `<choose>` 标签和 `<when>` 标签来判断条件,并根据条件拼接不同表名,如下所示: ```xml <select id="selectData" parameterType="Map" resultMap="resultMap"> SELECT * FROM <choose> <when test="tableName == 'table1'"> table1 </when> <when test="tableName == 'table2'"> table2 </when> <otherwise> table3 </otherwise> </choose> WHERE id = #{id} </select> ``` 这里的 `tableName` 是一个传递给 SQL 的参数,用于判断选择哪个表名进行拼接。 接下来,在 Java 代码中,我们只需将正确的表名传递给 Mybatis 的 SQL 语句执行方法。例如,使用 `<map>` 来包装参数,并将 `tableName` 参数传递给 SQL,如下所示: ```java Map<String, Object> paramMap = new HashMap<>(); paramMap.put("id", 1); paramMap.put("tableName", "table1"); List<Map<String, Object>> result = sqlSession.selectList("selectData", paramMap); ``` 在上述代码中,我们将 `tableName` 参数设置为 "table1",这样 Mybatis 就会动态拼接 `table1` 表名,并执行相应的 SQL 查询。 通过以上方法,我们可以实现根据不同的条件动态拼接表名。这在某些场景下非常实用,例如在多租户系统中,根据不同的租户拼接不同表名

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一彡十

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值