Example usage for org.apache.ibatis.session SqlSession update

List of usage examples for org.apache.ibatis.session SqlSession update

Introduction

In this page you can find the example usage for org.apache.ibatis.session SqlSession update.

Prototype

int update(String statement, Object parameter);

Source Link

Document

Execute an update statement.

Usage

From source file:in.flipbrain.dao.MyBatisDao.java

License:Apache License

public void saveAssessment(AssessmentDto dto) {
    SqlSession session = sqlSessionFactory.openSession();
    try {/*from www  . ja  v  a 2  s.  c  om*/
        if (dto.assessId < 1 && !dto.isDeleted()) {
            session.insert("insertAssessment", dto);
        } else if (dto.isDeleted()) {
            session.delete("deleteAssessment", dto);
        } else {
            session.update("updateAssessment", dto);
        }

        for (AssessmentQuestionDto q : dto.questions) {
            q.assessId = dto.assessId;
            if (q.assessQuestId < 1 && !q.isDeleted()) {
                session.insert("insertAssessmentQuestion", q);
            } else if (q.isDeleted()) {
                session.delete("deleteAssessmentQuestion", q);
            } else {
                session.update("updateAssessmentQuestion", q);
            }
        }
        session.commit();
    } finally {
        session.close();
    }
}

From source file:in.flipbrain.dao.MyBatisDao.java

License:Apache License

protected <T> int saveEntity(String stmtId, T dto, Operation op) {
    int rows = 0;
    SqlSession session = sqlSessionFactory.openSession();
    try {//w w  w  .  ja  v a  2s .  com
        logActivity(session);
        switch (op) {
        case Del:
            rows = session.delete(stmtId, dto);
            break;
        case Ins:
            rows = session.insert(stmtId, dto);
            break;
        case Upd:
            rows = session.update(stmtId, dto);
            break;
        }
        session.commit();
    } finally {
        session.close();
    }
    return rows;
}

From source file:io.starter.datamodel.ContentData.java

License:Open Source License

/**
 * upload a new content item in the system
 * //from  w w  w  .j  av a2s  .  c  o  m
 * @param author
 * @param device
 * @param copyright
 * @param description
 * @param url
 * @param servletRequest
 * @param servletResponse
 * @throws Exception
 */
