1、mybatis javaweb java.lang.ClassNotFoundException: org.apache.ibatis.io.Resources
之前测试以java application形式运行结果正常,而在服务器上运行结果异常,推测应该将mybatis这个jar包复制到WEB-INF下的lib文件夹下。经测试,异常消失!
2、Cause: java.sql.SQLException: Unknown system variable 'tx_isolation’
1、改用mysql-connector8.0.20(的确是版本的原因,因为mysql8之后查看事务由原来的SELECT @@tx_isolation改为了SELECT @@transaction_isolation);
2、修改配置文件:value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC&charsetEncoding=utf-8" />
XXX:很重要的一点是,都不起作用,虽然maven里修改了版本号,更新完依赖里也成了8.0.20,但依然不行
必须要在web-inf里新建lib文件夹(maven项目里说是不用lib,看来不得不建啊),然后把8.0.20的jar包copy进去再build即可成功
3、org.apache.ibatis.binding.BindingException: Type interface com.hywl.dal.EmployeeDAL is not known to the MapperRegistry.
1、xml中关联的那个类库不正确(<mapper namespace="com.hywl.dal.EmployeeDAL">)namespace值不正确
2、还要注意一下mybatis.xml中的关联局部配置文件:
<!-- 可以通过mapper标签将主配置文件Mybatis.xml与局部配置文件关联起来 resource:局部配置文件的路径 -->
<!--<mappers>
<mapper resource="com/hywl/dao/impl/DepartmentDaoImpl.xml" />
<mapper resource="com/hywl/dao/impl/EmployeeDaoImpl.xml" />
</mappers> -->
<!-- 如有100个xml局部子文件,则须写100行,故修改其为: -->
<mappers> <package name="com/hywl/dal"/> </mappers>
4、报错:某字段找不到,显示出某个字段(empName,param1)这样的
1、在接口函数的参数中加上如:int getAllEmpAndAccAndDepCount(@Param(“emp”) Employee emp);
2、XML中的SQL这样修改:其它不变
<select id="getAllEmpAndAccAndDepCount" resultType="Integer" parameterType="com.hywl.entity.Employee">
<![CDATA[
select count(*) from emp e
inner join dep d on e.depid=d.depid
left join account a
on e.empId=a.empId
where 1 = 1
]]>
<!-- <where> -->
<if test="emp.empName != '' and emp.empName != null">and e.empName like concat(#{emp.empName},'%')
</if>
<if test="emp.sex != '' and emp.sex != null">and sex = #{emp.sex}
</if>
<if test="emp.status != -1 and emp.status != null">and status = #{emp.status}
</if>
<!-- 实体类、上面的映射表里都重新定义了一个did,值其实就是depId,但不定义总是此变量模棱两可 -->
<if test="emp.did != '' and emp.did != null and emp.did != -1">and e.`depId` = #{emp.did}
</if>
</select>
5、报错:java.lang.NumberFormatException: For input string: "depId"
1、应该是String转int或空转Int报错;
2、我的错是:1)点击部门,显示此部门下的员工,而员工list也在dep的实体类里,那返回应该是dep的实体类,我返回成了dep的list
2)xml中的select的id和一对多的resultMap的id一样了,且select中的resultMap写成了resultType
3、正确的引用顺序为:
1)普通调用:controller->interface->xml->查找id为interface某函数名
2)集合数据调用:controller->interface->xml->查找id为interface某函数名->查找此函数名的resultMap(此时必有一个id与resultMap同名的集合)