SQL语句在mybatis中加/**/会报错可以使用--

SQL语句在mybatis中加/**/会报错可以使用  "<!---->"

在这里插入图片描述

在这里插入图片描述
错误原因

    <!--  查询每个商户的全部商铺-->
    <select id="queryShopList" resultMap="shopMap">
        SELECT
        s.shop_id,
        s.shop_name, s.shop_desc,
        s.shop_addr,
        s.phone,
        s.shop_img, s.priority,
        s.create_time,
        s.last_edit_time,
        s.enable_status, s.advice,
        a.area_id,
        a.area_name,
        sc.shop_category_id,
        sc.shop_category_name
        FROM
        tb_shop s,
        tb_area a,
        tb_shop_category sc
        <where>
            /* 根据商店的分类查询所有的商店*/
            <if
                    test="shopCondition.shopCategory != null and
                    shopCondition.shopCategory.shopCategoryId != null">
                AND
                s.shop_category_id=#{shopCondition.shopCategory.shopCategoryId}
            </if>
            /**/
            <if
                    test="shopCondition.shopCategory != null and
                    shopCondition.shopCategory.parent != null and
                    shopCondition.shopCategory.parent.shopCategoryId != null">
                AND
                /*联系另外一张表查询相对应的信息*/
                /*查询所有的二级目录分类Id*/
                s.shop_category_id in
                (select shop_category_id from
                tb_shop_category
                WHERE
                /*根据商店分类的二级目录的父类即上一级目录的分类Id查询二级分类目录下的分类Id*/
                /*  即查询的是某个一级分类下所有的店铺*/
                parent_id=#{shopCondition.shopCategory.parent.shopCategoryId})
            </if>
            <if
                    test="shopCondition.area != null and
                    shopCondition.area.areaId != null">
                AND
                s.area_id=#{shopCondition.area.areaId}
            </if>
            <if test="shopCondition.shopName != null">
                AND
                s.shop_name like '%${shopCondition.shopName}%'
            </if>
            <if test="shopCondition.enableStatus != null">
                AND
                s.enable_status=#{shopCondition.enableStatus}
            </if>
            <if
                    test="shopCondition.owner != null
                    and shopCondition.owner.userId != null">
                AND
                s.owner_id=#{shopCondition.owner.userId}
            </if>
            AND
            /* 多表关联*/
            s.area_id=a.area_id
            AND
            s.shop_category_id=sc.shop_category_id
        </where>
        ORDER BY
        s.priority DESC
        <!--  分页条件-->
        LIMIT #{rowIndex},#{pageSize}
    </select>

修改后:

<select id="queryShopList" resultMap="shopMap">
        SELECT
        s.shop_id,
        s.shop_name, s.shop_desc,
        s.shop_addr,
        s.phone,
        s.shop_img, s.priority,
        s.create_time,
        s.last_edit_time,
        s.enable_status, s.advice,
        a.area_id,
        a.area_name,
        sc.shop_category_id,
        sc.shop_category_name
        FROM
        tb_shop s,
        tb_area a,
        tb_shop_category sc
        <where>
            <!--   根据商店的分类查询所有的商店-->
            <!--  联系另外一张表查询相对应的信息-->
            <!--  查询所有的二级目录分类Id-->
            <!--  根据商店分类的二级目录的父类即上一级目录的分类Id查询二级分类目录下的分类Id-->
            <!--  即查询的是某个一级分类下所有的店铺-->
             <!--  多表关联-->
             <!--  分页条件-->
            <if
                    test="shopCondition.shopCategory != null and
                    shopCondition.shopCategory.shopCategoryId != null">
                AND
                s.shop_category_id=#{shopCondition.shopCategory.shopCategoryId}
            </if>

            <if
                    test="shopCondition.shopCategory != null and
                    shopCondition.shopCategory.parent != null and
                    shopCondition.shopCategory.parent.shopCategoryId != null">
                AND

                s.shop_category_id in
                (select shop_category_id from
                tb_shop_category
                WHERE

                parent_id=#{shopCondition.shopCategory.parent.shopCategoryId})
            </if>
            <if
                    test="shopCondition.area != null and
                    shopCondition.area.areaId != null">
                AND
                s.area_id=#{shopCondition.area.areaId}
            </if>
            <if test="shopCondition.shopName != null">
                AND
                s.shop_name like '%${shopCondition.shopName}%'
            </if>
            <if test="shopCondition.enableStatus != null">
                AND
                s.enable_status=#{shopCondition.enableStatus}
            </if>
            <if
                    test="shopCondition.owner != null
                    and shopCondition.owner.userId != null">
                AND
                s.owner_id=#{shopCondition.owner.userId}
            </if>
            AND

            s.area_id=a.area_id
            AND
            s.shop_category_id=sc.shop_category_id
        </where>
        ORDER BY
        s.priority DESC

        LIMIT #{rowIndex},#{pageSize}
    </select>

在这里插入图片描述

2019-01-16 20:01:38.906 [main] DEBUG com.imooc.o2o.dao.ShopDao.queryShopList - ==>  Preparing: SELECT s.shop_id, s.shop_name, s.shop_desc, s.shop_addr, s.phone, s.shop_img, s.priority, s.create_time, s.last_edit_time, s.enable_status, s.advice, a.area_id, a.area_name, sc.shop_category_id, sc.shop_category_name FROM tb_shop s, tb_area a, tb_shop_category sc WHERE /* 根据商店的分类查询所有的商店*/ /**/ AND /* 多表关联*/ s.area_id=a.area_id AND s.shop_category_id=sc.shop_category_id ORDER BY s.priority DESC LIMIT ?,? 
Wed Jan 16 20:01:38 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Wed Jan 16 20:01:38 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Wed Jan 16 20:01:38 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Wed Jan 16 20:01:38 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
2019-01-16 20:01:38.935 [main] DEBUG com.imooc.o2o.dao.ShopDao.queryShopList - ==> Parameters: 0(Integer), 5(Integer)
2019-01-16 20:01:38.953 [main] INFO  o.s.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
2019-01-16 20:01:38.970 [main] INFO  o.s.jdbc.support.SQLErrorCodesFactory - SQLErrorCodes loaded: [DB2, Derby, H2, HDB, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]

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 'AND
            /* 多表关联*/
            s.area_id=a.area_id
            AN' at line 26
### The error may exist in file [D:\eclipse x64\o2o\target\classes\mapper\ShopDao.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT         s.shop_id,         s.shop_name, s.shop_desc,         s.shop_addr,         s.phone,         s.shop_img, s.priority,         s.create_time,         s.last_edit_time,         s.enable_status, s.advice,         a.area_id,         a.area_name,         sc.shop_category_id,         sc.shop_category_name         FROM         tb_shop s,         tb_area a,         tb_shop_category sc          WHERE /* 根据商店的分类查询所有的商店*/                          /**/                                                                              AND             /* 多表关联*/             s.area_id=a.area_id             AND             s.shop_category_id=sc.shop_category_id          ORDER BY         s.priority 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 'AND
            /* 多表关联*/
            s.area_id=a.area_id
            AN' at line 26
; 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 'AND
            /* 多表关联*/
            s.area_id=a.area_id
            AN' at line 26

	at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:234)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
	at com.sun.proxy.$Proxy21.selectList(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
	at com.sun.proxy.$Proxy32.queryShopList(Unknown Source)
	at com.imooc.o2o.dao.ShopDaoTest.testQueryShopListAndCount(ShopDaoTest.java:97)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
	at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: 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 'AND
            /* 多表关联*/
            s.area_id=a.area_id
            AN' at line 26
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
	at com.mysql.jdbc.Util.getInstance(Util.java:408)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3978)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3914)

猜你喜欢

转载自blog.csdn.net/qq_42664961/article/details/86514022
今日推荐