SQL String cannot be empty

错误提示

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.sql.SQLException: SQL String cannot be empty
### The error may exist in mapper/UserMapper.xml
### The error may involve mapper.UserMapper.selectUserById
### The error occurred while executing a query
### SQL: 
### Cause: java.sql.SQLException: SQL String cannot be empty
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:76)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:87)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:57)
    at com.sun.proxy.$Proxy5.selectUserById(Unknown Source)
    at test.ManyToManyTest2.main(ManyToManyTest2.java:16)
    ......

分析

mapper/UserMapper.xml文件中id为mapper.UserMapper.selectUserById的这条select语句,忘了写SQL语句:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.UserMapper">
    <select id="selectUserById" parameterType="int"
        resultMap="userMap">
    </select>
    <resultMap type="domain.User" id="userMap">
        <id property="id" column="id" />
        <result property="userName" column="username" />
        <result property="loginName" column="loginname" />
        <result property="password" column="password" />
        <result property="phone" column="phone" />
        <result property="address" column="address" />
        <collection property="orders" javaType="ArrayList"
            ofType="domain.Order" column="id" select="mapper.OrderMapper.selectOrdersByUserId">
            <id property="id" column="id" />
            <result property="code" column="code" />
            <result property="total" column="total" />
        </collection>
    </resultMap>
</mapper>

解决方案

SQL语句写上即可:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.UserMapper">
    <select id="selectUserById" parameterType="int"
        resultMap="userMap">
        select * from tb_user where id=#{id}
    </select>
    <resultMap type="domain.User" id="userMap">
        <id property="id" column="id" />
        <result property="userName" column="username" />
        <result property="loginName" column="loginname" />
        <result property="password" column="password" />
        <result property="phone" column="phone" />
        <result property="address" column="address" />
        <collection property="orders" javaType="ArrayList"
            ofType="domain.Order" column="id" select="mapper.OrderMapper.selectOrdersByUserId">
            <id property="id" column="id" />
            <result property="code" column="code" />
            <result property="total" column="total" />
        </collection>
    </resultMap>
</mapper>