联表查询,两张表,一张是table表,一张db表,根据数据库的名字查其下属的所有表。
遇到mybatis错误:There is no getter for property named ‘xxx’ in ‘class java.lang.String’
首先在DAO层接口加上@Param(“dbName”)
List<TkvTable> selectByDBName(@Param("dbName") String dbName);
SQL语句
<select id="selectByDBName" resultType="TkvTable" parameterType="String">
select
t.id,
t.name,
t.ctime,
t.uptime,
t.cuser,
t.upuser,
t.item_count,
t.database_id,
d.name as db_name
from
tkv_table t INNER JOIN tkv_database d ON t.database_id=d.id
where 1=1
<if test="dbName != null and dbName != ''">
and d.name like #{dbName}
</if>
</select>
<if test="dbName != null and dbName != ''">
and d.name like #{dbName}
</if>
这里的dbName要和接口里的参数相对应,要注意是d.name不是name
再举个例子:
List queryByRangeOfTime(@Param(“minTime”) Long min, @Param(“maxTime”) Long max);
<select id="queryByRangeOfTime" parameterType="Long" resultType="NoiseMessage">
select * from noise_message
where collect_time >= #{minTime} and collect_time <![CDATA[ <= ]]> #{maxTime}
order by collect_time desc
</select>
注意Param(“???”)的参数和SQL语言参数一致
本文详细解析了在使用MyBatis进行联表查询时遇到的常见错误:找不到指定属性的getter方法。通过具体示例,介绍了如何正确使用@Param注解来匹配SQL语句中的参数,确保联表查询的正确执行。
4万+

被折叠的 条评论
为什么被折叠?



