Java Code Examples for org.apache.ibatis.io.Resources#getResourceAsStream()

The following examples show how to use org.apache.ibatis.io.Resources#getResourceAsStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlogLimitPluginMain.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void main(String[] args) throws IOException {
	String resource = "mybatis-config.xml";
	InputStream inputStream = Resources.getResourceAsStream(resource);
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

	SqlSession session2 = sqlSessionFactory.openSession();
	try {
		Blog blog = new Blog();
		blog.setTitle("new_java");
		blog.setId(101);
		BlogMapper mapper = session2.getMapper(BlogMapper.class);
		mapper.updateBlog(blog);

		session2.commit();
	} finally {
		session2.close();
	}

}
 
Example 2
Source File: MybatisTest.java    From springdream with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    // 获取SqlSessionFactory
    String resource = "MyBatis-Configuration.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    // 获取SqlSession
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.selectUser(1);
        System.out.println("\nprint user info --> " + user.toString());
    } finally {
        sqlSession.close();
    }
}
 
Example 3
Source File: MybatisUtil.java    From code with Apache License 2.0 6 votes vote down vote up
public static void template(Callback callback) {
    SqlSession session = null;
    //1.读取配置文件
    try (InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");) {
        //2.创建构建者对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //3.创建 SqlSession 工厂对象
        factory = builder.build(in);
        //4.创建 SqlSession 对象
        session = factory.openSession();
        //5.执行操作
        callback.execute(session);
        //6.提交或回滚事务
        session.commit();
    } catch (IOException e) {
        if (session != null) {
            session.rollback();
        }
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
        }
    }
}
 
Example 4
Source File: MybatisUtil.java    From code with Apache License 2.0 6 votes vote down vote up
public static void template(Callback callback) {
    SqlSession session = null;
    //1.读取配置文件
    try (InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");) {
        //2.创建构建者对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //3.创建 SqlSession 工厂对象
        factory = builder.build(in);
        //4.创建 SqlSession 对象
        session = factory.openSession();
        //5.执行操作
        callback.execute(session);
        //6.提交或回滚事务
        session.commit();
    } catch (IOException e) {
        if (session != null) {
            session.rollback();
        }
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
        }
    }
}
 
Example 5
Source File: BlogMain.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void main(String[] args) throws IOException {
		String resource = "mybatis-config-sourceCode.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

		SqlSession session = sqlSessionFactory.openSession();
		try {
			 select(session);
//			selectHandler(session, sqlSessionFactory.getConfiguration());
//			selectMyHandler(session);
			// insert(session);
			// update(session);
		} finally {
			session.close();
		}
	}
 
Example 6
Source File: FlowerTest.java    From Project with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
      // 加载 xml 文件流
      InputStream is = Resources.getResourceAsStream("MyBatis.xml");
      //使用工厂设计模式
      SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
      //生产SqlSession
      SqlSession session=factory.openSession();

      List<Flower> list = session.selectList("a.b.selAll");
      for (Flower flower : list) {
          System.out.println(flower.toString());
      }

      int count = session.selectOne("a.b.selById");
System.out.println(count);

      //把数据库中哪个列的值当作map的key
      Map<Object, Object> map = session.selectMap("a.b.selectMap", "name");
      System.out.println(map);

      session.close();
  }
 
Example 7
Source File: BlogPluginMain.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void main(String[] args) throws IOException {
	String resource = "mybatis-config.xml";
	InputStream inputStream = Resources.getResourceAsStream(resource);
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

	SqlSession session2 = sqlSessionFactory.openSession();
	try {
		BlogMapper mapper = session2.getMapper(BlogMapper.class);
		List<Blog> blogs = mapper.selectBlogs("ksfzhaohui");
		//最大只会查出50条  select * from (select * from blog where author = ?) limit_table_name_blog limit 50 
		System.out.println(blogs.size());
	} finally {
		session2.close();
	}

}
 
