1, 参数个数为 1 个 (string 或者 int)
dao 层方法为以下两种:
- [java] view plain https://blog.csdn.net/abubu123/article/details/53282894# copy https://blog.csdn.net/abubu123/article/details/53282894#
- /**
- * 单个 int 型
- */
- public List<UserComment> findByDepartmentId(int dapartmentId);
- /**
- * 单个 string 型
- */
- public Source findByTitle(String title);
对应的 Mapper 取值:
取值时应当注意, 参数名字应该与 dao 层传入的参数名字相同.
- [html] view plain https://blog.csdn.net/abubu123/article/details/53282894# copy https://blog.csdn.net/abubu123/article/details/53282894#
- /* 单个 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 层方法:
- [java] view plain https://blog.csdn.net/abubu123/article/details/53282894# copy https://blog.csdn.net/abubu123/article/details/53282894#
- /*****1. 正常传参 *****/
- public Dailyuserinfo findStutaByUserAndDaily(String username,String dailyid);
- /*****2. 注解传参 *****/
- public List<UserTab> selectUserListExceptUserId
- (@Param("USER_ID")String USER_ID,
- @Param("LIMIT_POS")int LIMIT_POS,
- @Param("LIMIT_SIZE")int LIMIT_SIZE);
对应的 Mapper 取值:
取值时应当注意, 参数名字应该与 dao 层传入的参数名字相同.
- [html] view plain https://blog.csdn.net/abubu123/article/details/53282894# copy https://blog.csdn.net/abubu123/article/details/53282894#
- /**** 正常传参方式参数获取 ****/
- <select id="findStutaByUserAndDaily"
- 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};
- </select>
- /**** 注解传参方式参数获取 ****/
- <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 中的方法.
- [java] view plain https://blog.csdn.net/abubu123/article/details/53282894# copy https://blog.csdn.net/abubu123/article/details/53282894#
- /*****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 取值:
- [html] view plain https://blog.csdn.net/abubu123/article/details/53282894# copy https://blog.csdn.net/abubu123/article/details/53282894#
- <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} or content like #{keyword}) and
- </if>
- type=#{type}
- order by ordernum asc
- limit #{page},#{pageSize};
- </select>
4, 参数为对象.
mapper 中使用对象中的属性直接取值, 或者 [对象. 属性] 取值.
dao 中的方法.
- [java] view plain https://blog.csdn.net/abubu123/article/details/53282894# copy https://blog.csdn.net/abubu123/article/details/53282894#
- /***** 使用对象传参 *****/
- public int addUserComment(UserComment UC);
- /***** 对象中的属性 *****/
- private int id;
- private int department_id;
- private String telphone;
- private String content;
- private String createtime;
对应的 Mapper 取值:
- [html] view plain https://blog.csdn.net/abubu123/article/details/53282894# copy https://blog.csdn.net/abubu123/article/details/53282894#
- <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 层:
- [java] view plain https://blog.csdn.net/abubu123/article/details/53282894# copy https://blog.csdn.net/abubu123/article/details/53282894#
- /****** 此示例中直接省去 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 取值:
- [html] view plain https://blog.csdn.net/abubu123/article/details/53282894# copy https://blog.csdn.net/abubu123/article/details/53282894#
- <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 文件中取值.
来源: http://www.bubuko.com/infodetail-2557105.html