mybatis

mybiatis-config.xml




    
    
        
            
            
            
                
                
                
                
                
            
        
    
    
    
        
    

mapper.xml





    
        
        
        
        
    
    
        insert into tab_user values(null,#{uname},#{pwd},#{js});
    
    
    
        delete from tab_user where id=#{id}
    
    
    
        delete from tab_user where id in
        
            #{id}
        
    
    
    
        update tab_user
        
            
                
                    uname=#{uname}
                
                
                    pwd=#{pwd}
                
                
                    js=#{js}
                
            
        
        where id=#{id}
    
    
    
    
    
    
    
    
    
    
    


测试

//开发mybatis第1步读取mybatis配置文件
    Reader resourceAsReader = Resources.getResourceAsReader("config/mybatis-config.xml");
    //开发mybatis第2步创建mybatis的工厂模式:SqlSessionFactory 
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsReader);
    //开发mybatis第3步创建mybatis的session
    SqlSession session = factory.openSession();
    //开发mybatis第4步通过session操作数据库
    List list=session.selectList("listDept");
    for(Dept dd:list){
        System.out.println(dd.getDid()+" "+dd.getDname());
        //if(dd.getDname().equals("开发部")){
            Set set = dd.getSet();
            for(Person p:set){
                System.out.println(p.getName()+ " "+p.getDegree());
            }
        //}
    }
    List list1=session.selectList("list");
    for(Person p:list1){
        System.out.println(p.getName()+ " "+p.getDegree());
    }
    //开发mybatis第5步执行事务提交(查询查以 不需要)
    session.commit();
    //开发mybatis第6步关闭连挡
    session.close();

你可能感兴趣的:(mybatis)