Mybatis中各种操作总结


Mybatis中各种操作总结

1.大于等于和小于等于的写法

大于等于
= ]]>
小于等于

例如:sql如下:
create_date_time = ]]> #{startTime} and  create_date_time  #{endTime}

2.参数遍历

index属性:记录遍历的次数。通过#{index}可以获取到是第几次遍历

1. 传入的参数为list的时候

 对应的Dao中的Mapper文件是:
public List selectByIds(List ids);
 
xml文件代码片段:

 
 
2. 传入的参数为Array的时候

对应的Dao中的Mapper文件是:

public List selectByIds(int[] ids);

 

xml文件代码片段: 

 
3. 传入的参数为Map的时候
 
对应的Dao中的Mapper文件是:

public List selectByIds(Map params);

 
xml文件代码片段: 

 
map的时候需要注意的是:collection的值“ids”是存储在map中的key(比如:map.put("ids",ids));尤其需要注意;

4.当传参比较多,各种类型都有时

Dao层代码
List getTodaySuccessList(@Param("vehiclestr") List vehiclestr, @Param("btime") String btime, @Param("etime") String etime);

xml文件代码:
    

3.插入之后返回特定字段


	 	
	 	
	 		SELECT LAST_INSERT_ID()
	 	
	 	INSERT INTO user(username,birthday,sex,address) VALUES(#{username},#{birthday},#{sex},#{address}) 
	 

4.sql复用

	
    
    
        FROM test 
        WHERE id = #{param}