Mybatis基础问题---resultType=hashmap有何特殊?

weidu23 2014-01-17 09:34:00
首先说明,我这个是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存在啊???
...全文
3380 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
陌乐 2016-06-26
  • 打赏
  • 举报
回复
你好,你调用方法为什么会用findbyname 而不是这个方法obtainedPublicName?
weidu23 2014-01-17
  • 打赏
  • 举报
回复
引用 2 楼 leox_2012 的回复:
<resultMap id="BaseResultMap" type="cn.caculate.model.CalData" >
    <id column="cal_data_id" property="calDataId" jdbcType="INTEGER" />
    <result column="prjid" property="prjid" jdbcType="INTEGER" />
    <result column="data_file_id" property="dataFileId" jdbcType="INTEGER" />
    <result column="data_type" property="dataType" jdbcType="INTEGER" />
  </resultMap>
你先将你自己的实体对象映射成一个resultMap,然后在查询的select里面使用resultMap参数对其进行引用。如下:
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from cal_data
    where cal_data_id = #{calDataId,jdbcType=INTEGER}
  </select>
的确是这样的, 通过看你的、还有楼上的xml代码, 我倒是知道自己错在哪里了! 谢谢你 也谢谢楼上
weidu23 2014-01-17
  • 打赏
  • 举报
回复
引用 4 楼 xieyongcx 的回复:
<?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="ShopCarMapper">
	<sql id="notDeletedSql"> is_deleted = 0 </sql> 
	<sql id="isDeletedSql"> is_deleted = 1 </sql> 
  <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" column="MODE" />
		<result property="importMoney" column="IMPORT_MONEY" />
		<result property="courseName" column="COURSE_NAME" />
		<result property="isDeleted" column="IS_DELETED" />
		<result property="updateAt" column="UPDATE_AT" />
		<result property="createAt" column="CREATE_AT" />
  </resultMap>

	<select id="getShopCarById" parameterType="int" resultMap="ShopCar">
		select * from TBL_SHOP_CAR where shop_car_id = #{shopCarId} and <include refid="notDeletedSql"/>

	</select>
	
	<select id="listShopCar" parameterType="com.able.mycu.search.course.ShopCarSearch" resultMap="ShopCar">
	    select * from TBL_SHOP_CAR where <include refid="notDeletedSql"/>
<!--		<if test="shopCarId != null"> and SHOP_CAR_ID = #{shopCarId} </if>	-->
<!--		<if test="recruitId != null"> and RECRUIT_ID = #{recruitId} </if>	-->
<!--		<if test="number != null"> and NUMBER = #{number} </if>	-->
<!--		<if test="courseId != null"> and COURSE_ID = #{courseId} </if>	-->
<!--		<if test="mode != null"> and MODE = #{mode} </if>	-->
<!--		<if test="importMoney != null"> and IMPORT_MONEY = #{importMoney} </if>	-->
<!--		<if test="courseName != null"> and COURSE_NAME like CONCAT('%','${COURSE_NAME}','%') </if>			-->
<!--		<if test="isDeleted != null"> and IS_DELETED like CONCAT('%','${IS_DELETED}','%') </if>			-->
<!--		<if test="updateAt != null"> and UPDATE_AT = #{updateAt} </if>	-->
<!--		<if test="createAt != null"> and CREATE_AT like CONCAT('%','${CREATE_AT}','%') </if>			-->
			<if test="schoolId != null"> and SCHOOL_ID=#{schoolId} </if>
			<if test="userId != null"> and USER_ID=#{userId} </if>			
	limit #{start},#{limit}
	</select>
	
	<select id="countShopCar" parameterType="com.able.mycu.search.course.ShopCarSearch" resultType="int">
		select count(1) from TBL_SHOP_CAR where <include refid="notDeletedSql"/>
