mybatis----返回集合类型和分步查询

接上一个例子,在原有的基础上增加了些配置或代码。
mybaits配置文件





  



	
		
		
		


  
    
      
      
        
        
        
      
    
  
  
   
   
   
  
  
  
    
    
  
  


第一个sql映射文件








 
  
  
  
  	update customers set name=#{param2},address=#{home} 
  	where id=#{id}
  
  
  
    	delete from customers where id=#{id}
  
  
  
  
  	insert into customers (name,address) value(#{name},#{home})
  
  
  
  
  
 
  
  

新增的在这里

 
  

对应增加接口方法

		public List getCustomersByDeptId(int deptId);

新增sql映射文件





	
	
	
	
	
		
			
		
		
		


	
	


  

测试

package top.demo.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

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 com.mysql.jdbc.DatabaseMetaData;

import top.demo.bean.Customer;
import top.demo.inter.DepartmentDao;
import top.demo.inter.TestSqlInterface;

public class Test {

	public static void main(String argv[]) throws IOException
	{
		String resource = "mybatisconfig.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		System.out.println(sqlSessionFactory);
		//构造参数传入true为开启自动提交  如果为false需要在之后手动commit
		SqlSession session=sqlSessionFactory.openSession(true);
		TestSqlInterface testDao = (TestSqlInterface) session.getMapper(TestSqlInterface.class);
		System.out.println(testDao.getOne(1));
		//testDao.updateInfo(1, "你好2222", "中国");
		//System.out.println(testDao.getOne(1));
		System.out.println(testDao.getSize());
		
		
		/*
		 * 接口参数直接用bean xml里用bean的属性
		 * */
		
		/*Customer customer=new Customer("这是测试的111","火星");
		boolean res=testDao.insert(customer);
		System.out.println(res);
		System.out.println(customer.getId());
		
		System.out.println(testDao.getOne(customer.getId()));*/
		
		
		/*
		 * 测试map作为接口的参数  且 映射xml里直接用map的key获取value
		 * */
		
		/*Map map=new HashMap<>();
		map.put("id", 10);
		
		System.out.println(testDao.deleteOne(map));*/
		
		
		//测试集合类型
		System.out.println(testDao.getCustomersByDeptId(1).size());
		DepartmentDao departmentDao = session.getMapper(DepartmentDao.class);
		//测试分步查询
		System.out.println(departmentDao.getDeptByIdOfFullInfo(1));
	
		session.close();
	}
	
}

mybatis----返回集合类型和分步查询_第1张图片

你可能感兴趣的:(JavaEE)