最近在学习mybatis,错误: 编译器找不到*mapper.xml,更准确的说是找不到实体类,挺低级的错误。报错信息如下:

D:\Java\jdk1.8.0_171\bin\java.exe 

java.lang.ExceptionInInitializerError
	at TestSmbms.before(TestSmbms.java:19)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at 
### Error building SqlSession.
### The error may exist in com/hm/mapper/UserMapsper.xml
### **Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'User'.  Cause: java.lang.ClassNotFoundException: Cannot find class: 

Process finished with exit code -1

实体类在bean的目录下,报错User没读取到在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <properties resource="config/database.properties"></properties>

    <!--设置别名  如果未设置别名报错-->
    <typeAliases>
        <package name="com.hm.bean"></package>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver"     value="${driver}"/>
                <property name="url"        value="${url}"/>
                <property name="username"   value="${username}"/>
                <property name="password"   value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/hm/mapper/UserMapper.xml"/>
    </mappers>
</configuration>

感觉今天又水一贴,总结下我用的是批量设置别名
package为某个包下的所有类起别名,name为包名,当前包以下面的每一个子包里的类都起一个默认别名

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