Exception in thread “main” java.lang.ExceptionInInitializerError

Error querying database. Cause: java.sql.SQLSyntaxErrorException: 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 ‘}’ at line 1

select _* _from emp where eid=#{eid} and ename = #{ename}
#{ename}打成了{ename}

Cause: java.sql.SQLSyntaxErrorException: Unknown column ‘emp.depid’ in ‘on clause’

数据库中的列名打错

使用Mybatis时程序执行成功,数据库却没有改变

因为mybatis默认不是自动提交事务的, 所以其实没有修改数据库,
刚刚新增完后立即返回的结果,是从mybatis为了提高性能设置的缓存里读取的,不是从数据库读取的
解决的办法有如下两种:
1.在openSession() 的括号里写true, 设定自动提交事务,
2.在代码中加入sqlSession.commit()

Cannot resolve symbol ‘ibatis’

加入import如下:

  1. import org.apache.ibatis.io.Resources;
  2. import org.apache.ibatis.session.SqlSession;
  3. import org.apache.ibatis.session.SqlSessionFactory;
  4. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  5. import org.apache.ibatis.io.Resources;

同时重新导入包
image.png

org.apache.ibatis.binding.BindingException: Type interface resources.powernode.dao.EmpDao is not known to the MapperRegistry.

出现这个错误的原因是mapper映射文件的namespace和接口类名不一致!所以注意namespace和接口名要一致。
**

org.apache.ibatis.exceptions.PersistenceException:

Error querying database. Cause: org.apache.ibatis.binding.BindingException: Parameter ‘0’ not found. Available parameters are [id, param1]

Cause: org.apache.ibatis.binding.BindingException: Parameter ‘0’ not found. Available parameters are [id, param1]

sql语句和emp实体类的名字没有对应上。

org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 4

注意: Map 作为接口返回值, sql 语句的查询结果最多只能有一条记录(可以没有)。 大于一条记录是错误。

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): resources.powernode.dao.EmpDao.SelectEmp_DepAll

在select语句中只配置了resultType,没有配置parameterType

org.apache.ibatis.exceptions.PersistenceException:

Error updating database. Cause: java.sql.SQLSyntaxErrorException: 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 ‘where eid=20181322

     ; 
        update emp set
        ename='小' at line 5

The error may exist in resources/powernode/dao/EmpDao.xml

The error may involve defaultParameterMap

The error occurred while setting parameters

SQL: update emp set ename=?, age = ?, where eid=? ; update emp set ename=?, age = ?, where eid=?

Cause: java.sql.SQLSyntaxErrorException: 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 ‘where eid=20181322

     ; 
        update emp set
        ename='小' at line 5

需要在mybatis.xml文件中添加权限设置

<property name="url" value="jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true&amp;allowMultiQueries=true"/>

设置外键后,没有实现级联删除
image.png
需要在设置外键的时候给on delete选择cascade属性
_

使用bind进行模糊查询时,没有输出
数据库查匹配字符串的时候要用like,不能用=

    <select id="selBind" resultType="resources.powernode.domain.Emp" parameterType="resources.powernode.domain.Emp">
        <bind name="ename" value="'%'+ename+'%'"/>
//  原: select * from emp where ename = '${ename}'
        select * from emp where ename like '${ename}'
    </select>

There is no getter for property named ‘Dept’ in ‘class resources.powernode.domain.EmpDept’ ### The error may exist in resources/powernode/dao/EmpDao.xml

property对应的属性名打错,应和EmpDept保持一致,

1452 - Cannot add or update a child row: a foreign key constraint fails (ssm.dept, CONSTRAINT dept_ibfk_1 FOREIGN KEY (depid) REFERENCES emp (depid) ON DELETE CASCADE ON UPDATE CASCADE), Time: 0.000000s

错误的给dept表也添加了关于emp表的外键,导致两个表数据不一致。含有约束的表中所有id都应该在主表中可以找到。

org.apache.ibatis.binding.BindingException: Type interface resources.powernode.dao.EmpDao is not known to the MapperRegistry.

核心配置文件 MyBatis.xml 里映射器配置文件路径没有写正确。
解决办法:将mappers里的路径名称修改正确即可

Operator ‘%’ cannot be applied to ‘java.lang.String’, ‘java.lang.String’

重复使用“”,将内部的“”改为‘’即可

" select * from emp where ename like '%' #{ename} '%' "