@PUT
@Path("upload")
@Produces(MediaType.APPLICATION_JSON)
@Consumes({ MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_OCTET_STREAM })
public String upload(@FormDataParam("binary_content") FormDataContentDisposition fileDetail,
        @FormDataParam("binary_content") InputStream uploadedInputStream,
        @FormDataParam("userImage") int userImage, @Context HttpServletRequest servletRequest,
        @Context HttpServletResponse servletResponse) throws Exception {

    User u = (User) servletRequest.getSession(true).getAttribute(SESSION_VAR_USER);
    /*
     * TODO: remove mediadir? String mediadir =
     * servletRequest.getSession().getServletContext()
     * .getInitParameter("media.dir");
     */
    String uploadedFileLocation = fileDetail.getFileName();

    // save it
    File[] uploadedFileset = saveToFile(uploadedInputStream, uploadedFileLocation, u.getId());

    Logger.log(
            "UserData.upload() File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation);

    if (userImage > 0) {
        String f = fileDetail.getFileName();
        u.setAvatarImage(f);

        SqlSession session = (SqlSession) servletRequest.getAttribute(SESSION_VAR_SQLSESSION);
        int rowsUpdated = session.update("io.starter.dao.UserMapper.updateByPrimaryKey", u);

        u = UserData.getUserObjectByID(u.getId(), servletRequest, servletResponse);
        u.setAvatarImage(f);

        session.commit();
        if (rowsUpdated < 1)
            throw new ServletException("UserData.upload() could not setAvatarImage()");
    }
    // return Response.status(200).entity(output).build();

    // return the JSON result
    JSONObject j = new JSONObject();

    String fnamey = uploadedFileLocation.substring(0, uploadedFileLocation.lastIndexOf("."));
    String fnamex = uploadedFileLocation.substring(uploadedFileLocation.lastIndexOf("."));

    String fna = "/" + fnamey + "/Standard" + fnamex;

    fna = fnamey + fnamex;
    j.put("filename", fna);

    j.put("mimeType", servletRequest.getSession().getServletContext().getMimeType(fna));

    return j.toString();
}

From source file:io.starter.datamodel.ContentData.java

License:Open Source License

/**
 * /*from   w  ww. j ava  2s .c o  m*/
 * @param servletRequest
 * @param servletResponse
 * @return
 * @throws JSONException
 * @throws IOException
 * @throws ServletException
 */
@DELETE
@Path("delete")
public void delete(@PathParam("contentId") int id, @Context HttpServletRequest servletRequest,
        @Context HttpServletResponse servletResponse) throws IOException, ServletException {

    // TODO: checkaccess
    // Content cxdel = getContentObject(servletRequest, servletResponse);
    // String contentToDelete = cxdel.getUrl();

    // TODO: S3FS is our friend
    // S3FS s3fs = new S3FS();
    // s3fs.deleteFile(S3_STARTER_MEDIA_BUCKET,);

    SqlSession session = (SqlSession) servletRequest.getAttribute(SESSION_VAR_SQLSESSION);

    try {

        // delete content acls
        AclExample a = new AclExample();
        Criteria c = a.createCriteria();
        c.andTargetIdEqualTo(id);
        c.andTargetTypeEqualTo(SystemConstants.SECURITY_TARGET_TYPE_CONTENT);

        session.delete("io.starter.dao.AclMapper.deleteByExample", a);
        session.commit();

        // delete content record
        session.delete("io.starter.dao.ContentMapper.deleteByPrimaryKey", id);
        session.commit();

    } catch (Exception e) {
        // if there are cascading FK constraints then we need to mark this
        // content item deleted (not actually delete it)
        // this way we preserve the integrity of the system
        // TODO: discuss handling deletion of content in re: orphan data

        Content cxdel = getContentObject(servletRequest, id);

        session = (SqlSession) servletRequest.getAttribute(SESSION_VAR_SQLSESSION);

        try {
            cxdel.setFlag(FLAG_DELETED);
            session.update("io.starter.dao.ContentMapper.updateByPrimaryKey", cxdel);
            session.commit();
        } catch (Exception x) {
            Logger.error("Total failure in ContentData.delete trying to mark content item as deleted: "
                    + x.toString());
            // hmmm total failure
        }

    }

    super.deleteds.add(id);
    // update caches
    super.removeContentFromCaches(id, servletRequest);

}

From source file:io.starter.TestContent.java

License:Open Source License

/**
 * /* www  .j  av a2 s  .c  o m*/
 * 
 * OUCH updating content objects totally broken.
 * 
 * @throws Exception
 */
@Test
@Ignore
public void setImageNamesForStarterItems() throws Exception {

    sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
    final SqlSession session = sqlSessionFactory.openSession(true);

    ContentExample example = new ContentExample();
    io.starter.model.ContentExample.Criteria criteria = example.createCriteria();

    example.setOrderByClause("post_date DESC");
    example.setDistinct(true);

    List<Content> results = session.selectList("io.starter.dao.ContentMapper.selectByExample", example);

    // update
    for (Content c : results) {
        String stz = c.getUrl();

        if (stz.indexOf(SystemConstants.S3_STARTER_SERVICE) > -1) {

            stz = stz.substring(stz.lastIndexOf("/") + 1);
            Logger.logInfo("Found Starter File: " + stz);
            c.setUrl(stz);
            try {

                ContentExample example1 = new ContentExample();
                io.starter.model.ContentExample.Criteria criteria1 = example1.createCriteria();

                criteria1.andIdEqualTo(c.getId());
                example1.setDistinct(true);

                int rowsUpdated = session.update("io.starter.dao.ContentMapper.Update_By_Example_Where_Clause",
                        example1);

            } catch (Exception e) {
                fail(e.toString());
            }
        }

    }

    results = session.selectList("io.starter.dao.ContentMapper.selectObjByExample", example);

    // update
    for (Content c : results) {
        String stz = c.getUrl();

        if (stz.indexOf(SystemConstants.S3_STARTER_SERVICE) > -1) {

            fail("Still Legacy Filenames Exist");
        }

    }

}

From source file:mybatis.client.MyJFrame.java

public void updateData(String s_id, String s_pwd, String s_name, String s_email, String s_phone) {

    Map<String, String> map = new HashMap<String, String>();

    map.put("id", s_id);
    //map.put("pwd", s_pwd);
    map.put("name", s_name);
    map.put("email", s_email);
    map.put("phone", s_phone);

    SqlSession ss = factory.openSession(true);
    int cnt = ss.update("mem.update", map);

    if (cnt == 1) {
        JOptionPane.showMessageDialog(this, "?? ?.");
        ss.commit();//from   ww  w  .  ja va  2s .c  o m
    } else {
        JOptionPane.showMessageDialog(this, " ");
        ss.rollback();
    }
    ss.close();
}

From source file:net.hasor.db.orm.mybatis3.SqlExecutorTemplate.java

License:Apache License

public int update(final String statement, final Object parameter) throws SQLException {
    return this.execute(new SqlSessionCallback<Integer>() {
        public Integer doSqlSession(SqlSession sqlSession) {
            return sqlSession.update(statement, parameter);
        }//from w  w  w  . j av  a2s. co m
    });
}

From source file:net.nexxus.db.DBManagerImpl.java

License:Open Source License

public void updateHeader(NntpArticleHeader header) {
    try {/*www .java2s .c  o  m*/
        SqlSession session = sqlFactory.openSession();
        HashMap map = DBUtils.mapHeaderForUpdate(header);
        session.update("updateStatus", map);
        session.commit();
        session.close();
    } catch (Exception e) {
        log.error("could not update header in database: " + e.getMessage());
    }

}

From source file:net.nexxus.db.DBManagerImpl.java

License:Open Source License

/**
 * update an NntpGroup in our groups list
 *//*w  w  w  .ja v a  2  s . c o m*/
public void updateGroup(NntpGroup group) throws Exception {
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("table", groupsTable);
    map.put("name", group.getName());
    map.put("auto_update", group.isAutoUpdate());
    map.put("hi", group.getHighID());
    map.put("low", group.getLowID());

    SqlSession session = sqlFactory.openSession();
    session.update("updateGroup", map);

    session.commit();
    session.close();
}

From source file:org.activiti.standalone.initialization.ProcessEngineInitializationTest.java

License:Apache License

public void testVersionMismatch() {
    // first create the schema
    ProcessEngineImpl processEngine = (ProcessEngineImpl) ProcessEngineConfiguration
            .createProcessEngineConfigurationFromResource(
                    "org/activiti/standalone/initialization/notables.activiti.cfg.xml")
            .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP)
            .buildProcessEngine();/*from  w w  w . j a  v a  2  s  . c o  m*/

    // 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<String, Object>();
        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 ActivitiException("couldn't update db schema version", e);
    } finally {
        if (success) {
            sqlSession.commit();
        } else {
            sqlSession.rollback();
        }
        sqlSession.close();
    }

    try {
        // now we can see what happens if when a process engine is being
        // build with a version mismatch between library and db tables
        ProcessEngineConfiguration
                .createProcessEngineConfigurationFromResource(
                        "org/activiti/standalone/initialization/notables.activiti.cfg.xml")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE)
                .buildProcessEngine();

        fail("expected exception");
    } catch (ActivitiWrongDbException e) {
        assertTextPresent("version mismatch", e.getMessage());
        assertEquals("25.7", e.getDbVersion());
        assertEquals(ProcessEngine.VERSION, e.getLibraryVersion());
    }

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