案例入门

  /**
     * 入门案例
     */
    public static void main(String[] args) throws IOException {
        //1、读取配置文件
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        //2、创建sqlSessionFactory工厂
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        //3、使用工厂生产sqlSession对象
        SqlSession sqlSession = factory.openSession();
        //4、使用SqlSession创建Dao接口的代理对象
        DemoMapper mapper = sqlSession.getMapper(DemoMapper.class);
        //5、使用代理对象执行方法
        List<Demo> demoList = mapper.getDemoList();
        for (Demo demo:demoList){
            System.out.println(demo.getId()+" "+demo.getName());
        }
        //6、释放资源
        sqlSession.close();
        in.close();

    }

 

posted @ 2019-10-22 11:05  AxeBurner  阅读(132)  评论(0编辑  收藏  举报