版权声明:本文为博主原创文章,转载请说明出处 https://blog.csdn.net/u010002184/article/details/83512778
别人老说?不编译#编译,一直稀里糊涂的,不如自己试一下,其实说白了就是#是否编译成?再用传入的参数代替。其实把这些原理用很通俗的语言说给一个新手听,反而说明自己也理解了,而不是一直跟新手说区别就是是否编译......
--正确
WeatherPO weatherPO = weatherDao.queryByAreaIdAndAreaName("101101001", "忻州");
<select id="queryByAreaIdAndAreaName" resultType="WeatherPO">
select * from weather_table
where area_id=#{areaId} and area_name=#{areaName}
</select>
DEBUG: [ABC.queryByAreaIdAndAreaName:159] ==> Preparing: select * from weather_table where area_id=? and area_name=?
DEBUG: [ABC.queryByAreaIdAndAreaName:159] ==> Parameters: 101101001(String), 忻州(String)
DEBUG: [ABC.queryByAreaIdAndAreaName:159] <== Total: 1
--错误,如果使用字符串则需要加单引号
WeatherPO weatherPO = weatherDao.queryByAreaIdAndAreaName("101101001", "忻州");
<select id="queryByAreaIdAndAreaName" resultType="WeatherPO">
select * from weather_table
where area_id=#{areaId} and area_name=${areaName}
</select>
DEBUG: [ABC.queryByAreaIdAndAreaName:159] ==> Preparing: select * from weather_table where area_id=? and area_name=忻州
DEBUG: [ABC.queryByAreaIdAndAreaName:159] ==> Parameters: 101101001(String)
org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '忻州' in 'where clause'
### The error may exist in file [F:\weatherexpert\weather-expert\target\classes\mybatis\mapper\Weather.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select * from weather_table where area_id=? and area_name=忻州
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '忻州' in 'where clause'
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '忻州' in 'where clause'
--正确,如果使用字符串则需要加单引号
WeatherPO weatherPO = weatherDao.queryByAreaIdAndAreaName("101101001", "'忻州'");
<select id="queryByAreaIdAndAreaName" resultType="WeatherPO">
select * from weather_table
where area_id=#{areaId} and area_name=${areaName}
</select>
DEBUG: [ABC.queryByAreaIdAndAreaName:159] ==> Preparing: select * from weather_table where area_id=? and area_name='忻州'
DEBUG: [ABC.queryByAreaIdAndAreaName:159] ==> Parameters: 101101001(String)
DEBUG: [ABC.queryByAreaIdAndAreaName:159] <== Total: 1
--正确
List<WeatherPO> weatherPOList = weatherDao.queryByProvinceName("山西");
public List<WeatherPO> queryByProvinceName(@Param("provinceName") String provinceName);
<select id="queryByProvinceName" resultType="WeatherPO">
select * from weather_table
where province_name=#{provinceName}
</select>
DEBUG: [ABC.queryByProvinceName:159] ==> Preparing: select * from weather_table where province_name=?
DEBUG: [ABC.queryByProvinceName:159] ==> Parameters: 山西(String)
DEBUG: [ABC.queryByProvinceName:159] <== Total: 4
--正确
List<WeatherPO> weatherPOList = weatherDao.queryByProvinceName("山西","area_id");
public List<WeatherPO> queryByProvinceName(@Param("provinceName") String provinceName,@Param("areaId") String areaId);
<select id="queryByProvinceName" resultType="WeatherPO">
select * from weather_table
where province_name=#{provinceName}
order by ${areaId}
</select>
DEBUG: [ABC.queryByProvinceName:159] ==> Preparing: select * from weather_table where province_name=? order by area_id
DEBUG: [ABC.queryByProvinceName:159] ==> Parameters: 山西(String)
DEBUG: [ABC.queryByProvinceName:159] <== Total: 4
<==>select * from weather_table where province_name= '山西' order by area_id --正确
--正确
List<WeatherPO> weatherPOList = weatherDao.queryByProvinceName("山西","area_id");
public List<WeatherPO> queryByProvinceName(@Param("provinceName") String provinceName,@Param("areaId") String areaId);
<select id="queryByProvinceName" resultType="WeatherPO">
select * from weather_table
where province_name=#{provinceName}
order by #{areaId}
</select>
DEBUG: [ABC.queryByProvinceName:159] ==> Preparing: select * from weather_table where province_name=? order by ?
DEBUG: [ABC.queryByProvinceName:159] ==> Parameters: 山西(String), area_id(String)
DEBUG: [ABC.queryByProvinceName:159] <== Total: 4
<==>select * from weather_table where province_name= '山西' order by 'area_id' --正确
--正确
List<WeatherPO> weatherPOList = weatherDao.queryByTableName("weather_table");
<select id="queryByTableName" resultType="WeatherPO">
select * from ${tableName}
</select>
DEBUG: [ABC.queryByTableName:159] ==> Preparing: select * from weather_table
DEBUG: [ABC.queryByTableName:159] ==> Parameters:
DEBUG: [ABC.queryByTableName:159] <== Total: 4
--错误
List<WeatherPO> weatherPOList = weatherDao.queryByTableName("weather_table");
<select id="queryByTableName" resultType="WeatherPO">
select * from #{tableName}
</select>
### SQL: select * from ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near ''weather_table'' at line 1
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''weather_table'' at line 1
<==> select * from 'weather_table' --错误
--正确
<select id="queryById" resultType="WeatherPO">
select * from weather_table
where id = #{id}
</select>
DEBUG: [ABC.queryById:159] ==> Preparing: select * from weather_table where id = ?
DEBUG: [ABC.queryById:159] ==> Parameters: 1261(Long)
DEBUG: [ABC.queryById:159] <== Total: 1
--正确
<select id="queryById" resultType="WeatherPO">
select * from weather_table
where id = ${id}
</select>
DEBUG: [ABC.queryById:159] ==> Preparing: select * from weather_table where id = 1261
DEBUG: [ABC.queryById:159] ==> Parameters:
DEBUG: [ABC.queryById:159] <== Total: 1
总结:
SQL语句中传入字符串时(此时只是单纯的SQL的用法,不看Mybatis):
在where中使用需要带单引号 ,如:"张三"对应#{name} 或者 "'张三'" 对应${name};不带单引号会报错
作为表名使用不能带单引号,如:"weather_table" 对应${tableName}
在order by中使用是否带单引号均可,如:"name"对应#{name} 或者 "name" 对应${name} 或者 "'name'" 对应${name}
#{}:传入字符串时,会自动给内容加单引号;
解析成?,在parameter中给出
在where中使用不报错;
传入表名时报错;
在order by中使用不报错
传入数字时,直接使用即可
${}:传入字符串时,不自动给内容加单引号;
不会解析,传入什么就使用什么
如果在where中使用会报错,需要传入带单引号的字符串;
传入表名时不报错;
在order by中使用不报错
传入数字时,直接使用即可