mybatis简单案例源码详细【注释全面】——测试层(UserMapperTest.java)

 /**  
* @Title: UserMapperTest.java
* @Package org.test
* @Description: TODO该方法的主要作用:
* @author A18ccms A18ccms_gmail_com  
* @date 2017-10-5 下午7:51:50
* @version V1.0  
*/
package org.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.apache.log4j.Logger;
import org.dao.UserMapper;
import org.entity.Users;
import org.junit.Test;
import org.util.MybatisUtils;

 /**   
 *    
 * 项目名称:ssm_chop1   
 * 类名称:UserMapperTest   
 * 类描述:   
 * 创建人:Mu Xiongxiong  
 * 创建时间:2017-10-5 下午7:51:50   
 * 修改人:Mu Xiongxiong   
 * 修改时间:2017-10-5 下午7:51:50   
 * 修改备注:   
 * @version    
 *    
 */
public class UserMapperTest {
	
	/**
	* @Fields logger :该字段的意思:日志
	*/
	private Logger logger = Logger.getLogger(UserMapperTest.class);
	/**
	* @Fields sqlSession :该字段的意思:sqlsession
	*/
	private SqlSession sqlSession = null;
	/**
	* @Fields rel :该字段的意思:区分增删改的是否成功
	*/
	private int rel = 0;
	
