Java Code Examples for org.apache.ibatis.session.SqlSession#insert()

The following examples show how to use org.apache.ibatis.session.SqlSession#insert() . 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: 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 2
Source File: BatchKeysTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsertJdbc3() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    User user1 = new User(null, "Pocoyo");
    sqlSession.insert("insertIdentity", user1);
    User user2 = new User(null, "Valentina");
    sqlSession.insert("insertIdentity", user2);
    sqlSession.flushStatements();
    assertEquals(Integer.valueOf(0), user1.getId());
    assertEquals(Integer.valueOf(1), user2.getId());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }

  sqlSession = sqlSessionFactory.openSession();
  List<User> users = sqlSession.selectList("selectIdentity");
  Assert.assertTrue(users.size() == 2);
}
 
Example 3
Source File: ComponentTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldInsertNestedPasswordFieldOfComplexType() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    //Create User
    User user = new User();
    user.setId(500000L);
    user.setPassword(new EncryptedString("secret"));
    user.setUsername("johnny" + Calendar.getInstance().getTimeInMillis());//random
    user.setAdministrator(true);

    sqlSession.insert("User.insert", user);

    //Retrieve User
    user = (User) sqlSession.selectOne("User.find", user.getId());

    assertNotNull(user.getId());

    sqlSession.rollback();
  } finally {
    sqlSession.close();
  }
}
 
Example 4
Source File: BatchKeysTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsertJdbc3() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    User user1 = new User(null, "Pocoyo");
    sqlSession.insert("insertIdentity", user1);
    User user2 = new User(null, "Valentina");
    sqlSession.insert("insertIdentity", user2);
    sqlSession.flushStatements();
    assertEquals(Integer.valueOf(0), user1.getId());
    assertEquals(Integer.valueOf(1), user2.getId());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }

  sqlSession = sqlSessionFactory.openSession();
  List<User> users = sqlSession.selectList("selectIdentity");
  Assert.assertTrue(users.size() == 2);
}
 
Example 5
Source File: BatchKeysTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsert() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    User user1 = new User(null, "Pocoyo");
    sqlSession.insert("insert", user1);
    User user2 = new User(null, "Valentina");
    sqlSession.insert("insert", user2);
    sqlSession.flushStatements();
    assertEquals(new Integer(50), user1.getId());
    assertEquals(new Integer(50), user2.getId());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }

  sqlSession = sqlSessionFactory.openSession();
  List<User> users = sqlSession.selectList("select");
  Assert.assertTrue(users.size() == 2);
}
 
Example 6
Source File: NoParamTypeTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAcceptDifferentTypeInTheSameBatch() {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    ObjA a = new ObjA();
    a.setId(1);
    a.setName(111);
    sqlSession.insert("insertUser", a);
    ObjB b = new ObjB();
    b.setId(2);
    b.setName("222");
    sqlSession.insert("insertUser", b);
    List<BatchResult> batchResults = sqlSession.flushStatements();
    batchResults.clear();
    sqlSession.clearCache();
    sqlSession.commit();
    List<User> users = sqlSession.selectList("selectUser");
    assertEquals(2, users.size());
  } finally {
    sqlSession.close();
  }
}
 
Example 7
Source File: BatchKeysTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsert() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
  try {
    User user1 = new User(null, "Pocoyo");
    sqlSession.insert("insert", user1);
    User user2 = new User(null, "Valentina");
    sqlSession.insert("insert", user2);
    sqlSession.flushStatements();
    assertEquals(new Integer(50), user1.getId());
    assertEquals(new Integer(50), user2.getId());
    sqlSession.commit();
  } finally {
    sqlSession.close();
  }

  sqlSession = sqlSessionFactory.openSession();
  List<User> users = sqlSession.selectList("select");
  Assert.assertTrue(users.size() == 2);
}
 
Example 8
Source File: ComponentTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldInsertNestedPasswordFieldOfComplexType() throws Exception {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    //Create User
    User user = new User();
    user.setId(500000L);
    user.setPassword(new EncryptedString("secret"));
    user.setUsername("johnny" + Calendar.getInstance().getTimeInMillis());//random
    user.setAdministrator(true);

    sqlSession.insert("User.insert", user);

    //Retrieve User
    user = (User) sqlSession.selectOne("User.find", user.getId());

    assertNotNull(user.getId());

    sqlSession.rollback();
  } finally {
    sqlSession.close();
  }
}
 
