Mybatis学习笔记(5)--parameterType输入类型

parameterType查询条件的输入类型分为简单类型和复杂类型,简单输入类型,如string ,int等,复杂类型如,pojo包装类,数组类型,集合类型,

UserMapper.xml文件






	
	
	
	
	

	
	
	

	
	
	
	
			
			
	
	
	
	

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

	
	
		UPDATE `user` SET
		username = #{name},
		address = #{address},
		birthday = #{birthday} WHERE id = #{id}
	

	
	
		delete from user where
		id=#{id}
	




2.QueryVo封装的pojo包装类


/**     
 * @文件名称: QueryVo.java   
 * @描述: TODO  
 * @作者:  一叶扁舟(skiff)
 * @时间:2018年1月12日 下午3:39:25  
 * @版本:V1.0     
 */ 
package com.skiff.www.domain.vo;

import java.util.List;

import com.skiff.www.domain.User;

/**  
 * @类功能说明:    queryVo中的一个属性是一个实体类
 * @作者: 一叶扁舟 (skiff)
 * @创建时间:2018年1月12日 下午3:39:25  
 * @版本:V1.0  
 */
public class QueryVo {
	private User user;
	private Integer[] idsArray;//数组
	private List idsList;//list集合

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the idsArray
	 */
	public Integer[] getIdsArray() {
		return idsArray;
	}

	/**
	 * @param idsArray the idsArray to set
	 */
	public void setIdsArray(Integer[] idsArray) {
		this.idsArray = idsArray;
	}

	/**
	 * @return the idsList
	 */
	public List getIdsList() {
		return idsList;
	}

	/**
	 * @param idsList the idsList to set
	 */
	public void setIdsList(List idsList) {
		this.idsList = idsList;
	}
	

}


3.junit测试类

/**     
 * @文件名称: UserMapperTest.java   
 * @描述: TODO  
 * @作者:  一叶扁舟(skiff)
 * @时间:2018年1月9日 下午6:14:34  
 * @版本:V1.0     
 */ 
package test.com.skiff.www.testUser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import com.skiff.www.domain.User;
import com.skiff.www.domain.vo.QueryVo;
import com.skiff.www.mapper.UserMapper;


/**  
 * @类功能说明:    
 * @作者: 一叶扁舟 (skiff)
 * @创建时间:2018年1月12日 下午3:41:07  
 * @版本:V1.0  
 */  
public class UserMapperVo {
	private SqlSessionFactory sqlSessionFactory;

	@Before
	public void init() throws Exception {
		InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
		this.sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
	}
	@Test
	public void parameterType1(){
		SqlSession session = this.sqlSessionFactory.openSession();
		UserMapper mapper = session.getMapper(UserMapper.class);
		QueryVo vo = new QueryVo();
		User user = new User();
		user.setName("无悔");
		vo.setUser(user);
		List users = mapper.queryUserByQueryVo(vo);//pojo包装对象
		for (User user2 : users) {
			System.out.println(user2);
		}
	}
	
	@Test
	public void parameterType2(){
		SqlSession session = this.sqlSessionFactory.openSession();
		UserMapper mapper = session.getMapper(UserMapper.class);
		
		QueryVo vo = new QueryVo();
		List ids = new ArrayList();
		ids.add(1);
		ids.add(24);
		ids.add(25);
		vo.setIdsList(ids);
		List users = mapper.queryUserByparameterTypeList(vo);//list集合
		for (User user2 : users) {
			System.out.println(user2);
		}
	}

	@Test
	public void parameterType3(){
		SqlSession session = this.sqlSessionFactory.openSession();
		UserMapper mapper = session.getMapper(UserMapper.class);
		
		Integer[] ids = new Integer[3];
		ids[0] = 1;
		ids[1] = 24;
		ids[2] = 25;
		vo.setIdsArray(ids);
		List users = mapper.queryUserByparameterTypeArray(ids);//数组
		for (User user2 : users) {
			System.out.println(user2);
		}
	}


}








你可能感兴趣的:(mybatis)