你直接返回时需要映射的
截取一段映射代码给你
<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>
截取一段映射代码给你
<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>
<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>
<!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的皮毛 ...
很激动,会点了,谢谢你
通过看你的、还有楼上的xml代码,
我倒是知道自己错在哪里了!
谢谢你
也谢谢楼上
我上面的题目就是用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>