数据库ORM框架mybatis 学习篇

数据库ORM框架mybatis 学习篇

1,老套路加载依赖库或者jar


  org.mybatis
  mybatis
  3.4.7
```
 
        
            mysql
            mysql-connector-java
            5.1.18
        


2.配置mybatis-config.xml




    
      
     
  
    
      
      
        
        
        
        
      
    
  
  
    
  

3 配置"DemoMapper.xml





	



4测试 注明(持久化对象,创建数据库略过)这里认为你是 熟悉Java的童鞋

	
@Test*斜体样式*
public void testMybatis() {
	String resource = "mybatis-config.xml";**//注意配置classpath 下**
	InputStream inputStream;
	try {
		inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
	   SqlSession s= sqlSessionFactory.openSession();
	  Demo d= (Demo)s.selectOne("bean.Demo.UserMapper.selectUserByID", 3);
	  System.out.println("----"+d.getId());
	  System.out.println("----"+d.getName());
	   
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}

你可能感兴趣的:(java,框架修炼)