首先说明,本人这个是springh+mybatis的东西。
本人在java里面有一个model包,里面的类叫UserInf,定义如下:
本人在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存在啊?
解决方案
5
你直接返回时需要映射的
截取一段映射代码给你
截取一段映射代码给你
<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>
10
<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>
20
<?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>
这是我们一个类的基础代码
5
你可以使用resultType或resultMap。resultType可以写类的路径,resultMap就是上面说的xml中定义的<resultMap></resultMap>。