Search in sources :

Example 51 with SqlSession

use of org.apache.ibatis.session.SqlSession in project HackTutorial by linrongbin16.

the class App method insertUser.

private void insertUser(String firstNname, String lastName, String email, Integer age) {
    SqlSession sqlSession = null;
    User u = new User(-1, firstNname, lastName, email, age);
    try {
        sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int result = mapper.insert(u);
        sqlSession.commit();
        logger.info("insert success, result:{}, User:{}", result, u);
    } catch (Exception ex) {
        logger.info("insert exception, exception:{}, User:{}", ex, u);
        if (sqlSession != null) {
            sqlSession.rollback();
        }
    } finally {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession)

Example 52 with SqlSession

use of org.apache.ibatis.session.SqlSession in project HackTutorial by linrongbin16.

the class App method findUserById.

private void findUserById(int id) {
    SqlSession sqlSession = null;
    User user = null;
    try {
        sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        user = mapper.findUserById(id);
    } catch (Exception ex) {
        logger.info("findUserById exception, id:{}, exception:{}", id, ex);
    } finally {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }
    if (user == null) {
        logger.info("findUserById fail, id {} not exist", id);
    } else {
        logger.info("findUserById success, User: {}", user);
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession)

Example 53 with SqlSession

use of org.apache.ibatis.session.SqlSession in project HackTutorial by linrongbin16.

the class App method deleteUserById.

private void deleteUserById(int id) {
    SqlSession sqlSession = null;
    int result = 0;
    try {
        sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        result = mapper.delete(id);
        sqlSession.commit();
    } catch (Exception ex) {
        logger.info("deleteUserById exception, id:{}, exception:{}", id, ex);
    } finally {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }
    if (result <= 0) {
        logger.info("deleteUserById fail, id {} not exist, result:{}", id, result);
    } else {
        logger.info("deleteUserById success, result:{}", result);
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession)

Example 54 with SqlSession

use of org.apache.ibatis.session.SqlSession in project taskana by Taskana.

the class CreateTaskAccTest method testCreateExternalTaskWithAttachment.

@WithAccessId(userName = "user_1_1", groupNames = { "group_1" })
@Test
public void testCreateExternalTaskWithAttachment() throws SQLException, NotAuthorizedException, InvalidArgumentException, ClassificationNotFoundException, WorkbasketNotFoundException, TaskAlreadyExistException, InvalidWorkbasketException, TaskNotFoundException, ConcurrencyException {
    TaskService taskService = taskanaEngine.getTaskService();
    Task newTask = taskService.newTask("USER_1_1", "DOMAIN_A");
    newTask.setClassificationKey("L12010");
    Map<String, String> customAttributesForCreate = createSimpleCustomProperties(27);
    newTask.addAttachment(createAttachment("DOCTYPE_DEFAULT", createObjectReference("COMPANY_A", "SYSTEM_B", "INSTANCE_B", "ArchiveId", "12345678901234567890123456789012345678901234567890"), "E-MAIL", "2018-01-15", customAttributesForCreate));
    newTask.setPrimaryObjRef(createObjectReference("COMPANY_A", "SYSTEM_A", "INSTANCE_A", "VNR", "1234567"));
    Task createdTask = taskService.createTask(newTask);
    assertNotNull(createdTask.getId());
    assertThat(createdTask.getCreator(), equalTo(CurrentUserContext.getUserid()));
    // verify that the database content is as expected
    TaskanaEngineProxyForTest engineProxy = new TaskanaEngineProxyForTest((TaskanaEngineImpl) taskanaEngine);
    try {
        SqlSession session = engineProxy.getSqlSession();
        AttachmentMapper mapper = session.getMapper(AttachmentMapper.class);
        engineProxy.openConnection();
        String customProperties = mapper.getCustomAttributesAsString(createdTask.getAttachments().get(0).getId());
        assertTrue(customProperties.contains("\"Property_26\":\"Property Value of Property_26\""));
        assertTrue(customProperties.contains("\"Property_25\":\"Property Value of Property_25\""));
        assertTrue(customProperties.contains("\"Property_21\":\"Property Value of Property_21\""));
        assertTrue(customProperties.contains("\"Property_19\":\"Property Value of Property_19\""));
        assertTrue(customProperties.contains("\"Property_16\":\"Property Value of Property_16\""));
        assertTrue(customProperties.contains("\"Property_12\":\"Property Value of Property_12\""));
        assertTrue(customProperties.contains("\"Property_11\":\"Property Value of Property_11\""));
        assertTrue(customProperties.contains("\"Property_7\":\"Property Value of Property_7\""));
        assertTrue(customProperties.contains("\"Property_6\":\"Property Value of Property_6\""));
    } finally {
        engineProxy.returnConnection();
    }
    Task readTask = taskService.getTask(createdTask.getId());
    assertNotNull(readTask);
    assertThat(readTask.getCreator(), equalTo(CurrentUserContext.getUserid()));
    assertNotNull(readTask.getAttachments());
    assertEquals(1, readTask.getAttachments().size());
    assertNotNull(readTask.getAttachments().get(0).getCreated());
    assertNotNull(readTask.getAttachments().get(0).getModified());
    assertEquals(readTask.getAttachments().get(0).getCreated(), readTask.getAttachments().get(0).getModified());
    assertNotNull(readTask.getAttachments().get(0).getClassificationSummary());
    assertNotNull(readTask.getAttachments().get(0).getObjectReference());
    // verify that the map is correctly retrieved from the database
    Map<String, String> customAttributesFromDb = readTask.getAttachments().get(0).getCustomAttributes();
    assertNotNull(customAttributesFromDb);
    assertTrue(customAttributesFromDb.equals(customAttributesForCreate));
}
Also used : Task(pro.taskana.Task) SqlSession(org.apache.ibatis.session.SqlSession) TaskService(pro.taskana.TaskService) TaskanaEngineProxyForTest(pro.taskana.impl.TaskanaEngineProxyForTest) AttachmentMapper(pro.taskana.mappings.AttachmentMapper) TaskanaEngineProxyForTest(pro.taskana.impl.TaskanaEngineProxyForTest) AbstractAccTest(acceptance.AbstractAccTest) Test(org.junit.Test) WithAccessId(pro.taskana.security.WithAccessId)

Example 55 with SqlSession

use of org.apache.ibatis.session.SqlSession in project camunda-bpm-platform by camunda.

the class JobMigrationScenario method triggerEntryCriterion.

@DescribesScenario("createJob")
public static ScenarioSetup triggerEntryCriterion() {
    return new ScenarioSetup() {

        public void execute(ProcessEngine engine, final String scenarioName) {
            final ProcessEngineConfigurationImpl engineConfiguration = (ProcessEngineConfigurationImpl) engine.getProcessEngineConfiguration();
            CommandExecutor commandExecutor = engineConfiguration.getCommandExecutorTxRequired();
            // create a job with the scenario name as id and a null suspension state
            commandExecutor.execute(new Command<Void>() {

                public Void execute(CommandContext commandContext) {
                    Connection connection = null;
                    Statement statement = null;
                    ResultSet rs = null;
                    try {
                        SqlSession sqlSession = commandContext.getDbSqlSession().getSqlSession();
                        connection = sqlSession.getConnection();
                        statement = connection.createStatement();
                        statement.executeUpdate("INSERT INTO ACT_RU_JOB(ID_, REV_, RETRIES_, TYPE_, EXCLUSIVE_, HANDLER_TYPE_) " + "VALUES (" + "'" + scenarioName + "'," + "1," + "3," + "'timer'," + DbSqlSessionFactory.databaseSpecificTrueConstant.get(engineConfiguration.getDatabaseType()) + "," + "'" + TimerStartEventJobHandler.TYPE + "'" + ")");
                        connection.commit();
                        statement.close();
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    } finally {
                        try {
                            if (statement != null) {
                                statement.close();
                            }
                            if (rs != null) {
                                rs.close();
                            }
                            if (connection != null) {
                                connection.close();
                            }
                        } catch (SQLException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    return null;
                }
            });
        }
    };
}
Also used : CommandContext(org.camunda.bpm.engine.impl.interceptor.CommandContext) SqlSession(org.apache.ibatis.session.SqlSession) SQLException(java.sql.SQLException) Statement(java.sql.Statement) CommandExecutor(org.camunda.bpm.engine.impl.interceptor.CommandExecutor) Connection(java.sql.Connection) ScenarioSetup(org.camunda.bpm.qa.upgrade.ScenarioSetup) ResultSet(java.sql.ResultSet) ProcessEngineConfigurationImpl(org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl) ProcessEngine(org.camunda.bpm.engine.ProcessEngine) DescribesScenario(org.camunda.bpm.qa.upgrade.DescribesScenario)

Aggregations

SqlSession (org.apache.ibatis.session.SqlSession)2117 Test (org.junit.jupiter.api.Test)1845 BaseDataTest (org.apache.ibatis.BaseDataTest)593 Test (org.junit.Test)127 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)104 Reader (java.io.Reader)97 User (com.github.pagehelper.model.User)87 ArrayList (java.util.ArrayList)84 UserMapper (com.github.pagehelper.mapper.UserMapper)75 Connection (java.sql.Connection)68 FirstName (mbg.test.common.FirstName)60 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)60 HashMap (java.util.HashMap)59 BeforeClass (org.junit.BeforeClass)56 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)54 RowBounds (org.apache.ibatis.session.RowBounds)36 Map (java.util.Map)34 Date (java.util.Date)33 List (java.util.List)24 AwfulTableMapper (mbg.test.mb3.generated.dsql.mapper.AwfulTableMapper)23