<!--		<if test="shopCarId != null"> and SHOP_CAR_ID = #{shopCarId} </if>	-->
<!--		<if test="recruitId != null"> and RECRUIT_ID = #{recruitId} </if>	-->
<!--		<if test="number != null"> and NUMBER = #{number} </if>	-->
<!--		<if test="courseId != null"> and COURSE_ID = #{courseId} </if>	-->
<!--		<if test="mode != null"> and MODE = #{mode} </if>	-->
<!--		<if test="importMoney != null"> and IMPORT_MONEY = #{importMoney} </if>	-->
<!--		<if test="courseName != null"> and COURSE_NAME like CONCAT('%','${COURSE_NAME}','%') </if>			-->
<!--		<if test="isDeleted != null"> and IS_DELETED like CONCAT('%','${IS_DELETED}','%') </if>			-->
<!--		<if test="updateAt != null"> and UPDATE_AT = #{updateAt} </if>	-->
<!--		<if test="createAt != null"> and CREATE_AT like CONCAT('%','${CREATE_AT}','%') </if>			-->
	</select>
	
	<select id="listAllShopCar" parameterType="com.able.mycu.search.course.ShopCarSearch" resultMap="ShopCar">
	    select * from TBL_SHOP_CAR where <include refid="notDeletedSql"/>
<!--		<if test="shopCarId != null"> and SHOP_CAR_ID = #{shopCarId} </if>	-->
<!--		<if test="recruitId != null"> and RECRUIT_ID = #{recruitId} </if>	-->
<!--		<if test="number != null"> and NUMBER = #{number} </if>	-->
<!--		<if test="courseId != null"> and COURSE_ID = #{courseId} </if>	-->
<!--		<if test="mode != null"> and MODE = #{mode} </if>	-->
<!--		<if test="importMoney != null"> and IMPORT_MONEY = #{importMoney} </if>	-->
<!--		<if test="courseName != null"> and COURSE_NAME like CONCAT('%','${COURSE_NAME}','%') </if>			-->
<!--		<if test="isDeleted != null"> and IS_DELETED like CONCAT('%','${IS_DELETED}','%') </if>			-->
<!--		<if test="updateAt != null"> and UPDATE_AT = #{updateAt} </if>	-->
<!--		<if test="createAt != null"> and CREATE_AT like CONCAT('%','${CREATE_AT}','%') </if>			-->
			<if test="schoolId != null"> and SCHOOL_ID=#{schoolId} </if>
			<if test="userId != null"> and USER_ID=#{userId} </if>	
	</select>

	<insert id="saveShopCar" parameterType="com.able.mycu.model.course.ShopCar">
		insert into TBL_SHOP_CAR (
			<if test="recruitId != null"> RECRUIT_ID </if>
			<if test="number != null"> ,NUMBER </if>
			<if test="courseId != null"> ,COURSE_ID </if>
			<if test="mode != null"> ,MODE </if>
			<if test="importMoney != null"> ,IMPORT_MONEY </if>
			<if test="courseName != null"> ,COURSE_NAME </if>
			<if test="isDeleted != null"> ,IS_DELETED </if>
			<if test="updateAt != null"> ,UPDATE_AT </if>
			<if test="createAt != null"> ,CREATE_AT </if>
			<if test="userId != null"> ,USER_ID </if>
			<if test="schoolId != null"> ,SCHOOL_ID </if>
		)
		values (
		<if test="recruitId != null"> #{recruitId} </if>
		<if test="number != null"> ,#{number} </if>
		<if test="courseId != null"> ,#{courseId} </if>
		<if test="mode != null"> ,#{mode} </if>
		<if test="importMoney != null"> ,#{importMoney} </if>
		<if test="courseName != null"> ,#{courseName} </if>
		<if test="isDeleted != null"> ,#{isDeleted} </if>
		<if test="updateAt != null"> ,#{updateAt} </if>
		<if test="createAt != null"> ,#{createAt} </if>
		<if test="userId != null"> ,#{userId} </if>
		<if test="schoolId != null"> ,#{schoolId} </if>
		)
	<selectKey resultType="int" order="AFTER" keyProperty="shopCarId">
		SELECT LAST_INSERT_ID() AS shop_car_id
	</selectKey>
	</insert>

	<update id="removeShopCarByRecruitId" parameterType="hashmap" >
		delete from TBL_SHOP_CAR where RECRUIT_ID = #{recruitId} and USER_ID=#{userId} and schoolId =#{schoolId}
	</update>

	<update id="updateShopCarById" parameterType="com.able.mycu.model.course.ShopCar">
		update TBL_SHOP_CAR set 
