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

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

Introduction

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

Prototype

int insert(String statement, Object parameter);

Source Link

Document

Execute an insert statement with the given parameter object.

Usage

From source file:io.starter.TestContent.java

License:Open Source License

/**
 * test inserting some data/*from ww w . j av  a  2 s.co m*/
 * 
 */
@Test
public void testInsertAndMarkContentTrashed() {

    Content content = new Content();
    content.setUserId(42);
    content.setAuthor(42);
    content.setAuthorDevice(1);
    content.setCopyright("Copyright 2013. All rights reserved");
    content.setDescription("CheezBurger Cat");
    content.setLicense("CC");
    content.setMimeType("text/plain");
    content.setUrl("http://www.hazcheezburger.com");
    content.setFlag(FLAG_STAR);
    content.setPostDate(new java.util.Date(System.currentTimeMillis()));
    // ContentMapper.insert(content);
    SqlSession session = sqlSessionFactory.openSession(true);
    session.insert("io.starter.dao.ContentMapper.insert", content);

    Integer id = content.getId();
    if (id == null)
        fail("Content insertion returns null ID");

    try {
        Content ccx = session.selectOne("io.starter.dao.ContentMapper.selectObjByPrimaryKey", id);
        User u = ccx.getUser();
        if (u == null)
            fail("User is null for content object inserted with user_id = 1. Content ID: " + id);

        String s = ccx.getDescription();
        if (s == null)
            fail("Failed to insert Content ID: " + id);
    } catch (Exception x) {
        fail("Exception fetching content from new Content ID: " + id);
    }

    // test deletion of that new content
    // content.setFlag(FLAG_DELETED);

    session = sqlSessionFactory.openSession(true);

    session.delete("io.starter.dao.ContentMapper.deleteByPrimaryKey", content);

    try {
        Content ccd = session.selectOne("io.starter.dao.ContentMapper.selectByPrimaryKey", id);

        if (ccd.getFlag() != FLAG_DELETED)
            fail("Failed to mark deleted newly inserted Content ID: " + id);
    } catch (Exception x) {
        // good!
    }
    session.close();

}

From source file:io.starter.TestContent.java

License:Open Source License

/**
 * test inserting some data/*from  w  ww .  j  av  a  2 s .  c o  m*/
 * 
 */
@Test
public void testInsertAndDeleteContent() {
    Content content = new Content();
    content.setAuthor(1);
    content.setAuthorDevice(1);
    content.setCopyright("Copyright 2013. All rights reserved");
    content.setDescription("CheezBurger Cat");
    content.setLicense("CC");
    content.setFlag(FLAG_STAR);
    content.setMimeType("text/plain");
    content.setUrl("http://www.hazcheezburger.com");
    content.setPostDate(new java.util.Date(System.currentTimeMillis()));

    sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
    SqlSession session = sqlSessionFactory.openSession(true);
    session.insert("io.starter.dao.ContentMapper.insert", content);

    Integer id = content.getId();
    if (id == null)
        fail("Content insertion returns null ID");

    try {
        Content ccx = session.selectOne("io.starter.dao.ContentMapper.selectByPrimaryKey", id);

        String s = ccx.getDescription();
        if (s == null)
            fail("Failed to insert Content ID: " + id);
    } catch (Exception x) {
        fail("Exception fetching content from new Content ID: " + id);
    }

    // test deletion of that new content
    Content cxdel = new Content();
    cxdel.setId(id);

    session = sqlSessionFactory.openSession(true);
    session.delete("io.starter.dao.ContentMapper.deleteByPrimaryKey", cxdel);

    session.close();

    try {
        Content ccd = session.selectOne("io.starter.dao.ContentMapper.selectByPrimaryKey", id);

        String s = ccd.getDescription();
        if (s != null)
            fail("Failed to delete inserted Content ID: " + id);
    } catch (Exception x) {
        // good!
    }

}

From source file:io.starter.TestContent.java

License:Open Source License

/**
 * test cpntent ownership ratings/*  w ww.ja  v  a2  s.c  o m*/
 * 
 * @throws Exception
 */
