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

The following examples show how to use org.apache.ibatis.session.SqlSession#update() . 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: BatchSqlBatchUpdater.java    From sqlhelper with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public BatchResult batchUpdate(MybatisBatchStatement statement, List<E> beans) throws SQLException {
    Preconditions.checkNotNull(statement);
    Preconditions.checkArgument(statement.getBatchMode() == BatchMode.BATCH_SQL);
    Preconditions.checkNotNull(sessionFactory);

    SqlSession session = sessionFactory.openSession(true);
    BatchResult<E> result = new BatchResult<E>();
    result.setParameters(beans);
    result.setStatement(statement);
    try {
        int updated = session.update(statement.getSql(), beans);
        result.setRowsAffected(updated);
    } catch (Throwable ex) {
        result.addThrowable(ex);
    } finally {
        session.close();
    }
    return result;
}
 
Example 2
Source File: ComplexTypeTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUpdateProps() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Item item = new Item();
    item.id = 10;
    Property p1 = new Property();
    p1.id = 11;
    p1.value = "value11";
    Property p2 = new Property();
    p2.id = 12;
    p2.value = "value12";
    List<Property> list = new ArrayList<Property>();
    list.add(p1);
    list.add(p2);
    item.properties = list;
    sqlSession.update("updateProps", item);
  } finally {
    sqlSession.close();
  }
}
 
Example 3
Source File: ComplexTypeTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUpdateProps() {
  SqlSession sqlSession = sqlSessionFactory.openSession();
  try {
    Item item = new Item();
    item.id = 10;
    Property p1 = new Property();
    p1.id = 11;
    p1.value = "value11";
    Property p2 = new Property();
    p2.id = 12;
    p2.value = "value12";
    List<Property> list = new ArrayList<Property>();
    list.add(p1);
    list.add(p2);
    item.properties = list;
    sqlSession.update("updateProps", item);
  } finally {
    sqlSession.close();
  }
}
 
Example 4
Source File: IDAllocDaoImpl.java    From SpringMVC-Project with MIT License 5 votes vote down vote up
@Override
public LeafAlloc updateMaxIdAndGetLeafAlloc(String tag) {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        sqlSession.update("com.doodl6.springmvc.service.leaf.segment.dao.IDAllocMapper.updateMaxId", tag);
        LeafAlloc result = sqlSession.selectOne("com.doodl6.springmvc.service.leaf.segment.dao.IDAllocMapper.getLeafAlloc", tag);
        sqlSession.commit();
        return result;
    } finally {
        sqlSession.close();
    }
}
 
Example 5
Source File: IDAllocDaoImpl.java    From SpringMVC-Project with MIT License 5 votes vote down vote up
@Override
public LeafAlloc updateMaxIdByCustomStepAndGetLeafAlloc(LeafAlloc leafAlloc) {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        sqlSession.update("com.doodl6.springmvc.service.leaf.segment.dao.IDAllocMapper.updateMaxIdByCustomStep", leafAlloc);
        LeafAlloc result = sqlSession.selectOne("com.doodl6.springmvc.service.leaf.segment.dao.IDAllocMapper.getLeafAlloc", leafAlloc.getKey());
        sqlSession.commit();
        return result;
    } finally {
        sqlSession.close();
    }
}
 
Example 6
Source File: IDAllocDaoImpl.java    From Leaf with Apache License 2.0 5 votes vote down vote up
@Override
public LeafAlloc updateMaxIdAndGetLeafAlloc(String tag) {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        sqlSession.update("com.sankuai.inf.leaf.segment.dao.IDAllocMapper.updateMaxId", tag);
        LeafAlloc result = sqlSession.selectOne("com.sankuai.inf.leaf.segment.dao.IDAllocMapper.getLeafAlloc", tag);
        sqlSession.commit();
        return result;
    } finally {
        sqlSession.close();
    }
}
 
