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
1<select id="selectUser" resultMap="BaseResultMap"> 2 select * from user_user_t where user_name = #{0} and user_area=#{1} 3</select>
其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。
##第二种方案 此方法采用Map传多参数.
Dao层的函数方法
public User selectUser(Map paramMap);
对应的Mapper.xml
1<select id="selectUser" resultMap="BaseResultMap"> 2 select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR} 3</select>
Service层调用
1private User xxxSelectUser(){ 2 Map paramMap=new hashMap(); 3 paramMap.put(“userName”,”对应具体的参数值”); 4 paramMap.put(“userArea”,”对应具体的参数值”); 5 User user=xxx. selectUser(paramMap); 6}
个人认为此方法不够直观,见到接口方法不能直接的知道要传的参数是什么。
##第三种方案
Dao层的函数方法
public User selectUser(@param(“userName”)String name,@param(“userArea”)String area);
对应的Mapper.xml
1<select id="selectUser" resultMap="BaseResultMap"> 2 select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR} 3</select>
个人觉得这种方法比较好,能让开发者看到dao层方法就知道该传什么样的参数,比较直观,个人推荐用此种方案。
##mybatis if标签判断的问题
细节可以参考XML代码:
1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <mapper namespace="com.lypaydb.mapper.Order_DayMapper"> 5 <resultMap type="com.lypaydb.pojo.Order_Day" id="odMap"> 6 <id property="id" column="id" /> 7 <result property="date" column="date" /> 8 <result property="total" column="total" /> 9 <result property="aisle" column="aisle" /> 10 <result property="operators" column="operators" /> 11 <result property="channelid" column="channelid" /> 12 <result property="appid" column="appid" /> 13 <result property="paycnt" column="paycnt" /> 14 </resultMap> 15 16 <!-- /*sql --> 17 <select id="findod" parameterType="java.util.Map" resultMap="odMap"> 18 select * from order_day where 1 = 1 19 <if test="${start} != null and ${start != ''}"> 20 and date >= #{start} 21 </if> 22 <if test="${end} != null and ${end} != ''"> 23 and #{end} > date 24 </if> 25 <if test="appid != null and appid != ''"> 26 and appid=${appid} 27 </if> 28 <if test="operators != null and operators != ''"> 29 and operators=${operators} 30 </if> 31 limit ${Page.startPos},${Page.pageSize}; 32 </select> 33 34 <select id="getAllCount" parameterType="java.util.Map" resultType="java.lang.Integer"> 35 select count(*) from order_day where 1=1 36 <if test="${start} != null and ${start != ''}"> 37 and date >= #{start} 38 </if> 39 <if test="${end} != null and ${end} != ''"> 40 and #{end} > date 41 </if> 42 <if test="appid != null and appid != ''"> 43 and appid=${appid} 44 </if> 45 <if test="operators != null and operators != ''"> 46 and operators=${operators} 47 </if> 48 </select> 49 <!-- sql*/ --> 50</mapper>
这里是正确的写法:POJO
1<select id="findod" parameterType="map" resultMap="odMap"> 2 select * from order_day where 1 = 1 3 <if test="start != null and start != ''"> 4 and date >= #{start} 5 </if> 6 <if test="end != null and end != ''"> 7 and #{end} > date 8 </if> 9 <if test="appid != null and appid != ''"> 10 and appid=${appid} 11 </if> 12 <if test="operators != null and operators != ''"> 13 and operators=${operators} 14 </if> 15 limit ${Page.startPos},${Page.pageSize}; 16</select>
MyBatis,数据库映射这一块。 <if test="end != null and end != ''">and #{end} > date </if>参数是你方法里面传过来参数的实体解析,或者键值对的解析。parameterType是参数类型。可以是map,也可以是你的实体类(完整的包名)
##foreach的使用
1<insert id="insertUserList"> 2 INSERT INTO user(username,password) 3 VALUES 4 <foreach collection="userList" item="user" separator=","> 5 (#{user.username},#{user.password}) 6 </foreach> 7</insert>
对应的接口:
int insertUserList(@Param("userList")List<User> list);