您的位置:首页 > 其它

Mybatis传多个参数(三种解决方案)

2016-05-16 00:00 211 查看
Mybatis分页插件 - PageHelpe
http://git.oschina.net/free/Mybatis_PageHelper

极其方便的使用Mybatis单表的增删改查
http://git.oschina.net/free/Mapper

Mybatis示例
http://blog.csdn.net/column/details/mybatis-sample.html

MyBatis官方文档
http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html

##第一种方案

DAO层的函数方法

public User selectUser(String name,String area);

对应的Mapper.xml

<select id="selectUser" resultMap="BaseResultMap">
select  *  from user_user_t   where user_name = #{0} and user_area=#{1}
</select>

其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。

##第二种方案 此方法采用Map传多参数.

Dao层的函数方法

public User selectUser(Map paramMap);

对应的Mapper.xml

<select id="selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}
</select>

Service层调用

private User xxxSelectUser(){
Map paramMap=new hashMap();
paramMap.put(“userName”,”对应具体的参数值”);
paramMap.put(“userArea”,”对应具体的参数值”);
User user=xxx. selectUser(paramMap);
}

个人认为此方法不够直观,见到接口方法不能直接的知道要传的参数是什么。

##第三种方案

Dao层的函数方法

public User selectUser(@param(“userName”)String name,@param(“userArea”)String area);

对应的Mapper.xml

<select id="selectUser" resultMap="BaseResultMap">
select * from user_user_t  where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}
</select>

个人觉得这种方法比较好,能让开发者看到dao层方法就知道该传什么样的参数,比较直观,个人推荐用此种方案。

##mybatis if标签判断的问题

细节可以参考XML代码:

<?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="com.lypaydb.mapper.Order_DayMapper">
<resultMap type="com.lypaydb.pojo.Order_Day" id="odMap">
<id property="id" column="id" />
<result property="date" column="date" />
<result property="total" column="total" />
<result property="aisle" column="aisle" />
<result property="operators" column="operators" />
<result property="channelid" column="channelid" />
<result property="appid" column="appid" />
<result property="paycnt" column="paycnt" />
</resultMap>

<!-- /*sql -->
<select id="findod" parameterType="java.util.Map" resultMap="odMap">
select * from order_day where 1 = 1
<if test="${start} != null and ${start != ''}">
and   date >= #{start}
</if>
<if test="${end} != null and ${end} != ''">
and #{end} > date
</if>
<if test="appid != null and appid != ''">
and  appid=${appid}
</if>
<if test="operators != null and operators != ''">
and operators=${operators}
</if>
limit ${Page.startPos},${Page.pageSize};
</select>

<select id="getAllCount" parameterType="java.util.Map" resultType="java.lang.Integer">
select count(*)  from order_day where 1=1
<if test="${start} != null and ${start != ''}">
and   date >= #{start}
</if>
<if test="${end} != null and ${end} != ''">
and #{end} > date
</if>
<if test="appid != null and appid != ''">
and  appid=${appid}
</if>
<if test="operators != null and operators != ''">
and operators=${operators}
</if>
</select>
<!-- sql*/ -->
</mapper>

这里是正确的写法:POJO

<select id="findod" parameterType="map" resultMap="odMap">
select * from order_day where 1 = 1
<if test="start != null and start != ''">
and   date >= #{start}
</if>
<if test="end != null and end != ''">
and #{end} > date
</if>
<if test="appid != null and appid != ''">
and  appid=${appid}
</if>
<if test="operators != null and operators != ''">
and operators=${operators}
</if>
limit ${Page.startPos},${Page.pageSize};
</select>

MyBatis,数据库映射这一块。 <if test="end != null and end != ''">and #{end} > date </if>参数是你方法里面传过来参数的实体解析,或者键值对的解析。parameterType是参数类型。可以是map,也可以是你的实体类(完整的包名)

##foreach的使用

<insert id="insertUserList">
INSERT INTO user(username,password)
VALUES
<foreach collection="userList" item="user" separator=",">
(#{user.username},#{user.password})
</foreach>
</insert>

对应的接口:

int insertUserList(@Param("userList")List<User> list);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: