学Mybatis偶遇注解实现映射器,mybatis连接数据库时遇到时区问题和Mapped Statements collection does not contain value for xxx

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>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值