@Test
public void testOwnershipPermissions() throws Exception {

    Content content = new Content();
    content.setAuthor(42);
    content.setUserId(42);
    content.setAuthorDevice(1);
    content.setCopyright("Copyright 2014. All rights reserved");
    content.setDescription("TEST Starter Content");
    content.setLicense("CC");

    content.setFlag(FLAG_STAR);
    content.setMimeType("text/html");
    content.setUrl("http://$$PROJECT_DOMAIN$$");
    content.setPostDate(new java.util.Date(System.currentTimeMillis()));
    sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
    SqlSession session = sqlSessionFactory.openSession(true);
    session.insert("io.starter.dao.ContentMapper.insert", content);
    session.commit();

    Integer id = content.getId();
    assertTrue(id != null);

    Content ccx = session.selectOne("io.starter.dao.ContentMapper.selectObjByPrimaryKey", id);

    User user = ccx.getUser();
    assertTrue(ccx.getUserId() == 42);

    // assert that contact info is stripped from this
    assertEquals(null, user.getEmail());

    // and that we got the right user
    assertEquals("test", user.getUsername());

    // 1. Build the Subject instance for the test to run:
    Subject subjectUnderTest = new Subject.Builder(getSecurityManager()).buildSubject();

    // 2. Bind the subject to the current thread:
    setSubject(subjectUnderTest);
    // see /login.jsp for these form fields
    String username = "test";
    String password = API_CRYPT_KEY;

    // create a UsernamePasswordToken using the
    // username and password provided by the user
    UsernamePasswordToken token = new UsernamePasswordToken(username, password);

    // get the user (aka subject) associated with this request.

    Subject subject = SecurityUtils.getSubject();

    try {
        subject.checkPermission("thing:action");
        fail("User INCORRECTLY has permission thing:action");
    } catch (AuthorizationException x) {
        // GOOD!
    }

    subject.login(token);
    user.setSubject(subject);

    ContentData.setContentOwnerPermissions(content, user, session);

    WildcardPermission owner = new WildcardPermission(SystemConstants.SECURITY_TARGET_TYPE_CONTENT + ":"
            + SystemConstants.SECURITY_ACL_OWNER + ":" + content.getId());

    assert (user.checkAccess(owner));

    // test deletion of that new content
    session = sqlSessionFactory.openSession(true);
    session.delete("io.starter.dao.ContentMapper.deleteByPrimaryKey", ccx);

    try {
        Content ccd = session.selectOne("io.starter.dao.ContentMapper.selectByPrimaryKey", id);

        String s1 = ccd.getDescription();
        if (s1 != null)
            fail("Failed to delete inserted Content ID: " + id);
    } catch (Exception x) {
        // good!
    }

    session.close();
}

From source file:io.starter.TestContent.java

License:Open Source License

/**
 * test cpntent ratings!//from w w  w. j  ava 2s. c o  m
 * 
 * @throws Exception
 */
@Test
@Ignore
// we don't select ratings objects anymore... revisit someday
public void testRatings() throws Exception {
    Content content = new Content();
    content.setAuthor(42);
    content.setUserId(42);
    content.setAuthorDevice(1);
    content.setCopyright("Copyright 2013. All rights reserved");
    content.setDescription("TEST Ostrich Pillow");
    content.setLicense("CC");

    content.setFlag(FLAG_STAR);
    content.setMimeType("text/html");
    content.setUrl("http://www.ostrichpillow.com");

    content.setPostDate(new java.util.Date(System.currentTimeMillis()));
    sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
    SqlSession session = sqlSessionFactory.openSession(true);
    session.insert("io.starter.dao.ContentMapper.insert", content);

    Integer id = content.getId();
    assertTrue(id != null);

    // rate the bastard
    ContentRating rx = new ContentRating();
    rx.setContentId(id);
    rx.setFlag(0);
    rx.setRaterId(1);
    rx.setReview("that kinda sucked");
    rx.setType(1);
    rx.setValue((long) 3);
    session.insert("io.starter.dao.ContentRatingMapper.insert", rx);

    session.commit();

    Content ccx = session.selectOne("io.starter.dao.ContentMapper.selectObjByPrimaryKey", id);

    String s = ccx.getDescription();
    if (s == null)
        fail("Failed to insert Content ID: " + id);

    ArrayList<ContentRating> ratings = ccx.getRatings();
    ContentRating rating = ratings.get(0);
    assertEquals("that kinda sucked", rating.getReview());

    User user = ccx.getUser();
    assertTrue(ccx.getUserId() == 1);
    assertEquals("john@$$PROJECT_DOMAIN$$", user.getEmail());

    // test deletion of that new content

    session = sqlSessionFactory.openSession(true);
    session.delete("io.starter.dao.ContentRatingMapper.deleteByPrimaryKey", rx);

    session.delete("io.starter.dao.ContentMapper.deleteByPrimaryKey", ccx);

    session.close();

    try {
        Content ccd = session.selectOne("io.starter.dao.ContentMapper.selectByPrimaryKey", id);

        String s1 = ccd.getDescription();
        if (s1 != null)
            fail("Failed to delete inserted Content ID: " + id);
    } catch (Exception x) {
        // good!
    }

    session.close();
}

