MyBatis使用

原文链接:http://blog.csdn.net/qq_22329521/article/details/75051031

导包

  • 下载mybaits https://github.com/mybatis/mybatis-3/releases
  • 下载连接mysql驱动

配置sqlMapconfig.xml文件




    
    
        
            
            
            
            
                
                
                
                
            
        
    
    
    
        
    

User.xml






    
    

    
    
       
    
    
    

    
         

     
         select LAST_INSERT_ID()
     
      insert into user (username,birthday,address,sex) VALUE (#{username},#{birthday},#{address},#{sex})
    

    
        update user set username=#{username},sex=#{sex},birthday=#{birthday},address=#{address}
        where id=#{id}
    
    
    
        DELETE from user where id=#{id}
    

增删改查


   @Test
    public void fun1() throws IOException {
//        加载核心配置文件
        String resource="sqlMapConfig.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        //创建sqlSessionFactory
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //创建sqlsession
        SqlSession sqlSession = factory.openSession();
        /**
                User o = sqlSession.selectOne("test.findUserById", 10);
        System.out.println(o);

        List users=sqlSession.selectList("test.findUserByName","五");
        for (User u:users){
            System.out.println(u);
        }
        */
        /**
              User user = new User();
        user.setAddress("北京");
        user.setSex("男");
        user.setBirthday(new Date());
        user.setUsername("富媒体");
        int i= sqlSession.insert("test.addUser",user);
        sqlSession.commit();

        Integer id = user.getId();
        System.out.println(id);
        */
        /**
          User user = new User();

        user.setAddress("上海");
        user.setSex("男");
        user.setBirthday(new Date());
        user.setUsername("富媒体");
        user.setId(28);
        int i= sqlSession.update("test.updateUserById",user);
        sqlSession.commit();
        */
        /*
           int i= sqlSession.delete("test.deleteUserById",28);
        sqlSession.commit();
        */
    }
  

封装dao调用getMapper方法

public interface UserMapper {

    //遵循4个原则
    //接口名字==User.xml中的id
    //返回类型与Mapper.xml中的返回类型一直
    //方法的入参与Mapper.xml的入参一致
    //命名空间绑定接口
    List findUserByQueryVo(QueryVo vo);

    public Integer countUser();

}

 

    

    @Test
    public void fun5() throws IOException {
//        加载核心配置文件
        String resource="sqlMapConfig.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        //创建sqlSessionFactory
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //创建sqlsession
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        QueryVo queryVo = new QueryVo();
        User user = new User();
        user.setUsername("五");
        queryVo.setUser(user);
        List userByQueryVo = mapper.findUserByQueryVo(queryVo);
        for (User u:userByQueryVo){
            System.out.println(u);
        }
        System.out.println(mapper.countUser());
    }

如果数据库字段与对象中的字段不一致使用resultmap来处理


    
        
        
    
    
    

动态sql

  • if/where
    

别把and放后面 比如 username=#{username} and

    User user = new User();
//        user.setSex("1");
        user.setUsername("张小明");
        List users = mapper.selectUserBySexAndUserName(user);
        for (User or:users){
            System.out.println(or);
        }
  • sql片段:提取公共是sql语句
  
        SELECT * FROM  user
    
    
  • foreach

 
    
        
        
            
            
                
            
        
    
   
    
 QueryVo queryVo = new QueryVo();
        ArrayList integers = new ArrayList<>();
        integers.add(24);
        integers.add(22);
        queryVo.setList_ids(integers);

        List users = mapper.selectUserByIds(queryVo);

 Integer[] integers=new Integer[2];
        integers[0]=24;
        integers[1]=22;
     List users = mapper.selectUserByIds(integers);
        for (User or:users){
            System.out.println(or);
        }

一对一关联

    
    
        
        
        
        
        
            
            
        
    
    
  List selectOrdersList = mapper.selectOrders();

        for (Orders orders : selectOrdersList) {
            System.out.println(orders);
        }

一对多

    
    
        
        
        
        
            
            
        
    
    

spring与mybatis结合

sqlMapConfig.xml




    
    
        
        
    

    
        
    



applicationContext.xml





    
    
    
    
        
        
        
        
        
        
    
    
    
    
        
        
        
    


    
    
        
    

    
     
         
         
     

    
    
        
        
    

public class test {
    @Test
    public void test(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //这是mapper动态代理
//        UserMap userMapper = (UserMap) ac.getBean("userMapper");
//这是Mapper扫描基本包
        UserMap userMap=ac.getBean(UserMap.class);
        User userById = userMap.findUserById(10);
        System.out.print(userById);
    }
}

mybatis 逆向工程(mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程,可以针对单表自动生成mybatis执行所需要的代码)

http://blog.csdn.net/h3243212/article/details/50778937

mybatis与hibernate的不同

Mybatis不完全是ORM框架, 因为Mybatis需要程序员自己写sql预计,程序员直接编写原生态sql,可严格控制sql执行性能,灵活度高,但是mybatis无法做到数据库无关性(如果换数据库sql需要重写),hibernate数据无关性强

别人总结很具体:http://www.cnblogs.com/inspurhaitian/p/4647485.html

参考文章http://www.cnblogs.com/inspurhaitian/p/4647485.html

你可能感兴趣的:(MyBatis使用)