	/**
	 * 
	* @Description: 该方法的主要作用:不是用工具类进行测试
	* @Title: test
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_noMybatisUtil(){
		
		String resource = "mybatis-config.xml";
		int count = 0;
		SqlSession sqlSession  = null;
		try {
			//1.获取mybatis-config.xml的输入流
			InputStream inputStream = Resources.getResourceAsStream(resource);
			//2.创建SqlSessionFaction对象,完成对配置文件的读取
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
			//3.创建sqlsession
			sqlSession = factory.openSession();
			//4.调用mapper文件来对数据进行操作,必须把mapper文件引入mybatis-config.xml中
			count = sqlSession.selectOne("org.dao.UserMapper.count");
			logger.debug("UserMapperTest count----"+count);
			System.out.println(count);
		} catch (IOException e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			sqlSession.close();
		}
		
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:使用工具类进行测试
	* @Title: test_MybatisUtils
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_MybatisUtils(){
		SqlSession sqlSession = null;
		int count = 0;
		try {
			sqlSession = MybatisUtils.createSqlSession();
			count = sqlSession.selectOne("org.dao.UserMapper.count");
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
		System.out.println(count);
		
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:调用接口进行查询
	* @Title: test_Icount
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_Icount(){
		SqlSession sqlSession = null;
		int count = 0;
		try {
			sqlSession = MybatisUtils.createSqlSession();
			//接口UserMapper的名字必须要和映射文件的名字一样
			count = sqlSession.getMapper(UserMapper.class).count();
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
		System.out.println("总记录数是:"+count);
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:使用接口的方式查询所有的数据
	* @Title: test_IgetUserList
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUserList(){
		SqlSession sqlSession = null;
		List<Users> userList = new ArrayList<Users>();
		
		try {
			sqlSession = MybatisUtils.createSqlSession();
			userList = sqlSession.getMapper(UserMapper.class).getUserList();
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
		
		for (Users users : userList) {
			System.out.println("用户名是"+users.getUserName());
		}
		
		
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:根据用户名进行模糊查询信息
	* @Title: test_IgetUsersByName
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUsersByName(){
		SqlSession sqlSession  = null;
		Users users = null;
		
		try {
			sqlSession = MybatisUtils.createSqlSession();
			users = sqlSession.getMapper(UserMapper.class).getUsersByName("明");
			
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
		
		System.out.println(users.getPhone());
		
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:根据用户对象进行查询
	* @Title: test_IgetUserListByUser
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUserListByUser(){
		SqlSession sqlSession = null;
		List<Users> users1 = null;
		Users users = new Users();
		
		try {
			users.setUserName("李");
			users.setUserrole(2);
			sqlSession = MybatisUtils.createSqlSession();
			users1  = sqlSession.getMapper(UserMapper.class).getUserListByUser(users);
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
		for (Users user : users1) {
			System.out.println(user.getUserName());
		}
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:查询用户列表,参数是Map
	* @Title: test_IgetUsersListByMap
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUsersListByMap(){
		SqlSession sqlSession = null;
		List<Users> usersList = null;
		
		try {
			sqlSession = MybatisUtils.createSqlSession();
			Map<String, String> map = new HashMap<String, String>();
			map.put("userName", "李");
			map.put("userrole", "2");
			usersList = sqlSession.getMapper(UserMapper.class).getUsersListByMap(map);
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
		for (Users user : usersList) {
			System.out.println(user.getUserName());
		}
		
	}

	/**
	 * 
	* @Description: 该方法的主要作用:连接查询
	* @Title: test_IgetUserListAndRole
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUserListAndRole(){
		SqlSession sqlSession = null;
		List<Users> userList = null;
		
		try {
			sqlSession = MybatisUtils.createSqlSession();
			Users users = new Users();
			users.setUserName("李");
			users.setUserrole(2);
			userList = sqlSession.getMapper(UserMapper.class).getUserListAndRole(users);
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
		
		
		for (Users users : userList) {
			System.out.println(users.getAddress());
		}
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:添加用户信息
	* @Title: test_IsaveUser
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IsaveUser(){
		try {
			sqlSession =  MybatisUtils.createSqlSession();
			Users users  = new Users();
			users.setAddress("山东省济南市");
			users.setBirthday(new Date());
			users.setCreateBy(1);
			users.setCreationDate(new Date());
			users.setGender("1");
			users.setModifyBy(2);
			users.setModifyDate(new Date());
			users.setPhone("15066675713");
			users.setUserCode("wangwei");
			users.setUserName("王伟");
			users.setUserPassword("123456");
			users.setUserrole(1);
			rel = sqlSession.getMapper(UserMapper.class).saveUser(users);
			if(rel>0){
				System.out.println("添加成功");
			}else{
				System.out.println("添加失败");
			}
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			sqlSession.commit();
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:根据编号修改
	* @Title: test_IupdateUser
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IupdateUser(){
		try {
			sqlSession  = MybatisUtils.createSqlSession();
			Users users  = new Users();
			users.setAddress("山东省济南市");
			users.setBirthday(new Date());
			users.setCreateBy(1);
			users.setCreationDate(new Date());
			users.setGender("1");
			users.setModifyBy(2);
			users.setModifyDate(new Date());
			users.setPhone("15066675713");
			users.setUserCode("wangwei");
			users.setUserName("李伟");
			users.setUserPassword("123456");
			users.setUserrole(1);
			users.setId(19);
			rel = sqlSession.getMapper(UserMapper.class).updateUser(users);
			if(rel>0){
				System.out.println("修改成功");
			}else{
				System.out.println("修改失败");
			}
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			sqlSession.commit();
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}
	
	/**
	* @Description: 该方法的主要作用:根据id删除数据
	* @Title: test_IdelUser
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IdelUser(){
		try {
			sqlSession = MybatisUtils.createSqlSession();
			rel = sqlSession.getMapper(UserMapper.class).delUser(19);
			if(rel>0){
				System.out.println("删除成功!");
			}else{
				System.out.println("删除失败!");
			}
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			sqlSession.commit();
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:根据id查询用户信息
	* @Title: test_IgetUsersById
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUsersById(){
		try {
			sqlSession = MybatisUtils.createSqlSession();
			Users users = sqlSession.getMapper(UserMapper.class).getUsersById(15);
			System.out.println(users.getUserName());
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:根据角色id查询用户信息
	* @Title: test_IgetUsersByRoleId
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUsersByRoleId(){
		try {
			sqlSession = MybatisUtils.createSqlSession();
			List<Users> userList = sqlSession.getMapper(UserMapper.class).getUsersByRoleId(2);
			for (Users users : userList) {
				System.out.println(users.getUserName());
			}
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:根据userName和userrole动态查询用户信息
	* @Title: test_IgetUsersListByUserNameAndRole_if
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUsersListByUserNameAndRole_if(){
		try {
			sqlSession = MybatisUtils.createSqlSession();
			List<Users> userList = sqlSession.getMapper(UserMapper.class).getUsersListByUserNameAndRole_if(null, 3);
			for (Users users : userList) {
				System.out.println(users.getUserName());
			}
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:动态根据用户名和角色id查询用户列表,where and|or
	* @Title: getUsersListByUserNameAndRole_ifAndwhere
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUsersListByUserNameAndRole_ifAndwhere(){
		try {
			sqlSession = MybatisUtils.createSqlSession();
			List<Users> userList = sqlSession.getMapper(UserMapper.class).getUsersListByUserNameAndRole_ifAndwhere(null, 2);
			for (Users users : userList) {
				System.out.println(users.getUserName());
			}
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:使用is+set动态修改用户表信息
	* @Title: updateUser_ifAndSet
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IupdateUser_ifAndSet(){
		try {
			sqlSession  = MybatisUtils.createSqlSession();
			Users users  = new Users();
			users.setAddress("山东省济南市");
			users.setBirthday(new Date());
			users.setCreateBy(1);
			users.setCreationDate(new Date());
			users.setGender("1");
			users.setModifyBy(2);
			users.setModifyDate(new Date());
			users.setPhone("15066675713");
			users.setUserCode("wangwei");
			users.setUserName("李伟");
			users.setUserPassword("123456");
			users.setUserrole(1);
			users.setId(15);
			rel = sqlSession.getMapper(UserMapper.class).updateUser_ifAndSet(users);
			if(rel>0){
				System.out.println("修改成功");
			}else{
				System.out.println("修改失败");
			}
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			sqlSession.commit();
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}

	/**
	 * 
	* @Description: 该方法的主要作用:动态根据用户名和角色id查询用户列表,使用trim进行查询用户信息,where and|or
	* @Title: getUsersListByUserNameAndRole_ifAndwhere_trim
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUsersListByUserNameAndRole_ifAndwhere_trim(){
		try {
			sqlSession = MybatisUtils.createSqlSession();
			List<Users> userList = sqlSession.getMapper(UserMapper.class)
						.getUsersListByUserNameAndRole_ifAndwhere_trim("张", 3);
			for (Users users : userList) {
				System.out.println(users.getUserName());
			}
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}

	/**
	 * 
	* @Description: 该方法的主要作用:使用trim代替set进行动态修改用户信息
	* @Title: updateUser_ifAndTrim
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IupdateUser_ifAndTrim(){
		try {
			sqlSession  = MybatisUtils.createSqlSession();
			Users users  = new Users();
			users.setAddress("山东省济南市");
			users.setBirthday(new Date());
			users.setCreateBy(1);
			users.setCreationDate(new Date());
			users.setGender("1");
			users.setModifyBy(2);
			users.setModifyDate(new Date());
			users.setPhone("15066675713");
			users.setUserCode("wangwei");
			users.setUserName("李伟伟");
			users.setUserPassword("123456");
			users.setUserrole(1);
			users.setId(15);
			rel = sqlSession.getMapper(UserMapper.class).updateUser_ifAndTrim(users);
			if(rel>0){
				System.out.println("修改成功");
			}else{
				System.out.println("修改失败");
			}
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			sqlSession.commit();
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}

	/**
	 * 
	* @Description: 该方法的主要作用:根据用户角色列表,获取该角色刘表下用户列表信息foreach_array
	* @Title: test_IgetUsersByRoleId_foreach_array
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUsersByRoleId_foreach_array(){
		try {
			sqlSession = MybatisUtils.createSqlSession();
			Integer roleids [] = {1,2,3};
			List<Users> userList = sqlSession.getMapper(UserMapper.class).getUsersByRoleId_foreach_array(roleids);
			for (Users users : userList) {
				System.out.println(users.getUserName());
			}
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:根据用户角色列表,获取该角色刘表下用户列表信息foreach_list
	* @Title: getUsersByRoleId_foreach_list
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUsersByRoleId_foreach_list(){
		try {
			sqlSession = MybatisUtils.createSqlSession();
			List<Integer> roleids = new ArrayList<Integer>();
			roleids.add(1);
			roleids.add(2);
			List<Users> userList = sqlSession.getMapper(UserMapper.class).getUsersByRoleId_foreach_list(roleids);
			for (Users users : userList) {
				System.out.println(users.getUserName());
			}
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}

	/**
	 * 
	* @Description: 该方法的主要作用:根据用户角色列表和性别(多参数),获取该角色刘表下用户列表信息foreach_map
	* @Title: test_IgetUsersByRoleId_foreach_many_map
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUsersByRoleId_foreach_many_map(){
		try {
			sqlSession = MybatisUtils.createSqlSession();
			List<Integer> roleids = new ArrayList<Integer>();
			Map<String, Object> conditionmap = new HashMap<String, Object>();
			roleids.add(1);
			roleids.add(2);
			conditionmap.put("gender", 1);
			conditionmap.put("roleids", roleids);
			
			List<Users> userList = sqlSession.getMapper(UserMapper.class).getUsersByRoleId_foreach_many_map(conditionmap);
			for (Users users : userList) {
				System.out.println(users.getUserName());
			}
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:根据用户角色列表(单参数),获取该角色刘表下用户列表信息foreach_map
	* @Title: getUsersByRoleId_foreach_one_map
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUsersByRoleId_foreach_one_map(){
		try {
			sqlSession = MybatisUtils.createSqlSession();
			List<Integer> roleids = new ArrayList<Integer>();
			Map<String, Object> conditionmap = new HashMap<String, Object>();
			roleids.add(1);
			roleids.add(2);
			conditionmap.put("rKey", roleids);
			
			List<Users> userList = sqlSession.getMapper(UserMapper.class).getUsersByRoleId_foreach_one_map(conditionmap);
			for (Users users : userList) {
				System.out.println(users.getUserName());
			}
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}
	
	/**
	 * 
	* @Description: 该方法的主要作用:查询用户列表,使用choose
	* @Title: getUsersList_choose
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUsersList_choose(){
		try {
			sqlSession = MybatisUtils.createSqlSession();
			
			List<Users> userList = sqlSession.getMapper(UserMapper.class).getUsersList_choose(null,null,null,new Date());
			for (Users users : userList) {
				System.out.println(users.getUserName());
			}
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}

	/**
	 * 
	* @Description: 该方法的主要作用:分页查询用户信息
	* @Title: getUserList_page
	* @param   设定文件  
	* @return  返回类型:void   
	* @throws
	 */
	@Test
	public void test_IgetUserList_page(){
		try {
			sqlSession = MybatisUtils.createSqlSession();
			
			List<Users> userList = sqlSession.getMapper(UserMapper.class).getUserList_page(4,3);
			for (Users users : userList) {
				System.out.println(users.getUserName());
			}
		} catch (Exception e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}finally{
			MybatisUtils.closeSqlSession(sqlSession);
		}
	}
}



                                    
posted @ 2017-10-09 12:00  穆雄雄  阅读(223)  评论(0编辑  收藏  举报