From source file:mbg.test.mb3.miscellaneous.MiscellaneousTest.java

License:Apache License

@Test
public void testAnotherAwfulTableInsert() {
    SqlSession sqlSession = sqlSessionFactory.openSession();

    try {//from  w  w w  .j av a  2  s .  c om
        Anotherawfultable record = new Anotherawfultable();
        record.setId(5);
        record.setSelect("select");
        record.setInsert("insert");

        sqlSession.insert("mbg.test.mb3.generated.miscellaneous.xml.AnotherawfultableMapper.insert", record);

        Anotherawfultable key = new Anotherawfultable();
        key.setId(5);

        Anotherawfultable returnedRecord = (Anotherawfultable) sqlSession.selectOne(
                "mbg.test.mb3.generated.miscellaneous.xml.AnotherawfultableMapper.selectByPrimaryKey", key);

        assertEquals(record.getId(), returnedRecord.getId());
        assertEquals(record.getSelect(), returnedRecord.getSelect());
        assertEquals(record.getInsert(), returnedRecord.getInsert());
        assertEquals(record.getUpdate(), returnedRecord.getUpdate());
        assertEquals(record.getDelete(), returnedRecord.getDelete());
    } finally {
        sqlSession.close();
    }
}

From source file:mybatis.client.MyJFrame.java

void addData(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);
    //5? ?  map? key -> mybatis 

    /*//from   www.  j  a  v  a2 s . c  om
            MemVO mem = new MemVO();
            mem.setId(s_id);
            mem.setName(s_name);
            mem.setEmail(s_email);
            mem.setPhone(s_phone);
    */
    // 3) factory  SqlSession? 
    SqlSession ss = factory.openSession(true);

    // 4) sql  
    int cnt = ss.insert("mem.add", map);

    if (cnt == 1) {
        JOptionPane.showMessageDialog(this, "??.");
    } else {
        JOptionPane.showMessageDialog(this, " ");
    }

}

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

License:Apache License

public int insert(final String statement, final Object parameter) throws SQLException {
    return this.execute(new SqlSessionCallback<Integer>() {
        public Integer doSqlSession(SqlSession sqlSession) {
            return sqlSession.insert(statement, parameter);
        }/*from  w ww  .  java 2  s  . com*/
    });
}

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

License:Open Source License

public void addHeader(NntpArticleHeader header) throws Exception {
    try {/*from ww  w.jav  a2  s  . c o  m*/
        SqlSession session = sqlFactory.openSession(true);
        HashMap map = DBUtils.mapHeader(header);
        session.insert("insertHeader", map);
        session.close();
    } catch (Exception e) {
        log.error("could not add header to database: " + e.getMessage());
        //e.printStackTrace();
        throw e;
    }
}

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

License:Open Source License

/**
 * create the NntpGroup List table to keep track of groups
 * that are being used/*  ww  w  .  jav a2 s.co m*/
 */
public void createServerGroups() throws Exception {
    SqlSession session = sqlFactory.openSession();
    session.insert("createGroupsTable", groupsTable);
    session.commit();
    session.close();
}

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

License:Open Source License

/**
 * add an NntpGroup to our groups list//from  w w  w. j av  a 2  s  . c  o  m
 */
public void addGroup(NntpGroup group) throws Exception {
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("table", groupsTable);
    map.put("server", group.getServer());
    map.put("name", group.getName());
    map.put("hi", group.getHighID());
    map.put("low", group.getLowID());

    SqlSession session = sqlFactory.openSession();
    session.insert("addGroup", map);

    // now ensure there is an ArticleHeader table for it
    session.insert("createHeaderTable", DBUtils.convertGroup(group.getName()));

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