MyBatis动态语句及利用Map类来传递参数

之前通过自定义的bo、po、vo类以及写多个相应的resultmap来接受数据库的数据。在程序前后端接口有改动时,往往会因为这些bo、po、vo类不满足要求,要牵一发而动全身。现在,通过动态的MyBatis以及把参数写成map类型,并用map来接受数据库的数据,可以极大地简化程序,增强代码的复用性。

附代码如下:

mapper层的UserMapper.xml,及UserMapper.java





	
	
		
		
			
				${tablename}
			
			
				${tablename}
			
			
				${tablename}
			
			
				${tablename}
			
			
				${tablename}
			
		
	

	
	
		
			
				and ID=#{ID}
			
			
				and Name = #{Name}
			
			
				and Tel = #{Tel}
			
			
				and Balance = #{Balance}
			
			
				and timestamp = #{timestamp}
			
		
	
	
	
	
		
			
				and ID like CONCAT('%',#{ID},'%') 
			
			
				and Name like CONCAT('%',#{Name},'%') 
			
			
				and Tel like CONCAT('%',#{Tel},'%') 
			
			
				and Balance like CONCAT('%',#{Balance},'%')
			
			
				and timestamp = like CONCAT('%',#{timestamp},'%')
			
		
	
	

	
	
		
		
			
				ID,
			
			
				Name,
			
			
				Tel,
			
			
				Balance,
			
			
				timestamp,
					
		
	

	
	
		
		
			
				#{ID},
			
			
				#{Name},
			
			
				#{Tel},
			
			
				#{Balance},
			
			
				#{timestamp},
					
		
	
	
	
	
			
				ID=#{IDchange},
			
			
				Name = #{Namechange},
			
			
				Tel = #{Telchange},
			
			
				Balance = #{Balancechange},
			
			
				timestamp = #{timestampchange},
			
	
	

	
		insert into 
		
		()
		values
		()
	
	
	
	
	
		
	
		update 
		
		
			
		
		
	
	
	
		update 
		
		
			
		
		
	
	
	
		delete from
		
		
	
	
	
		delete from
		
		
	


	

UserMapper.java

package com.terabits.mapper;
/** 
* @author 作者Vladimir E-mail: [email protected]
* @version 创建时间:2017年12月14日 下午5:09:18 
* 类说明 
*/

import java.util.List;
import java.util.Map;


public interface UserMapper {
	
	public int insertItem(Map map) throws Exception;
	
	public List> selectItem(Map map) throws Exception;
	
	public List> selectItemFuzzy(Map map) throws Exception;
	
	public int updateItem(Map map) throws Exception;
	
	public int updateItemFuzzy(Map map) throws Exception;
	
	public int deleteItem(Map map) throws Exception;
	
	public int deleteItemFuzzy(Map map) throws Exception;

}

DAO层:

package com.terabits.dao;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.terabits.mapper.UserMapper;

/** 
 * @author 作者Vladimir E-mail: [email protected]
 * @version 创建时间:2017年12月14日 下午5:08:17 
 * 类说明 
 */
@Repository("userDAO")
public class UserDAO {

	@Autowired
	private UserMapper userMapper;

	public int insertItem(Map map) throws Exception {
		try {
			userMapper.insertItem(map);
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}

	public List> selectItem(Map map) throws Exception{
		return userMapper.selectItem(map);
	}

	public List> selectItemFuzzy(Map map) throws Exception{
		return userMapper.selectItemFuzzy(map);
	}

	public int updateItem(Map map) throws Exception{
		try {
			userMapper.updateItem(map);
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
		
	}

	public int updateItemFuzzy(Map map) throws Exception{
		try {
			userMapper.updateItemFuzzy(map);
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}

	public int deleteItem(Map map) throws Exception{
		try {
			userMapper.deleteItem(map);
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}

	public int deleteItemFuzzy(Map map) throws Exception{
		try {
			userMapper.deleteItemFuzzy(map);
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}

}



你可能感兴趣的:(MyBatis动态语句及利用Map类来传递参数)