Example 7
Source File: IDAllocDaoImpl.java    From Leaf with Apache License 2.0 5 votes vote down vote up
@Override
public LeafAlloc updateMaxIdByCustomStepAndGetLeafAlloc(LeafAlloc leafAlloc) {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        sqlSession.update("com.sankuai.inf.leaf.segment.dao.IDAllocMapper.updateMaxIdByCustomStep", leafAlloc);
        LeafAlloc result = sqlSession.selectOne("com.sankuai.inf.leaf.segment.dao.IDAllocMapper.getLeafAlloc", leafAlloc.getKey());
        sqlSession.commit();
        return result;
    } finally {
        sqlSession.close();
    }
}
 
Example 8
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 9
Source File: SqlSessionTestBase.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
protected final void testAndVerifyUpdate() throws Exception {
    // Given
    final String updateId = "updateId";
    SqlSession sqlSession = getSqlSession();
    // When
    sqlSession.update(updateId);
    sqlSession.update(updateId, new Object());
    // Then
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    Method update1 = sqlSession.getClass().getDeclaredMethod("update", String.class);
    Method update2 = sqlSession.getClass().getDeclaredMethod("update", String.class, Object.class);
    verifier.verifyTrace(event("MYBATIS", update1, Expectations.cachedArgs(updateId)));
    verifier.verifyTrace(event("MYBATIS", update2, Expectations.cachedArgs(updateId)));
}
 
Example 10
Source File: MonitNotifyHistoryDao.java    From JavaTutorial with Apache License 2.0 5 votes vote down vote up
public int updateByPrimaryKeySelective(MonitNotifyHistory record) {
    SqlSession session = _ssFactory.openSession(true);
    int result = 0;
    try {
        session.update(assembleStatement("updateByPrimaryKeySelective"), record);
    } finally {
        close(session);
    }
    
    return result;
}
 
Example 11
Source File: ProcessEngineInitializationTest.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Test
public void testVersionMismatch() {
    // first create the schema
    ProcessEngineImpl processEngine = (ProcessEngineImpl) ProcessEngineConfiguration
            .createProcessEngineConfigurationFromResource("org/flowable/standalone/initialization/notables.flowable.cfg.xml")
            .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP).buildProcessEngine();

    // then update the version to something that is different to the library version
    DbSqlSessionFactory dbSqlSessionFactory = (DbSqlSessionFactory) processEngine.getProcessEngineConfiguration().getSessionFactories()
            .get(DbSqlSession.class);
    SqlSessionFactory sqlSessionFactory = dbSqlSessionFactory.getSqlSessionFactory();
    SqlSession sqlSession = sqlSessionFactory.openSession();
    boolean success = false;
    try {
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("name", "schema.version");
        parameters.put("value", "25.7");
        parameters.put("revision", 1);
        parameters.put("newRevision", 2);
        sqlSession.update("updateProperty", parameters);
        success = true;
    } catch (Exception e) {
        throw new FlowableException("couldn't update db schema version", e);
    } finally {
        if (success) {
            sqlSession.commit();
        } else {
            sqlSession.rollback();
        }
        sqlSession.close();
    }

    // now we can see what happens if when a process engine is being
    // build with a version mismatch between library and db tables
    Consumer<FlowableWrongDbException> flowableWrongDbExceptionRequirements = flowableWrongDbException -> {
        assertThat(flowableWrongDbException.getDbVersion()).isEqualTo("25.7");
        assertThat(flowableWrongDbException.getLibraryVersion()).isEqualTo(ProcessEngine.VERSION);
    };
    assertThatThrownBy(() -> ProcessEngineConfiguration
            .createProcessEngineConfigurationFromResource("org/flowable/standalone/initialization/notables.flowable.cfg.xml")
            .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE).buildProcessEngine())
            .isInstanceOfSatisfying(FlowableWrongDbException.class, flowableWrongDbExceptionRequirements)
            .hasMessageContaining("version mismatch");

    // closing the original process engine to drop the db tables
    processEngine.close();
}