java mapper 参数_Mybatis框架中Mapper文件传值参数获取。【Mybatis】

1、参数个数为1个(string或者int)

dao层方法为以下两种:

/**

* 单个int型

*/

public List findByDepartmentId(int dapartmentId);

/**

* 单个string型

*/

public Source findByTitle(String title);

对应的Mapper取值:

取值时应当注意,参数名字应该与dao层传入的参数名字相同。

/*单个int型*/

select * from wx_user_comment where

department_id=#{departmentId}

order by createtime desc;

/*单个string型*/

select * from wx_source where

title=#{title};

/*****或者直接使用通用方法获取参数*****/

select * from wx_user_comment where

department_id=#{_parameter}

order by createtime desc;

select * from wx_source where

title=#{_parameter};

2、参数个数为多个。

dao层方法:

/*****1.正常传参*****/

public Dailyuserinfo findStutaByUserAndDaily(String username,String dailyid);

/*****2.注解传参*****/

public List selectUserListExceptUserId

(@Param("USER_ID")String USER_ID,

@Param("LIMIT_POS")int LIMIT_POS,

@Param("LIMIT_SIZE")int LIMIT_SIZE);

对应的Mapper取值:

取值时应当注意,参数名字应该与dao层传入的参数名字相同。

/****正常传参方式参数获取****/

parameterType="java.lang.String"

resultType="com.thinkgem.jeesite.modules.dailynews.entity.Dailyuserinfo">

select * from daily_user_info

where login_name=#{username}

And daily_id=#{dailyid};

/****注解传参方式参数获取****/

resultMap="userResMap">

select * from MH_USER

where USER_ID!=#{USER_ID}

and USER_STATE>9

order by NICK_NAME

limit #{LIMIT_POS},#{LIMIT_SIZE}

3、参数为map的形式。

mapper中使用map的key取值。

dao中的方法。

/*****1.参数为map*****/

public List search(Map param);

/***2.map的内部封装***/

Map param=new HashMap();

param.put("page", (page-1)*pageSize);

param.put("pageSize",pageSize);

param.put("keyword","%"+keyword+"%");

param.put("type",type);

List sources=sourceDao.search(param);

对应的Mapper取值:

parameterType="java.util.Map"

resultType="com.bonc.wechat.entity.publicserver.Source">

select * from wx_source

where

(title like #{keyword} or content like #{keyword}) and

type=#{type}

order by ordernum asc

limit #{page},#{pageSize};

4、参数为对象。

mapper中使用对象中的属性直接取值,或者【对象.属性】取值。

dao中的方法。

/*****使用对象传参*****/

public int addUserComment(UserComment UC);

/*****对象中的属性*****/

private int id;

private int department_id;

private String telphone;

private String content;

private String createtime;

对应的Mapper取值:

parameterType="com.bonc.wechat.entity.publicserver.UserComment">

insert into wx_user_comment

(department_id,

telphone,

content,

createtime)

values(#{department_id},

#{telphone},

#{content},

#{createtime});

*使用【对象.属性】取值。

dao层:

/******此示例中直接省去dao层,service直接绑定mapper层******/

public PageResult findPanoramaPage(Page page) throws Exception{

List panoramaList = new ArrayList();

try {

panoramaList = (List)dao.findForList("PanoramaMapper.findPagePanorama", page);

} catch (Exception e) {

e.printStackTrace();

}

PageResult pageResult = new PageResult(page.getTotalResult(),panoramaList);

return pageResult;

}

对应的Mapper取值:

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

AND a.project_type = #{page.projectType}

AND (a.project_name LIKE '%${page.keyword}%' OR a.project_code LIKE '%${page.keyword}%')

ORDER BY

a.sort

推荐使用第三和第四种,将所传的数据在controller层或者service层封装起来,传入mapper文件中取值。


版权声明:本文为weixin_28481133原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。