文章目录
1.使用Resources获取文件信息是,显示getResourceAsStream不是Resources的方法,原因导包错误。
Resources.getResourceAsStream(resource)
2、mybatis模糊查询无结果(logj日志显示查询语句正确,但结果与mysql查询不一致)
解决方法:
修改数据库连接地址:
url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
其中,characterEncoding=UTF-8必须写在第一位
3、mybatis使用properties导入数据库信息,mysql.username出现数据库拒绝访问,
结果是,db.properties文件中写的root后有一个空格,看不见,鼠标移上去发现的,哭晕
4.mybatis 大于小于等于的表示
小于:< 用 <表示不可去掉;号
大于:> 用 >表示不可去掉;号
5、mybatis 一对多,collection属性,查询结果没有去重的原因
例如一个学生(student)对应多门课程(class)
在select中,必须包含student的主键(不包含student查询的class信息不去重),和class的主键(不包含查询的class信息为空)
不包含dep表的主键d_id,结果不去重
2019-07-10 19:38:48 [信息] > Preparing: SELECT dep.d_name,student.s_id,student.s_name FROM dep,student where dep.d_id = student.sd_id;
2019-07-10 19:38:48 [信息] > Parameters:
2019-07-10 19:38:48 [信息] < Columns: d_name, s_id, s_name
2019-07-10 19:38:48 [信息] < Row: 文学院, 1, 周杰伦
2019-07-10 19:38:48 [信息] <== Row: 文学院, 2, 王力宏
2019-07-10 19:38:48 [信息] <== Row: 文学院, 3, 刘亦菲
2019-07-10 19:38:48 [信息] <== Row: 文学院, 4, 狄龙
2019-07-10 19:38:48 [信息] <== Row: 法学院, 5, 周芳芳
2019-07-10 19:38:48 [信息] <== Row: 法学院, 6, 胡远
2019-07-10 19:38:48 [信息] <== Total: 6
结果数目:6
包含d_id,结果去重
2019-07-10 19:40:25 [信息] > Preparing: SELECT dep.d_id,dep.d_name,student.s_id,student.s_name FROM dep,student where dep.d_id = student.sd_id;
2019-07-10 19:40:25 [信息] > Parameters:
2019-07-10 19:40:25 [信息] < Columns: d_id, d_name, s_id, s_name
2019-07-10 19:40:25 [信息] < Row: 2, 文学院, 1, 周杰伦
2019-07-10 19:40:25 [信息] <== Row: 2, 文学院, 2, 王力宏
2019-07-10 19:40:25 [信息] <== Row: 2, 文学院, 3, 刘亦菲
2019-07-10 19:40:25 [信息] <== Row: 2, 文学院, 4, 狄龙
2019-07-10 19:40:25 [信息] <== Row: 3, 法学院, 5, 周芳芳
2019-07-10 19:40:25 [信息] <== Row: 3, 法学院, 6, 胡远
2019-07-10 19:40:25 [信息] <== Total: 6
结果数目:2
6、mybatis sqlsessionFacotorybuilder()错误
The type org.apache.ibatis.session.Configuration cannot be resolved. It is indirectly referenced
解决方法,更改mybatis版本到低版本
7、mybatis显示执行日志
直接配置在mybatis.xml也可以
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
需要在maven导入下面三个依赖,不能只导入log4j核心依赖包,否则不显示日志
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
<version>1.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.13</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.13</version>
</dependency>
8、mybatis插入操作,log4j显示正确,数据库却没有数据
需要在数据库关闭之前,加入
sqlsession.commit();
提交事务