使用MyBatis中遇到的问题及总结

遇到的问题

有如下报错

org.springframework.dao.DataIntegrityViolationException:
### Error querying database. Cause: java.sql.SQLException: ORA-00911: 无效字符

### The error may exist in file [D:\Project_1\reactiveDispose\dist\classes\est\e6300\reactivedispose\dao\impl\MonitorRateDaoImpl.xml]
### The error may involve est.e6300.reactivedispose.dao.impl.MonitorRateDaoImpl.monitorNumForDline-Inline
### The error occurred while setting parameters
### SQL: select count(*) from est_pw_device device left join est_cim_disttrans disttrans on disttrans.id = device.disttransid left join est_cim_dline dline on dline.id = disttrans.dlineid inner join est_pw_terminal terminal on terminal.id = device.terminalid where dline.id = ? and (device.status = 1 or device.status = 4) and device.valid = 1;
### Cause: java.sql.SQLException: ORA-00911: 无效字符

; SQL []; ORA-00911: 无效字符
; nested exception is java.sql.SQLException: ORA-00911: 无效字符

解决办法:去掉Dao的xml文件中SQL语句结尾的英文分号";"

总结

MyBatis中Dao的xml文件注意事项

1. 一般占位符$或#不是问题的原因,当然,推荐使用#

2. xml中设置的parameterType是java.lang.Long,依然可以传入Map,且功能正常

3. xml中结尾不要带";"

4. 如果返回的结果是多个,用List接收,设置的resultType应该是元素的类型,而不是集合的类型

The "MyBatis-3-User-Guide" says: resultType: The fully qualified class name or alias for the expected type that will be returned from this statement. Note that in the case of collections, this should be the type that the collection contains, not the type of the collection itself. Use resultType OR resultMap, not both.

5. .xml中使用标签复用SQL代码的方式

没用标签时的SQL

<select id="findUserById" resultType="com.lcy.entity.User">
    SELECT
     id,username,password,age,phone,email
    FROM t_user
    WHERE
    id=#{id}
</select>

使用标签后的SQL

<sql id="userColumns"> id,username,password,age,phone,email </sql>

<select id="findUserById" resultType="com.lcy.entity.User">
    SELECT
    <include refid="userColumns"></include>
    FROM t_user
    WHERE
    id=#{id}
</select>

猜你喜欢

转载自www.cnblogs.com/lichuanyan/p/12131701.html
今日推荐