(๑•॒̀ ູ॒•́๑)嘟嘟嘟 2022-04-16 17:32 采纳率: 73.7%
浏览 56
已结题

MyBatis映射器中resultMap元素的问题

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图

1.AddressMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.AddressMapper">
    <resultMap id="AddressMap" type="address">
        <!--resultMap元素的子元素 constructor元素的作用是映射构造方法,一定要保证参数的对应-->
        <constructor>
            <!--idArg是主键的唯一标识。javatype指定的是基本数据类型,也可以是封装类型-->
            <idArg column="id" jdbcType="Integer" />
            <!--arg元素标识的是主键以外的其它列-->
            <arg column="contact" jdbcType="String"/>
            <arg column="addressDesc" jdbcType="String"/>
        </constructor>
    </resultMap>
    <select id="queryAll" databaseId="AddressMap">
        select * from address
    </select>
</mapper>

2.AddressMapper.java

package mapper;

import pojo.Address;

import java.util.List;

public interface AddressMapper {
    //
    List<Address> queryAll();

}

3.
Address.java类

package pojo;

public class Address {
    //id主键自增
    private Integer id;
    //联络人
    private String contact;
    //具体地址
    private String addressDesc;
    //如果使用constructor元素映射有参构造方法,则参数的类型,需要写为封装类型,否者会报错
    public Address(Integer id, String contact, String addressDesc) {
        this.id = id;
        this.contact = contact;
        this.addressDesc = addressDesc;
    }
}

4.测试类