Example 9
Source File: SelectKeyTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test(expected=PersistenceException.class)
public void testSeleckKeyReturnsNoData() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Map<String, String> parms = new HashMap<String, String>();
    parms.put("name", "Fred");
    int rows = sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table2.insertNoValuesInSelectKey", parms);
    assertEquals(1, rows);
    assertNull(parms.get("id"));
  } finally {
    sqlSession.close();
  }
}
 
Example 10
Source File: SelectKeyTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test(expected=PersistenceException.class)
public void testSeleckKeyReturnsTooManyData() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Map<String, String> parms = new HashMap<String, String>();
    parms.put("name", "Fred");
    sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table2.insertTooManyValuesInSelectKey", parms);
    sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table2.insertTooManyValuesInSelectKey", parms);
  } finally {
    sqlSession.close();
  }
}
 
Example 11
Source File: SelectKeyTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test(expected = PersistenceException.class)
public void testSeleckKeyWithWrongKeyProperty() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Name name = new Name();
    name.setName("Kyoto");
    sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table2.insertWrongKeyProperty", name);
  } finally {
    sqlSession.close();
  }
}
 
Example 12
Source File: SelectKeyTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void testInsertTable1() {
  SqlSession sqlSession = sqlSessionFactory.openSession();

  try {
    Map<String, String> parms = new HashMap<String, String>();
    parms.put("name", "Fred");
    int rows = sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table1.insert", parms);
    assertEquals(1, rows);
    assertEquals(11, parms.get("id"));

  } finally {
    sqlSession.close();
  }
}
 
Example 13
Source File: TestBatisXML.java    From java-course-ee with MIT License 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    String resource = "Configuration.xml";
    Reader reader = Resources.getResourceAsReader(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);

    SqlSession session = sqlSessionFactory.openSession();
    try {
        Region region = new Region();
        region.setRegionName("12345");

        session.insert("ibatis.RegionMapper.insertRegion", region);
        session.commit();

        System.out.println("Region ID: " + region.getRegionId());

        findAndGet(session);

        region.setRegionName("54321");
        session.update("ibatis.RegionMapper.updateRegion", region);
        session.commit();
        findAndGet(session);

        session.delete("ibatis.RegionMapper.deleteRegion", region.getRegionId());
        session.commit();
        findAndGet(session);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.close();
    }

}
 
Example 14
Source File: SqlSessionTestBase.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
protected final void testAndVerifyInsert() throws Exception {
    // Given
    final String insertId = "insertId";
    SqlSession sqlSession = getSqlSession();
    // When
    sqlSession.insert(insertId);
    sqlSession.insert(insertId, new Object());
    // Then
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    Method insert1 = sqlSession.getClass().getDeclaredMethod("insert", String.class);
    Method insert2 = sqlSession.getClass().getDeclaredMethod("insert", String.class, Object.class);
    verifier.verifyTrace(event("MYBATIS", insert1, Expectations.cachedArgs(insertId)));
    verifier.verifyTrace(event("MYBATIS", insert2, Expectations.cachedArgs(insertId)));
}
 
Example 15
Source File: SelectKeyTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test(expected = PersistenceException.class)
public void testSeleckKeyWithWrongKeyProperty() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Name name = new Name();
    name.setName("Kyoto");
    sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table2.insertWrongKeyProperty", name);
  } finally {
    sqlSession.close();
  }
}
 
Example 16
Source File: SelectKeyTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void testInsertTable1() {
  SqlSession sqlSession = sqlSessionFactory.openSession();

  try {
    Map<String, String> parms = new HashMap<String, String>();
    parms.put("name", "Fred");
    int rows = sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table1.insert", parms);
    assertEquals(1, rows);
    assertEquals(11, parms.get("id"));

  } finally {
    sqlSession.close();
  }
}
 
