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层的函数方法
对应的Mapper.xml
其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。
##第二种方案 此方法采用Map传多参数.
Dao层的函数方法
对应的Mapper.xml
Service层调用
个人认为此方法不够直观,见到接口方法不能直接的知道要传的参数是什么。
##第三种方案
Dao层的函数方法
对应的Mapper.xml
个人觉得这种方法比较好,能让开发者看到dao层方法就知道该传什么样的参数,比较直观,个人推荐用此种方案。
##mybatis if标签判断的问题
细节可以参考XML代码:
这里是正确的写法:POJO
MyBatis,数据库映射这一块。 <if test="end != null and end != ''">and #{end} > date </if>参数是你方法里面传过来参数的实体解析,或者键值对的解析。parameterType是参数类型。可以是map,也可以是你的实体类(完整的包名)
##foreach的使用
对应的接口:
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);
相关文章推荐
- springmvc的拦截器,怎么设置不拦截的url
- php+mysql 判断记录位置
- 1164 - Horrible Queries
- HTML5中canvas知识点详解1-用HTML5绘制一个简单的矩形
- HTML5中canvas知识点详解2-HTML5中路径的使用
- HTML5中canvas知识点详解3-绘制渐变图形
- HTML5中canvas知识点详解4-绘制变形图形
- HTML5中canvas知识点详解5-图形的组合
- HTML5中canvas知识点详解6-给图像绘制阴影
- HTML5中canvas知识点详解7-使用图像
- HTML5中canvas知识点详解8-绘制文字
- HTML5多媒体播放知识点总结
- MySQL客户端程序及一些使用小工具介绍
- Linux下Oracle GoldenGate(OGG)的配置
- mysql主从复制环境部署
- springmvc 基于注解的controller
- servlet一般防止表单重复提交
- struts2防止重复提交
- struts2小案例 包括防止重复提交 转换器
- php中的自动加载类机制原理