<!--		<if test="shopCarId != null"> ,SHOP_CAR_ID = #{shopCarId} </if>		-->
		<if test="recruitId != null"> RECRUIT_ID = #{recruitId} </if>		--
		<if test="number != null"> ,NUMBER = #{number} </if>		
<!--		<if test="courseId != null"> ,COURSE_ID = #{courseId} </if>		-->
<!--		<if test="mode != null"> ,MODE = #{mode} </if>		-->
<!--		<if test="importMoney != null"> ,IMPORT_MONEY = #{importMoney} </if>		-->
<!--		<if test="courseName != null"> ,COURSE_NAME = #{courseName} </if>		-->
<!--		<if test="isDeleted != null"> ,IS_DELETED = #{isDeleted} </if>		-->
			<if test="updateAt != null"> ,UPDATE_AT = #{updateAt} </if>		
<!--		<if test="createAt != null"> ,CREATE_AT = #{createAt} </if>		-->
		where RECRUIT_ID = #{recruitId} and USER_ID = #{userId} and SCHOOL_ID = #{schoolId}
	</update>
</mapper>
这是我们一个类的基础代码
非常感谢你,我学会你这个xml的一些东西了,虽然,只是mybatis的皮毛 ... 很激动,会点了,谢谢你
xieyongcx 2014-01-17
  • 打赏
  • 举报
回复
<?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="ShopCarMapper">
	<sql id="notDeletedSql"> is_deleted = 0 </sql> 
	<sql id="isDeletedSql"> is_deleted = 1 </sql> 
  <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" column="MODE" />
		<result property="importMoney" column="IMPORT_MONEY" />
		<result property="courseName" column="COURSE_NAME" />
		<result property="isDeleted" column="IS_DELETED" />
		<result property="updateAt" column="UPDATE_AT" />
		<result property="createAt" column="CREATE_AT" />
  </resultMap>

	<select id="getShopCarById" parameterType="int" resultMap="ShopCar">
		select * from TBL_SHOP_CAR where shop_car_id = #{shopCarId} and <include refid="notDeletedSql"/>

	</select>
	
	<select id="listShopCar" parameterType="com.able.mycu.search.course.ShopCarSearch" resultMap="ShopCar">
	    select * from TBL_SHOP_CAR where <include refid="notDeletedSql"/>
<!--		<if test="shopCarId != null"> and SHOP_CAR_ID = #{shopCarId} </if>	-->
<!--		<if test="recruitId != null"> and RECRUIT_ID = #{recruitId} </if>	-->
<!--		<if test="number != null"> and NUMBER = #{number} </if>	-->
<!--		<if test="courseId != null"> and COURSE_ID = #{courseId} </if>	-->
<!--		<if test="mode != null"> and MODE = #{mode} </if>	-->
<!--		<if test="importMoney != null"> and IMPORT_MONEY = #{importMoney} </if>	-->
<!--		<if test="courseName != null"> and COURSE_NAME like CONCAT('%','${COURSE_NAME}','%') </if>			-->
<!--		<if test="isDeleted != null"> and IS_DELETED like CONCAT('%','${IS_DELETED}','%') </if>			-->
<!--		<if test="updateAt != null"> and UPDATE_AT = #{updateAt} </if>	-->
<!--		<if test="createAt != null"> and CREATE_AT like CONCAT('%','${CREATE_AT}','%') </if>			-->
			<if test="schoolId != null"> and SCHOOL_ID=#{schoolId} </if>
			<if test="userId != null"> and USER_ID=#{userId} </if>			
	limit #{start},#{limit}
	</select>
	
	<select id="countShopCar" parameterType="com.able.mycu.search.course.ShopCarSearch" resultType="int">
		select count(1) from TBL_SHOP_CAR where <include refid="notDeletedSql"/>
<!--		<if test="shopCarId != null"> and SHOP_CAR_ID = #{shopCarId} </if>	-->
<!--		<if test="recruitId != null"> and RECRUIT_ID = #{recruitId} </if>	-->
<!--		<if test="number != null"> and NUMBER = #{number} </if>	-->
<!--		<if test="courseId != null"> and COURSE_ID = #{courseId} </if>	-->
<!--		<if test="mode != null"> and MODE = #{mode} </if>	-->
<!--		<if test="importMoney != null"> and IMPORT_MONEY = #{importMoney} </if>	-->
<!--		<if test="courseName != null"> and COURSE_NAME like CONCAT('%','${COURSE_NAME}','%') </if>			-->
<!--		<if test="isDeleted != null"> and IS_DELETED like CONCAT('%','${IS_DELETED}','%') </if>			-->
<!--		<if test="updateAt != null"> and UPDATE_AT = #{updateAt} </if>	-->
<!--		<if test="createAt != null"> and CREATE_AT like CONCAT('%','${CREATE_AT}','%') </if>			-->
	</select>
	
	<select id="listAllShopCar" parameterType="com.able.mycu.search.course.ShopCarSearch" resultMap="ShopCar">
	    select * from TBL_SHOP_CAR where <include refid="notDeletedSql"/>
<!--		<if test="shopCarId != null"> and SHOP_CAR_ID = #{shopCarId} </if>	-->
<!--		<if test="recruitId != null"> and RECRUIT_ID = #{recruitId} </if>	-->
<!--		<if test="number != null"> and NUMBER = #{number} </if>	-->
<!--		<if test="courseId != null"> and COURSE_ID = #{courseId} </if>	-->
<!--		<if test="mode != null"> and MODE = #{mode} </if>	-->
<!--		<if test="importMoney != null"> and IMPORT_MONEY = #{importMoney} </if>	-->
<!--		<if test="courseName != null"> and COURSE_NAME like CONCAT('%','${COURSE_NAME}','%') </if>			-->
<!--		<if test="isDeleted != null"> and IS_DELETED like CONCAT('%','${IS_DELETED}','%') </if>			-->
<!--		<if test="updateAt != null"> and UPDATE_AT = #{updateAt} </if>	-->
<!--		<if test="createAt != null"> and CREATE_AT like CONCAT('%','${CREATE_AT}','%') </if>			-->
			<if test="schoolId != null"> and SCHOOL_ID=#{schoolId} </if>
			<if test="userId != null"> and USER_ID=#{userId} </if>	
	</select>

	<insert id="saveShopCar" parameterType="com.able.mycu.model.course.ShopCar">
		insert into TBL_SHOP_CAR (
			<if test="recruitId != null"> RECRUIT_ID </if>
			<if test="number != null"> ,NUMBER </if>
			<if test="courseId != null"> ,COURSE_ID </if>
			<if test="mode != null"> ,MODE </if>
			<if test="importMoney != null"> ,IMPORT_MONEY </if>
			<if test="courseName != null"> ,COURSE_NAME </if>
			<if test="isDeleted != null"> ,IS_DELETED </if>
			<if test="updateAt != null"> ,UPDATE_AT </if>
			<if test="createAt != null"> ,CREATE_AT </if>
			<if test="userId != null"> ,USER_ID </if>
			<if test="schoolId != null"> ,SCHOOL_ID </if>
		)
		values (
		<if test="recruitId != null"> #{recruitId} </if>
		<if test="number != null"> ,#{number} </if>
		<if test="courseId != null"> ,#{courseId} </if>
		<if test="mode != null"> ,#{mode} </if>
		<if test="importMoney != null"> ,#{importMoney} </if>
		<if test="courseName != null"> ,#{courseName} </if>
		<if test="isDeleted != null"> ,#{isDeleted} </if>
		<if test="updateAt != null"> ,#{updateAt} </if>
		<if test="createAt != null"> ,#{createAt} </if>
		<if test="userId != null"> ,#{userId} </if>
		<if test="schoolId != null"> ,#{schoolId} </if>
		)
	<selectKey resultType="int" order="AFTER" keyProperty="shopCarId">
		SELECT LAST_INSERT_ID() AS shop_car_id
	</selectKey>
	</insert>

	<update id="removeShopCarByRecruitId" parameterType="hashmap" >
		delete from TBL_SHOP_CAR where RECRUIT_ID = #{recruitId} and USER_ID=#{userId} and schoolId =#{schoolId}
	</update>

	<update id="updateShopCarById" parameterType="com.able.mycu.model.course.ShopCar">
		update TBL_SHOP_CAR set 