System.out.println("+++++++++++++++++++++");
            AddressMapper addressMapper = sqlSession.getMapper(AddressMapper.class);
            List<Address> addressList=addressMapper.queryAll();
            for (Address address : addressList){
                System.out.println(address);

5.数据库

create table address(
    id int primary key auto_increment,
    contact varchar(15),
    addressDesc varchar(20)
);
insert into address(contact,addressDesc) values ('zhanggsan','huhhot');
insert into address(contact,addressDesc) values ('lisi','haotao');

select * from address;
运行结果及报错内容
G:\jdk\jdk-14.0.2\bin\java.exe "-javaagent:G:\ideaIC-2021.2.1 (1)\IntelliJ IDEA Community Edition 2021.2.1\lib\idea_rt.jar=65376:G:\ideaIC-2021.2.1 (1)\IntelliJ IDEA Community Edition 2021.2.1\bin" -Dfile.encoding=UTF-8 -classpath G:\Javamyfist\target\classes;C:\Users\HP\.m2\repository\org\mybatis\mybatis\3.5.6\mybatis-3.5.6.jar;C:\Users\HP\.m2\repository\mysql\mysql-connector-java\8.0.25\mysql-connector-java-8.0.25.jar;C:\Users\HP\.m2\repository\com\google\protobuf\protobuf-java\3.11.4\protobuf-java-3.11.4.jar;C:\Users\HP\.m2\repository\log4j\log4j\1.2.16\log4j-1.2.16.jar MyBatisTest
Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
Class not found: org.jboss.vfs.VFS
JBoss 6 VFS API is not available in this environment.
Class not found: org.jboss.vfs.VirtualFile
VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
Using VFS adapter org.apache.ibatis.io.DefaultVFS
Find JAR URL: file:/G:/Javamyfist/target/classes/pojo
Not a JAR: file:/G:/Javamyfist/target/classes/pojo
Reader entry: Address.class
Reader entry: User.class
Listing file:/G:/Javamyfist/target/classes/pojo
Find JAR URL: file:/G:/Javamyfist/target/classes/pojo/Address.class
Not a JAR: file:/G:/Javamyfist/target/classes/pojo/Address.class
Reader entry: ����   : 
Find JAR URL: file:/G:/Javamyfist/target/classes/pojo/User.class
Not a JAR: file:/G:/Javamyfist/target/classes/pojo/User.class
Reader entry: ����   : o
Checking to see if class pojo.Address matches criteria [is assignable to Object]
Checking to see if class pojo.User matches criteria [is assignable to Object]
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Created connection 467796378.
Returned connection 467796378 to pool.
Find JAR URL: file:/G:/Javamyfist/target/classes/mapper
Not a JAR: file:/G:/Javamyfist/target/classes/mapper
Reader entry: AddressMapper.class
Reader entry: AddressMapper.xml
Reader entry: UserMapper.class
Reader entry: UserMapper.xml
Listing file:/G:/Javamyfist/target/classes/mapper
Find JAR URL: file:/G:/Javamyfist/target/classes/mapper/AddressMapper.class
Not a JAR: file:/G:/Javamyfist/target/classes/mapper/AddressMapper.class
Reader entry: ����   :   mapper/AddressMapper  java/lang/ObjectqueryAll ()Ljava/util/List;     Signature "()Ljava/util/List<Lpojo/Address;>; 
Find JAR URL: file:/G:/Javamyfist/target/classes/mapper/AddressMapper.xml
Not a JAR: file:/G:/Javamyfist/target/classes/mapper/AddressMapper.xml
Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
Find JAR URL: file:/G:/Javamyfist/target/classes/mapper/UserMapper.class
Not a JAR: file:/G:/Javamyfist/target/classes/mapper/UserMapper.class
Reader entry: ����   : 1  mapper/UserMapper  java/lang/ObjectqueryAll ()Ljava/util/List;     Signature ()Ljava/util/List<Lpojo/User;>; selectUserById (I)Lpojo/User; RuntimeVisibleAnnotations &Lorg/apache/ibatis/annotations/Select; value $select * from users where id = ${id} queryByFirstName (Ljava/lang/String;)I queryByName (Ljava/lang/String;)Lpojo/User; queryByUnameAndGen !(Ljava/util/Map;)Ljava/util/List; T(Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;)Ljava/util/List<Lpojo/User;>; queryByUnameAndGen1 6(Ljava/lang/String;Ljava/lang/String;)Ljava/util/List; C(Ljava/lang/String;Ljava/lang/String;)Ljava/util/List<Lpojo/User;>; "RuntimeVisibleParameterAnnotations %Lorg/apache/ibatis/annotations/Param;userName gender queryByUnameAndGen2 (Lpojo/User;)Ljava/util/List; *(Lpojo/User;)Ljava/util/List<Lpojo/User;>; queryByUnameAndGen3 (ILpojo/User;)Ljava/util/List; +(ILpojo/User;)Ljava/util/List<Lpojo/User;>; id user     queryAll1 
Find JAR URL: file:/G:/Javamyfist/target/classes/mapper/UserMapper.xml
Not a JAR: file:/G:/Javamyfist/target/classes/mapper/UserMapper.xml
Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
Checking to see if class mapper.AddressMapper matches criteria [is assignable to Object]
Checking to see if class mapper.UserMapper matches criteria [is assignable to Object]
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in mapper/AddressMapper.xml
### The error occurred while processing mapper_resultMap[AddressMap]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'mapper/AddressMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving JdbcType. Cause: java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.type.JdbcType.Integer
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:52)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:36)
    at MyBatisTest.getSqlSessionFactory(MyBatisTest.java:29)
    at MyBatisTest.main(MyBatisTest.java:42)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'mapper/AddressMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving JdbcType. Cause: java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.type.JdbcType.Integer
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:122)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:99)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:50)
    ... 3 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'mapper/AddressMapper.xml'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving JdbcType. Cause: java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.type.JdbcType.Integer
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:123)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:95)
    at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.loadXmlResource(MapperAnnotationBuilder.java:179)
    at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parse(MapperAnnotationBuilder.java:118)
    at org.apache.ibatis.binding.MapperRegistry.addMapper(MapperRegistry.java:72)
    at org.apache.ibatis.binding.MapperRegistry.addMappers(MapperRegistry.java:106)
    at org.apache.ibatis.binding.MapperRegistry.addMappers(MapperRegistry.java:118)
    at org.apache.ibatis.session.Configuration.addMappers(Configuration.java:837)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:368)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:120)
    ... 5 more
Caused by: org.apache.ibatis.builder.BuilderException: Error resolving JdbcType. Cause: java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.type.JdbcType.Integer
    at org.apache.ibatis.builder.BaseBuilder.resolveJdbcType(BaseBuilder.java:73)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.buildResultMappingFromContext(XMLMapperBuilder.java:392)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.processConstructorElement(XMLMapperBuilder.java:317)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:272)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:254)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:246)
    at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:119)
    ... 14 more
Caused by: java.lang.IllegalArgumentException: No enum constant org.apache.ibatis.type.JdbcType.Integer
    at java.base/java.lang.Enum.valueOf(Enum.java:273)
    at org.apache.ibatis.type.JdbcType.valueOf(JdbcType.java:25)
    at org.apache.ibatis.builder.BaseBuilder.resolveJdbcType(BaseBuilder.java:71)
    ... 20 more

Process finished with exit code 1
我的解答思路和尝试过的方法
我想要达到的结果

可以达到映射的效果

  • 写回答

3条回答 默认 最新

  • a1767028198 2022-04-16 18:58
    关注

    javaType和jdbcType可是不同的 - -

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月18日
  • 已采纳回答 4月18日
  • 创建了问题 4月16日

悬赏问题

  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题
  • ¥15 使用dify通过OpenAI 的API keys添加OpenAI模型时报了“Connection Error”错误