Mybatis 初识

mybatis跟hibernate一样,是持久层方面的,简单点就是数据库连接

mybatis-config.xml : 引入资源文件。然后配置环境,mybatis可以有多个环境,环境配置事务管理、jdbc,datasource


	
	
	
	
	
	
		
		
		
		
		
		
		
		
		
		
	
	
	
	
	

mybatisUtil:建立sessionFactory,再open,获取sqlSeesion

public class MybatisUtils {

    private static SqlSessionFactory sessionFactory;
    
    public static SqlSessionFactory getSessionFactory() throws Exception{
        InputStream inputStream= Resources.getResourceAsStream("mybatis-config.xml");
        sessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        return sessionFactory;
    }
    
    public static SqlSession openSqlSession() throws Exception{
        return getSessionFactory().openSession();
    }
}
mapper文件:对应接口的方法。


  
	
	
	



	insert into student values(null,#{username},#{password})


 
	update student set username=#{username} ,password=#{password} where id=#{id}



	delete from student where id=#{id}


   

   





test文件:

  @Test
    public void add() throws Exception{
        Logger logger=Logger.getLogger(test.class);
        SqlSession session=MybatisUtils.openSqlSession();
        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        int result=  studentMapper.add(new Student( "cai1", "123"));
        if(result==1) logger.info("添加成功");
        session.commit();
    }
    @Test
    public void delete() throws Exception{
        Logger logger=Logger.getLogger(test.class);
        SqlSession session=MybatisUtils.openSqlSession();
        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        int result=  studentMapper.delete(new Student(2));
        if(result==1) logger.info("添加成功");
        session.commit();
    }
    @Test
    public void update() throws Exception{
        Logger logger=Logger.getLogger(test.class);
        SqlSession session=MybatisUtils.openSqlSession();
        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        int result=  studentMapper.update(new Student(4,"caica222i","123"));
        if(result==1) logger.info("添加成功");
        session.commit();
    }
    @Test
    public void findById() throws Exception{
        Logger logger=Logger.getLogger(test.class);
        SqlSession session=MybatisUtils.openSqlSession();
        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        List studentList =  studentMapper.findById(new Student(5));
        logger.info("添加成功");
        System.out.println(studentList);
        session.commit();
    }
    @Test
    public void select() throws Exception{
        Logger logger=Logger.getLogger(test.class);
        SqlSession session=MybatisUtils.openSqlSession();
        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        List studentList =  studentMapper.select();
        logger.info("添加成功");
        System.out.println(studentList);
        session.commit();
    }

接口:

public interface StudentMapper {

    public int add(Student student);
    public int delete(Student Student);
    public int update(Student student);
    public List select();
    public List findById(Student student);
}




你可能感兴趣的:(框架)