Mybatis框架中,Mapper文件參數獲取一般有以下幾種:
1、參數個數爲1個(string或者int)
dao層方法爲以下兩種:
/**
* 單個int型
*/
public List<UserComment> findByDepartmentId(int dapartmentId);
/**
* 單個string型
*/
public Source findByTitle(String title);
對應的Mapper取值:
取值時應當注意,參數名字應該與dao層傳入的參數名字相同。
/*單個int型*/
<select id="findByDepartmentId" resultType="com.bonc.wechat.entity.publicserver.UserComment">
select * from wx_user_comment where
department_id=#{departmentId}
order by createtime desc;
</select>
/*單個string型*/
<select id="findByTitle" parameterType="java.lang.String" resultType="com.bonc.wechat.entity.publicserver.Source">
select * from wx_source where
title=#{title};
</select>
或者直接使用通用方法獲取參數
<select id="findByDepartmentId" resultType="com.bonc.wechat.entity.publicserver.UserComment">
select * from wx_user_comment where
department_id=#{_parameter}
order by createtime desc;
</select>
<select id="findByTitle" parameterType="java.lang.String" resultType="com.bonc.wechat.entity.publicserver.Source">
select * from wx_source where
title=#{_parameter};
</select>
2、參數個數爲多個。
dao層方法:
/*****1.註解傳參*****/
public List<UserTab> selectUserListExceptUserId
(@Param("USER_ID")String USER_ID,
@Param("LIMIT_POS")int LIMIT_POS,
@Param("LIMIT_SIZE")int LIMIT_SIZE);
對應的Mapper取值:
取值時應當注意,參數名字應該與dao層傳入的參數名字(@param處聲明的參數名)相同。
/****註解傳參方式參數獲取****/
<select id="selectUserListExceptUserId"
resultMap="userResMap">
select * from MH_USER
where USER_ID!=#{USER_ID}
and USER_STATE>9
order by NICK_NAME
limit #{LIMIT_POS},#{LIMIT_SIZE}
</select>
3、參數爲map的形式(常用)
mapper中使用map的key取值
dao中的方法:
/*****1.參數爲map*****/
public List<Source> search(Map<String,Object> param);
/***2.map的內部封裝***/
Map<String,Object> param=new HashMap<String,Object>();
param.put("page", (page-1)*pageSize);
param.put("pageSize",pageSize);
param.put("keyword","%"+keyword+"%");
param.put("type",type);
List<Source> sources=sourceDao.search(param);
對應的Mapper取值:
<select id="search"
parameterType="java.util.Map"
resultType="com.bonc.wechat.entity.publicserver.Source">
select * from wx_source
where
<if test="keyword != null and keyword != ''">
(title like #{keyword(按map鍵取值)} or content like #{keyword}) and
</if>
type=#{type}
order by ordernum asc
limit #{page},#{pageSize};
</select>
4、參數爲對象(常用)
1.mapper中使用對象中的屬性直接取值,在映射接口中的方法不加@Param
2.【對象.屬性】取值:對象的名稱一定要在映射接口中的方法中聲明@Param並一致
dao中的方法:
/*****使用對象傳參*****/
public int addUserComment(UserComment UC);
/*****對象中的屬性*****/
private int id;
private int department_id;
private String telphone;
private String content;
private String createtime;
對應的Mapper取值:
<insert id="addUserComment"
parameterType="com.bonc.wechat.entity.publicserver.UserComment">
insert into wx_user_comment
(department_id,
telphone,
content,
createtime)
values(#{department_id},
#{telphone},
#{content},
#{createtime});
</insert>
*使用【對象.屬性】取值。
dao層:
/******此示例中直接省去dao層,service直接綁定mapper層******/
public PageResult findPanoramaPage(Page page) throws Exception{
List<PageData> panoramaList = new ArrayList<PageData>();
try {
panoramaList = (List<PageData>)dao.findForList("PanoramaMapper.findPagePanorama", page);
} catch (Exception e) {
e.printStackTrace();
}
PageResult pageResult = new PageResult(page.getTotalResult(),panoramaList);
return pageResult;
}
對應的Mapper取值:
<select id="findPagePanorama"
parameterType="page"
resultType="pd">
SELECT
a.id as id,
a.project_code as projectCode,
a.project_name as projectName,
a.project_addr as projectAddr,
a.project_type as projectType
FROM
calm_project a
WHERE
a.is_valid=1
<if test="page.projectType != null and page.projectType != ''">
AND a.project_type = #{page.projectType}
</if>
<if test="page.keyword != null and page.keyword != ''">
AND (a.project_name LIKE '%${page.keyword}%' OR a.project_code LIKE '%${page.keyword}%')
</if>
ORDER BY
a.sort
</select>
推薦使用第三和第四種,將所傳的數據在controller層或者service層封裝起來,傳入mapper文件中取值。