日期:2014-05-18  浏览次数:20744 次

Mybatis基础问题---resultType=hashmap有何特殊?
首先说明,我这个是springh+mybatis的东西。
我在java里面有一个model包,里面的类叫UserInf,定义如下:
package zxw.model;
public class UserInf {
    private Integer userId;
    private String userName;
    private String userPasswd;
    //getter and setter
}

此时,我在mybatis的UserInfMapper.xml中写道:
<mapper namespace="zxw.dao.UserInfMapper" >
   <!-- public UserInf selectPublicName(String userName); -->
   <select id="obtainedPublicName" resultType="zxw.model.UserInf" parameterType="java.lang.String">
   select * from userInf where user_name = #{userName,jdbcType=VARCHAR}
  </select>
</mapper>

其中,对应的UserIntMapper.java中写了

package zxw.dao;
import zxw.model.UserInf;
public interface UserInfMapper {
    /**根据名字查询数据*/
    UserInf obtainedPublicName(String userName);
}

然后在service层中一个被@Service("user")的类中定义bean。
最后是调用getBean获取数据!

public static boolean checkUser(String userName , String userPasswd){
UserInfServiceI userInf = (UserInfServiceImpl)CreateBean.beanFactory("userInfService");
System.out.println(userInf.maxId());
UserInf user = userInf.findByName(userName);
System.out.println(user);
if( null == user )
return false ;
return  (
! user.getUserName().equals(userName)
||
! user.getUserPasswd().equals(userPasswd)
)
?
false
:
true;
}


此时,发现System.out.println(user);输入为null !!!

于是,我做了几个测试,我发现了:
我把UserInfMapper.xml中的resultType="zxw.model.UserInf" 改成resultType="hashmap",
我就能 System.out.println(user);打印出user信息了!!!
-----------------------
问:
   为什么我自己定义的resultType返回值类型就不能获取数据库信息,而resultType为hashmap就可以?
可是,我的zxw.model.UserInf存在啊???

------解决方案--------------------
你直接返回时需要映射的
截取一段映射代码给你
  <resultMap id="ShopCar" type="com.able.mycu.model.course.ShopCar">
<result property="shopCarId" column="SHOP_CAR_ID" />
<result property="recruitId" column="RECRUIT_ID" />
<result property="number" column="NUMBER" />
<result property="courseId" column="COURSE_ID" />
<result property="mode" colum