我以前是仅仅分页,排序是手动赋值的,没有问题:
接口
public List<FuZhuang> showalldescdesc1(@Param("offset") int offset, @Param("limit") int limit);
sql语句
<select id="showalldescdesc1" resultType="FuZhuang" >
SELECT * from fuzhuang order by price desc,yuexiaoliang desc LIMIT #{offset},#{limit};
</select
但是现在想先排序时传入参数(asc、desc):
接口
public List<FuZhuang> showalldescdesc1(@Param("offset") int offset, @Param("limit") int limit,@Param("str") String str);
sql语句
<select id="showalldescdesc1" resultType="FuZhuang" >
SELECT * from fuzhuang order by price #{str},yuexiaoliang desc LIMIT #{offset},#{limit};
</select>
结果报错了:
org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. 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 ''desc',yuexiaoliang desc LIMIT 6,6' at line 1
### The error may exist in mybatis/mappers/FuZhuangMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT * from fuzhuang order by price ?,yuexiaoliang desc LIMIT ?,?;
### 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 ''desc',yuexiaoliang desc LIMIT 6,6' 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 ''desc',yuexiaoliang desc LIMIT 6,6' at line 1
org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:233)
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:71)
org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:365)
$Proxy10.selectList(Unknown Source)
org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:195)
org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:124)
org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:90)
是什么原因啊?难道asc和desc不能以变量的形式传参吗?不会吧?该怎么解决啊!!!求助!!
------最佳解决方案--------------------------------------------------------
<select id="showalldescdesc1" resultType="FuZhuang" >
SELECT * FROM fuzhuang
<if test="ORDER == 'desc'">
ORDER BY price DESC
</if>
<if test="ORDER == 'asc'">