1.注解实现映射器
遇到的问题:
### Error building SqlSession.
### The error may exist in com/mybatis/mapper/UserMapper2
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/mybatis/mapper/UserMapper2
在mybatis的配置文件中映射器:
<mapper resource="com/mybatis/mapper/UserMapper2"/>
解决办法:
用类名的方式引入映射器:
<mapper class="com.mybatis.mapper.UserMapper2"
2.mybatis连接数据库时遇到时区问题
<property name="url" value="jdbc:mysql://localhost:3306/mavenweb?useUnicode=true**;**characterEncoding=utf8**;**serverTimezone=GMT%2B8"/>
在mybatis 的基础配置文件中value中实体要用 ; 间隔,上述的连接池的URL不能获取到时区的设置,所以会报错
解决:①更改数据库的配置文件中的时区
②只
<property name="url" value="jdbc:mysql://localhost:3306/mavenweb?serverTimezone=Asia/Shanghai"/>
3.创建映射文件后执行SQL时: Mapped Statements collection does not contain value for xxx.xxx
问题是在映射文件中仔细看看mapper中的namesapce,resultType,id是否正确,还有sqlSessin输入的参数是否和映射文件中属性相对应.
SqlSession执行映射文件中定义的SQL:
比如:
MyUser mu = ss.selectOne(
"com.mybatis.mapper.UserMapper.selectUserById", 1);
System.out.println(mu);
/*
* com.mybatis.mapper.UserMapper.selectUserById 为 UserMapper.xml
* 中的命名空间+select 的 id
*/
在xml中id的作用就是定位sql语句
<select id="selectUserById" parameterType="Integer"
resultType="com.mybatis.po.MyUser">
select * from user where uid = #{uid}
</select>