先了解下查询 如下sql 语句
说明 sql 的ID 要唯一!!!
<resultMap id="BaseResultMap" type="map" >
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="userName" property="userName" jdbcType="VARCHAR" />
<result column="passWord" property="password" jdbcType="VARCHAR" />
<result column="disPlayName" property="disPlayName" javaType="VARCHAR"/>
</resultMap>
//resultMap 将返回值映射到自定义到的map中
<select id="findAll" resultMap="BaseResultMap">
select
id , userName, passWord, disPlayName,AvatorUrl,DateCreate,description
from guser limit 1,10
</select>
//也可以不写resultMap 直接用resultType
<select id="findAll"resultType="map">
select
id , userName, passWord, disPlayName,AvatorUrl,DateCreate,description
from guser limit 1,10
</select>
也有人会把resultMap 写成如下的方式
//type 就是将返回值映射成一个pojo实体
<resultMap id="GuserResultMap" type="com.chris.apecircle.domain.Guser.Guser" >
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="userName" property="userName" jdbcType="VARCHAR" />
<result column="passWord" property="password" jdbcType="VARCHAR" />
<result column="disPlayName" property="disPlayName" javaType="VARCHAR"/>
</resultMap>
<select id="getAll" resultMap="GuserResultMap">
select
id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
from guser limit 1,10
</select>
//这里我反而觉得有点鸡肋,原因如下
//1.这里我如果只需上述的4 个参数,这样写会将实体中没有映射的属性也返回,返回值臃肿
//2.如果一定要返回的话 我们可以采用 resultType 的方式,少写了一个resultMap 代码也整洁了
//3.这种情况在有别名的状态下才能体现价值所在
resultType方式
<select id="getAll" resultType="com.chris.apecircle.domain.Guser.Guser">
select
id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
from guser limit 1,10
</select>
//MARK: resultType 和 resultMap 不能同时使用
传参数情况下
List<Guser> getGuserbyUserName(String userName);
//parameterType 输入的参数类型
<select id="getGuserbyUserName" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
from guser where userName = #{userName}
</select>
当有两个参数的时候 就不能用parameterType 了这时用几种方法来处理,这里我只展示一种常用的方法
一:采用下标的方式
Object getGuser(String userName,String pwd);
<select id="getGuser" resultMap="BaseResultMap" >
select
id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
from guser where userName = #{arg0} and passWord = #{arg1}
</select>
//这种对于mapper 修改的较少但是需要和参数一一对应,对于参数多时不友好
二:采用注解的方式
Object getGuser(@Param("userName") String userName,@Param("pwd") String pwd );
<select id="getGuser" resultMap="BaseResultMap" >
select
id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
from guser where userName = #{userName} and passWord = #{pwd}
</select>
//这种方式 相较于前者 明确传值名称,顺序不需要一一对应, 缺点 对于参数多时依然不友好
三:map 封装
Object getGuser(HashMap map);
<select id="getGuser" resultMap="BaseResultMap" >
select
id, userName, passWord, disPlayName,AvatorUrl,DateCreate,description
from guser where userName = #{userName} and passWord = #{pwd}
</select>