SSM(7)-mybaits整合

分三步走:
1.导入jar包:
2.写配置文件:mybatis-config.xml和mapper映射文件
3,JAVA类实现与调用



1.导入jar包: 1.1junit 1.2mybatis 1.3mysql-connector-java 1.4spring相关 1.5aspectJ AOP 织入器 1.6mybatis-spring整合包

2.写配置文件:mybatis-config.xml和mapper映射文件
mybatis-config.xml

 





   
       
   

   
       
           
           
               
               
               
               
           
       
   

   
       
   




   



3,JAVA类实现与调用
 

package com.tang.pojo;

public class User {
   private int id;        //id
   private String name;   //姓名
   private String pwd;   //密码
}
public interface UserMapper {
   public List selectUser();
}




//测试类
@Test
public void selectUser() throws IOException {

   String resource = "mybatis-config.xml";
   InputStream inputStream = Resources.getResourceAsStream(resource);
   SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
   SqlSession sqlSession = sqlSessionFactory.openSession();

   UserMapper mapper = sqlSession.getMapper(UserMapper.class);

   List userList = mapper.selectUser();
   for (User user: userList){
       System.out.println(user);
  }

   sqlSession.close();
}


 

你可能感兴趣的:(JAVA,数据库)