第一次使用Mybatis踩过的那些坑

org.apache.ibatis.binding.BindingException: Type interface com.test.dao.UserDao is not known to the MapperRegistry.

没有注册映射。需要在Mybatis-config.xml里面注册一下
在这里插入图片描述

Could not find resource com/test/dao/UserMapper.xml

org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in com/test/dao/UserMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/test/dao/UserMapper.xml

在这里插入图片描述

Cause: java.io.IOException: Could not find resource com/test/dao/UserMapper.xml

org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in com/test/dao/UserMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/test/dao/UserMapper.xml

需要父工程的pom.xml文件中配置一下resources 防止资源导出失败

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>

            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 2 字节的 UTF-8 序列的字节 2 无效。

org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in com/test/dao/UserMapper.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 2 字节的 UTF-8 序列的字节 2 无效。

出现此问题按一下方法解决
这是因为

最最最最重要的问题是记得清空一下IDEA的缓存和重载一下Maven

在这里插入图片描述
以及这个
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面我将为您介绍 MyBatis使用MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。在使用 MyBatis 进行开发时,我们需要先进行以下几个步骤: 1. 引入 MyBatis 的依赖 在项目的 pom.xml 文件中,添加以下依赖: ``` <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> <!-- 其他依赖项 --> </dependencies> ``` 2. 配置 MyBatis 在项目中创建一个 mybatis-config.xml 文件,用来配置 MyBatis 的相关属性和插件。具体配置可以参考官方文档:https://mybatis.org/mybatis-3/zh/configuration.html 3. 定义数据源 在项目中定义数据源,这里以使用 MySQL 为例: ``` <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> ``` 4. 定义 Mapper 接口 在项目中定义 Mapper 接口,用于操作数据库。Mapper 接口可以使用注解或 XML 进行配置,这里以 XML 配置为例: ``` <!-- UserMapper.xml --> <mapper namespace="com.example.dao.UserMapper"> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultType="com.example.model.User"> select * from user where id = #{id} </select> <insert id="insert" parameterType="com.example.model.User" useGeneratedKeys="true" keyProperty="id"> insert into user(name, age) values (#{name}, #{age}) </insert> </mapper> ``` 5. 使用 MyBatis使用 MyBatis 进行开发时,我们可以通过以下代码来获取 Mapper 接口实例和执行 SQL: ``` SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.selectByPrimaryKey(1); sqlSession.close(); ``` 以上就是 MyBatis 的基本使用方法,当然还有更多高级功能,可以参考官方文档进行学习。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值