在学习mybatis的时候,老师讲实体类和表中名字不一样的时候要配置resultMap完成映射,但是我配置了resultMap之后输出查询结果还是为null。
以下是我的mapper文件
<?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">
<!-- namespace表示命名空间 -->
<mapper namespace="com.mapper.SolMapper">
<!-- where后的是表里的名字 #{}里的一定是类里的名字 -->
<select id="findByName" parameterType="String" resultMap="mymap">
select * from sol where solname=#{nam}
</select>
<!-- 映射文件 -->
<!-- 表里的名字与列里名字不一样的时候 -->
<resultMap id="mymap" type="com.po.Sol" >
<!-- id property="id"实体类里的属性 column="id"表里的列/-->
<id property="id" column="id"/>
<result property="nam" column="solname"/>
<result property="sch" column="solschool"/>
<result property="hob" column="solhobby"/>
</resultMap>
</mapper>
下面是我的输出查询test文件
package com.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.po.Sol;
import com.util.SqlSessionUtil;
public class Test2 {
public static void main(String []args){
SqlSession sqlSession=SqlSessionUtil.getSqlSession();
sqlSession.selectOne("com.mapper.SolMapper.findByName","小叮当");
System.out.println("查找到"+log.getId()+log.getNam()+log.getSch()+log.getHob());
}
}
我在config文件中用了下面方法让我的程序显示运行过程
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
运行过程及输出null错误如下
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 372093341.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@162db19d]
==> Preparing: select * from sol where solname=?
==> Parameters: 小叮当(String)
<== Columns: id, solname, solschool, solhobby
<== Row: 666, 小叮当, 初中, 化学
<== Total: 1
查找到0nullnullnull