Example 17
Source File: UserMain.java    From blog with BSD 2-Clause "Simplified" License 5 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 session1 = sqlSessionFactory.openSession();
	User user = new User();
	user.setId(102);
	user.setSex(Sex.FEMALE);
	user.setName("zhaohui");
	try {
		session1.insert("insertUser", user);
		session1.commit();
	} finally {
		session1.close();
	}

	SqlSession session2 = sqlSessionFactory.openSession();
	try {
		UserMapper userMapper = session2.getMapper(UserMapper.class);
		User user2 = userMapper.getUser(102);
		System.out.println(user2.toString());
	} finally {
		session2.close();
	}

}
 
Example 18
Source File: SelectKeyTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test(expected=PersistenceException.class)
public void testSeleckKeyReturnsNoData() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Map<String, String> parms = new HashMap<String, String>();
    parms.put("name", "Fred");
    int rows = sqlSession.insert("org.apache.ibatis.submitted.selectkey.Table2.insertNoValuesInSelectKey", parms);
    assertEquals(1, rows);
    assertNull(parms.get("id"));
  } finally {
    sqlSession.close();
  }
}
 
Example 19
Source File: MonitNotifyHistoryDao.java    From JavaTutorial with Apache License 2.0 5 votes vote down vote up
public int insert(MonitNotifyHistory record) {
    SqlSession session = _ssFactory.openSession(true);
    int result = 0;
    try {
        result = session.insert(assembleStatement("insertSelective"), record);
    } finally {
        close(session);
    }
    
    return result;
}
 
Example 20
Source File: MybatisTest.java    From Oceanus with Apache License 2.0 4 votes vote down vote up
@Test
public void insertTest() throws SQLException {
	for(long id=1; id<=100; id++){
		SqlSession session = sqlSessionFactory.openSession(false);
		int result = -1;
		try {
			result = session.insert("com.bj58.oceanus.plugins.mybatis.entity.UserMapper.insertUser", new User(id, "uname"+id, RandomUtil.nextInt(10, 30)));
			System.out.println(result);
			id++;
			
			result = session.insert("com.bj58.oceanus.plugins.mybatis.entity.UserMapper.insertUser", new User(id, "uname"+id, RandomUtil.nextInt(10, 30)));
			System.out.println(result);
			id++;
			
			result = session.insert("com.bj58.oceanus.plugins.mybatis.entity.UserMapper.insertUser", new User(id, "uname"+id, RandomUtil.nextInt(10, 30)));
			System.out.println(result);
			id++;
			
			result = session.insert("com.bj58.oceanus.plugins.mybatis.entity.UserMapper.insertUser", new User(id, "uname"+id, RandomUtil.nextInt(10, 30)));
			System.out.println(result);
			id++;
			
			result = session.insert("com.bj58.oceanus.plugins.mybatis.entity.UserMapper.insertUser", new User(id, "uname"+id, RandomUtil.nextInt(10, 30)));
			System.out.println(result);
			id++;
			
			result = session.insert("com.bj58.oceanus.plugins.mybatis.entity.UserMapper.insertUser", new User(id, "uname"+id, RandomUtil.nextInt(10, 30)));
			System.out.println(result);
			id++;
			
			result = session.insert("com.bj58.oceanus.plugins.mybatis.entity.UserMapper.insertUser", new User(id, "uname"+id, RandomUtil.nextInt(10, 30)));
			System.out.println(result);
			id++;
			
			result = session.insert("com.bj58.oceanus.plugins.mybatis.entity.UserMapper.insertUser", new User(id, "uname"+id, RandomUtil.nextInt(10, 30)));
			System.out.println(result);
			id++;
			
			result = session.insert("com.bj58.oceanus.plugins.mybatis.entity.UserMapper.insertUser", new User(id, "uname"+id, RandomUtil.nextInt(10, 30)));
			System.out.println(result);
			id++;
			
			result = session.insert("com.bj58.oceanus.plugins.mybatis.entity.UserMapper.insertUser", new User(id, "uname"+id, RandomUtil.nextInt(10, 30)));
			System.out.println(result);
			
			session.commit();
		} catch(Exception e){
			e.printStackTrace();
			session.rollback();
		}finally {
			session.close();
		}
	}
}