Example 8
Source File: BlogRowBoundsMain.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void main(String[] args) throws IOException {
	String resource = "mybatis-config-sourceCode.xml";
	InputStream inputStream = Resources.getResourceAsStream(resource);
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

	SqlSession session = sqlSessionFactory.openSession();
	try {
		BlogMapper<Blog> mapper = session.getMapper(BlogMapper.class);
		int pageSize = 50;
		int totalCount = 55;
		int loopCount = (totalCount % pageSize == 0) ? totalCount / pageSize : totalCount / pageSize + 1;
		for (int i = 1; i <= loopCount; i++) {
			List<Blog> blogs = mapper.selectBlogs("ksfzhaohui", new RowBounds(i, pageSize));
			for (Blog b : blogs) {
				System.out.println(b);
			}
			System.out.println("============================");
		}
	} finally {
		session.close();
	}
}
 
Example 9
Source File: CeshiMyBatis.java    From cg-blog with MIT License 6 votes vote down vote up
@Test
public void testInsert(){
    String resource = "sqlMapConfig.xml";           //定位核心配置文件
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);        // 创建 SqlSessionFactory

    SqlSession sqlSession = sqlSessionFactory.openSession();            //获取到 SqlSession

    Person p = new Person();
    p.setId(5);
    p.setName("gavin");
    p.setAge(12);

    sqlSession.insert("yeepay.payplus.mapper.UserMapper.insert", p);
    sqlSession.commit();            //默认是不自动提交,必须手工提交
}
 
Example 10
Source File: CeshiMyBatis.java    From cg-blog with MIT License 5 votes vote down vote up
@Test
public void testUpdate() {   // 修改方法
    String resource = "sqlMapConfig.xml";            //定位核心配置文件
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);        // 创建 SqlSessionFactory

    SqlSession sqlSession = sqlSessionFactory.openSession();            // 获取到 SqlSession

    Person p = new Person();
    p.setId(1);
    p.setAge(12);

    sqlSession.insert("yeepay.payplus.mapper.UserMapper.update", p);
    sqlSession.commit();            //默认是不自动提交,必须手工提交
}
 
Example 11
Source File: OneToOneTest.java    From mybatis-test with Apache License 2.0 5 votes vote down vote up
@Before
public void prepare() throws IOException {
    String resource = "chapter4/mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    inputStream.close();
}
 
Example 12
Source File: TransactionalCacheTest.java    From mybatis-test with Apache License 2.0 5 votes vote down vote up
@Before
public void prepare() throws IOException {
    String resource = "chapter6/mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    inputStream.close();
}
 
Example 13
Source File: PeopleServiceImpl.java    From Project with Apache License 2.0 5 votes vote down vote up
public List<People> selectAll() throws IOException {
    // MyBatis 默认不加载配置文件,因此需要先加载配置文件,返回整个配置文件的流对象
    // 在数据访问层处理异常和在控制器中处理异常,一般在 service 中只抛出异常;
    InputStream inputStream = Resources.getResourceAsStream("MyBatis.xml");
    // 前面是工厂       实例化工厂对象时使用的是构建者设计模式   它的名称标志:后面有Builder
    // 构建者设计模式意义: 简化对象实例化过程
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
    // 整个 sqlsession 就是 MyBatis 中 API 封装的对象,增删改查都在里面
    SqlSession session = factory.openSession();
    List<People> list = session.selectList("com.gjxaiou.mapper.PeopleMapper.selAll");
    session.close();
    return list;
}
 
Example 14
Source File: XmlMapperBuilderTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSuccessfullyLoadXMLMapperFile() throws Exception {
  Configuration configuration = new Configuration();
  String resource = "org/apache/ibatis/builder/AuthorMapper.xml";
  InputStream inputStream = Resources.getResourceAsStream(resource);
  XMLMapperBuilder builder = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
  builder.parse();
}
 
Example 15
Source File: MybatisCacheTest.java    From code with Apache License 2.0 5 votes vote down vote up
@Before//在测试方法执行之前执行
public void init() throws Exception {
    //1.读取配置文件
    in = Resources.getResourceAsStream("SqlMapConfig.xml");
    //2.创建构建者对象
    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    //3.创建 SqlSession 工厂对象
    factory = builder.build(in);
    //4.创建 SqlSession 对象
    session = factory.openSession();
    //5.创建 Dao 的代理对象
    userMapper = session.getMapper(UserMapper.class);
}
 