<!--		<if test="shopCarId != null"> ,SHOP_CAR_ID = #{shopCarId} </if>		-->
		<if test="recruitId != null"> RECRUIT_ID = #{recruitId} </if>		--
		<if test="number != null"> ,NUMBER = #{number} </if>		
<!--		<if test="courseId != null"> ,COURSE_ID = #{courseId} </if>		-->
<!--		<if test="mode != null"> ,MODE = #{mode} </if>		-->
<!--		<if test="importMoney != null"> ,IMPORT_MONEY = #{importMoney} </if>		-->
<!--		<if test="courseName != null"> ,COURSE_NAME = #{courseName} </if>		-->
<!--		<if test="isDeleted != null"> ,IS_DELETED = #{isDeleted} </if>		-->
			<if test="updateAt != null"> ,UPDATE_AT = #{updateAt} </if>		
<!--		<if test="createAt != null"> ,CREATE_AT = #{createAt} </if>		-->
		where RECRUIT_ID = #{recruitId} and USER_ID = #{userId} and SCHOOL_ID = #{schoolId}
	</update>
</mapper>
这是我们一个类的基础代码
weidu23 2014-01-17
  • 打赏
  • 举报
回复
引用 1 楼 xieyongcx 的回复:
你直接返回时需要映射的 截取一段映射代码给你
  <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" column="MODE" />
		<result property="importMoney" column="IMPORT_MONEY" />
		<result property="courseName" column="COURSE_NAME" />
		<result property="isDeleted" column="IS_DELETED" />
		<result property="updateAt" column="UPDATE_AT" />
		<result property="createAt" column="CREATE_AT" />
  </resultMap>
可否截一段完整的给我看看,哪怕最简单只有一个参数也好!!! 我手头上没多少资料抹黑学习。 想知道你那个一个最简单的完整的!(PS:网上百度都不全,所以学习半吊子水)
leox_2010 2014-01-17
  • 打赏
  • 举报
回复
<resultMap id="BaseResultMap" type="cn.caculate.model.CalData" >
    <id column="cal_data_id" property="calDataId" jdbcType="INTEGER" />
    <result column="prjid" property="prjid" jdbcType="INTEGER" />
    <result column="data_file_id" property="dataFileId" jdbcType="INTEGER" />
    <result column="data_type" property="dataType" jdbcType="INTEGER" />
  </resultMap>
你先将你自己的实体对象映射成一个resultMap,然后在查询的select里面使用resultMap参数对其进行引用。如下:
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from cal_data
    where cal_data_id = #{calDataId,jdbcType=INTEGER}
  </select>
xieyongcx 2014-01-17
  • 打赏
  • 举报
回复
你直接返回时需要映射的 截取一段映射代码给你
  <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" column="MODE" />
		<result property="importMoney" column="IMPORT_MONEY" />
		<result property="courseName" column="COURSE_NAME" />
		<result property="isDeleted" column="IS_DELETED" />
		<result property="updateAt" column="UPDATE_AT" />
		<result property="createAt" column="CREATE_AT" />
  </resultMap>
weidu23 2014-01-17
  • 打赏
  • 举报
回复
引用 8 楼 chenxyz707 的回复:
你可以使用resultType或者resultMap。resultType可以写类的路径,resultMap就是上面说的xml中定义的<resultMap></resultMap>。
我上面的题目就是用resultType=“” 但是返回的就是Null,你可以看看我的提问,如下:zxw.model.UserInf

<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>
chenxyz707 2014-01-17
  • 打赏
  • 举报
回复
你可以使用resultType或者resultMap。resultType可以写类的路径,resultMap就是上面说的xml中定义的<resultMap></resultMap>。

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