Example 16
Source File: XmlConfigBuilderTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSuccessfullyLoadMinimalXMLConfigFile() throws Exception {
  String resource = "org/apache/ibatis/builder/MinimalMapperConfig.xml";
  InputStream inputStream = Resources.getResourceAsStream(resource);
  XMLConfigBuilder builder = new XMLConfigBuilder(inputStream);
  Configuration config = builder.parse();
  assertNotNull(config);
}
 
Example 17
Source File: MybatisDynamicSqlTest.java    From code with Apache License 2.0 5 votes vote down vote up
@Before//在测试方法执行之前执行
public void init() throws Exception {
    //1.读取配置文件
    in = Resources.getResourceAsStream("SqlMapConfig.xml");
    //2.创建构建者对象
    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    //3.创建 SqlSession 工厂对象
    SqlSessionFactory factory = builder.build(in);
    //4.创建 SqlSession 对象
    session = factory.openSession();
    //5.创建 Dao 的代理对象
    userMapper = session.getMapper(UserMapper.class);
}
 
Example 18
Source File: CeshiMyBatis.java    From cg-blog with MIT License 5 votes vote down vote up
@Test
public void testDeleteArray() {   // 批量删除
    String resource = "sqlMapConfig.xml";            //定位核心配置文件
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);        // 创建 SqlSessionFactory

    SqlSession sqlSession = sqlSessionFactory.openSession();            // 获取到 SqlSession

    sqlSession.delete("yeepay.payplus.mapper.UserMapper.deleteArray", new Integer[]{2, 3, 4});
    sqlSession.commit();            //默认是不自动提交,必须手工提交
}
 
Example 19
Source File: XmlConfigBuilderTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSuccessfullyLoadMinimalXMLConfigFile() throws Exception {
  String resource = "org/apache/ibatis/builder/MinimalMapperConfig.xml";
  InputStream inputStream = Resources.getResourceAsStream(resource);
  XMLConfigBuilder builder = new XMLConfigBuilder(inputStream);
  Configuration config = builder.parse();
  assertNotNull(config);
}
 
Example 20
Source File: Test.java    From Project with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {

        // 首先加载配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatis.xml");

        // 使用工厂设计模式,创建工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        // 生产 sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

//        Flower flower = new Flower();
//        flower.setId(2);
//
//        // 接收根据 Id 返回的对象
//        List<Flower> flowerList = sqlSession.selectList("MyFlower.selectAll");
//        Iterator<Flower> iterator = flowerList.iterator();
//        while (iterator.hasNext()){
//            System.out.println(((Flower)iterator.next()).toString());
//        }
//
//         // 获取同样 Id 数据数目
//        int countFlower = sqlSession.selectOne("MyFlower.countSelectById", flower);
//        System.out.println(countFlower);
//
//        //
//        Map<Object, Object> newNameMap = sqlSession.selectMap("MyFlower.selectByColumn", "newName");
//        System.out.println(newNameMap);
//
//
//
//        int insert = sqlSession.insert("MyFlower.insert", flower);
//        if (insert != 0){
//            System.out.println("插入成功");
//        }
//
//
//        flower.setId(3);
//        flower.setName("白玉兰");
//        int update = sqlSession.update("MyFlower.update", flower);
//        sqlSession.commit();


        PeopleMapper peopleMapper = sqlSession.getMapper(PeopleMapper.class);
        List<People> peopleList = peopleMapper.selectAll();
        Iterator<People> peopleIterator = peopleList.iterator();
        if (peopleIterator.hasNext()){
            System.out.println((People) peopleIterator.next());
        }
        System.out.println("------");


        // 多参数
        PeopleMapper mapper = sqlSession.getMapper(PeopleMapper.class);
        List<People> peopleList1 = mapper.selectByGenderAndScore("男", 99);
        Iterator<People> iterator2 = peopleList1.iterator();
        if (iterator2.hasNext()) {
            System.out.println(iterator2.next());
        }
        System.out.println("----------");


        // 动态 SQL
        List<People> peopleList2 = mapper.DynamicSelect("男", 76);
        Iterator<People> iterator3 = peopleList2.iterator();
        if (iterator3.hasNext()){
            System.out.println((People)iterator3.next());
        }
        System.out.println("------------");

        for (People people : peopleList2) {
            System.out.println(people);
        }


        sqlSession.close();

    }