Java Code Examples for org.apache.ibatis.session.SqlSession#commit()
The following examples show how to use
org.apache.ibatis.session.SqlSession#commit() .
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: DemoService.java From mybatis-daoj with Apache License 2.0 | 6 votes |
public void findBooks() { log.info("findBooks() >>"); SqlSession sqlSession = sessionFactory.openSession(false); try { BookDao bookDao = sqlSession.getMapper(BookDao.class); HashMap<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("minCost", new Float(0)); paramMap.put("maxCost", new Float(100)); List<Book> bookList = bookDao.find(paramMap, new PageBounds(1, 6)); for (Book book : bookList) { log.info(book.getBookId() + " " + book.getTitle() + " " + book.getPrice()); } log.info(""); sqlSession.commit(true); } finally { sqlSession.close(); } }
Example 2
Source File: RegisterActor.java From Nickle-Scheduler with Apache License 2.0 | 6 votes |
public void registerExecutorAndJob(RegisterEvent registerEvent) { SqlSession sqlSession = sqlSessionFactory.openSession(false); log.info("注册开始:{}", registerEvent); try { insertExecutorAndJob(registerEvent, sqlSession); insertTrigger(registerEvent, sqlSession); sqlSession.commit(); //通知注册成功 getSender().tell(REGISTER_OK, getSelf()); } catch (Exception e) { sqlSession.rollback(); log.error("注册发生错误:{}", e.getMessage()); //通知注册失败 getSender().tell(REGISTER_FAIL, getSelf()); } finally { sqlSession.close(); } log.info("注册结束"); }
Example 3
Source File: BatchInsertMain.java From blog with BSD 2-Clause "Simplified" License | 6 votes |
public static void main(String[] args) throws Exception { String resource = "mybatis-config2.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); FileAnalysis fileAnalysis = new FileAnalysis(); SqlSession session = sqlSessionFactory.openSession(); try { FielAnalysisMapper mapper = session.getMapper(FielAnalysisMapper.class); fileAnalysis = mapper.selectFileAnalysis("01"); session.commit(); } finally { session.close(); } FileAnalysisInsert fileAnalysisInsert = new FileAnalysisInsert(sqlSessionFactory); fileAnalysisInsert.batchInsertOrder(fileAnalysis); }
Example 4
Source File: FlushStatementNpeTest.java From mybaties with Apache License 2.0 | 6 votes |
@Test public void testSameUpdateAfterCommitBatch() { SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); try { PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class); Person person = personMapper.selectById(1); person.setFirstName("Simone"); // Execute first update then commit. personMapper.update(person); sqlSession.commit(); // Execute same update a second time. This used to raise an NPE. personMapper.update(person); sqlSession.commit(); } finally { sqlSession.close(); } }
Example 5
Source File: CeshiMyBatis.java From cg-blog with MIT License | 6 votes |
@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 6
Source File: MybatisUtil.java From code with Apache License 2.0 | 6 votes |
public static void template(Callback callback) { SqlSession session = null; //1.读取配置文件 try (InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");) { //2.创建构建者对象 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); //3.创建 SqlSession 工厂对象 factory = builder.build(in); //4.创建 SqlSession 对象 session = factory.openSession(); //5.执行操作 callback.execute(session); //6.提交或回滚事务 session.commit(); } catch (IOException e) { if (session != null) { session.rollback(); } e.printStackTrace(); } finally { if (session != null) { session.close(); } } }
Example 7
Source File: FlushStatementNpeTest.java From mybatis with Apache License 2.0 | 6 votes |
@Test public void testSameUpdateAfterCommitBatch() { SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); try { PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class); Person person = personMapper.selectById(1); person.setFirstName("Simone"); // Execute first update then commit. personMapper.update(person); sqlSession.commit(); // Execute same update a second time. This used to raise an NPE. personMapper.update(person); sqlSession.commit(); } finally { sqlSession.close(); } }
Example 8
Source File: BatchKeysTest.java From mybaties with Apache License 2.0 | 6 votes |
@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 9
Source File: UserDaoTest.java From java_study with Apache License 2.0 | 5 votes |
@Test public void updateUser() { SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.updateUser(new User(4,"王二小","67744444")); sqlSession.commit(); sqlSession.close(); }
Example 10
Source File: ChatGroupUserAction.java From anychat with MIT License | 5 votes |
public static ChatGroupUser updateChatGroupUser(String userId, String chatGroupId, Date chatCreateTime) { if (StringUtil.stringIsNull(userId) || StringUtil.stringIsNull(chatGroupId)) { return null; } List<ChatGroupUser> chatGroupUserList = getChatGroupUserList(userId, chatGroupId); if (chatGroupUserList == null || chatGroupUserList.size() == 0) { return null; } ChatGroupUser chatGroupUser = new ChatGroupUser(); chatGroupUser.setUserId(userId); chatGroupUser.setChatGroupId(chatGroupId); chatGroupUser.setChatGroupUserUpdateTime(chatCreateTime); SqlSession sqlSession = null; try { sqlSession = MybatisManager.getSqlSession(); ChatGroupUserMapper chatGroupUserMapper = sqlSession.getMapper(ChatGroupUserMapper.class); ChatGroupUserCriteria chatGroupUserCriteria = new ChatGroupUserCriteria(); ChatGroupUserCriteria.Criteria criteria = chatGroupUserCriteria.createCriteria(); criteria.andUserIdEqualTo(userId); criteria.andChatGroupIdEqualTo(chatGroupId); criteria.andChatGroupUserUpdateTimeLessThan(chatCreateTime); int result = chatGroupUserMapper.updateByExampleSelective(chatGroupUser, chatGroupUserCriteria); if (result != 1) { MybatisManager.log.warn("修改聊天组用户失败"); return null; } sqlSession.commit(); } catch (Exception e) { if (sqlSession != null) { sqlSession.rollback(); } MybatisManager.log.error("修改聊天组用户异常", e); return null; } finally { if (sqlSession != null) { sqlSession.close(); } } return getChatGroupUserList(userId, chatGroupId).get(0); }
Example 11
Source File: ChatAction.java From anychat with MIT License | 5 votes |
public static boolean updateChat(List<String> chatIdList) { if (chatIdList == null || chatIdList.size() == 0) { return false; } SqlSession sqlSession = null; Chat chat = new Chat(); chat.setChatType((byte) ChatConfig.CHAT_TYPE_RECEIVE); try { sqlSession = MybatisManager.getSqlSession(); ChatMapper chatMapper = sqlSession.getMapper(ChatMapper.class); ChatCriteria chatCriteria = new ChatCriteria(); ChatCriteria.Criteria criteria = chatCriteria.createCriteria(); criteria.andChatIdIn(chatIdList); int result = chatMapper.updateByExampleSelective(chat, chatCriteria); if (result == 0) { MybatisManager.log.warn("修改用户聊天内容失败"); return false; } if (result != chatIdList.size()) { MybatisManager.log.warn("修改用户聊天内容失败,提交" + chatIdList.size() + "条,修改" + result + "条"); } sqlSession.commit(); } catch (Exception e) { if (sqlSession != null) { sqlSession.rollback(); } MybatisManager.log.error("修改用户聊天内容异常", e); return false; } finally { if (sqlSession != null) { sqlSession.close(); } } return true; }
Example 12
Source File: TestBatisXML.java From java-course-ee with MIT License | 5 votes |
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 13
Source File: IDAllocDaoImpl.java From SpringMVC-Project with MIT License | 5 votes |
@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 14
Source File: IDAllocDaoImpl.java From SpringMVC-Project with MIT License | 5 votes |
@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 15
Source File: UserMain.java From blog with BSD 2-Clause "Simplified" License | 5 votes |
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 16
Source File: TransactionServiceImpl.java From nuls-v2 with MIT License | 5 votes |
@Override public void createTxTables(String tableName, String indexName, int number) { SqlSession sqlSession = sqlSessionFactory.openSession(); TransactionMapper mapper = sqlSession.getMapper(TransactionMapper.class); List<TxTable> list = new ArrayList<>(); for (int i = 0; i <= number; i++) { TxTable txTable = new TxTable(tableName + "_" + i, indexName + "_" + i); list.add(txTable); } mapper.createTxTables(list); sqlSession.commit(); sqlSession.close(); System.out.println("batch OK"); }
Example 17
Source File: TestBatisInterface.java From java-course-ee with MIT License | 5 votes |
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(); RegionDao dao = session.getMapper(RegionDao.class); System.out.println("RegionDao class: " + dao.getClass().getCanonicalName()); Region region = new Region(); region.setRegionName("12345"); dao.insertRegion(region); session.commit(); System.out.println("RegionId: " + region.getRegionId()); findAndGet(dao); region.setRegionName("54321"); dao.updateRegion(region); session.commit(); findAndGet(dao); dao.deleteRegionById(region.getRegionId()); session.commit(); findAndGet(dao); }
Example 18
Source File: ForceFlushOnSelectTest.java From mybatis with Apache License 2.0 | 5 votes |
@Test public void testSelectShouldFlushLocalCacheIfFlushLocalCacheAtferEachStatementIsTrue() throws SQLException { sqlSessionFactory.getConfiguration().setLocalCacheScope(LocalCacheScope.STATEMENT); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE); try { PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class); List<Person> people = personMapper.selectAllNoFlush(); updateDatabase(sqlSession.getConnection()); people = personMapper.selectAllFlush(); assertEquals("Simone", people.get(0).getFirstName()); sqlSession.commit(); } finally { sqlSession.close(); } }
Example 19
Source File: CeshiMyBatis.java From cg-blog with MIT License | 5 votes |
@Test public void testDeleteById(){ String resource = "sqlMapConfig.xml"; //定位核心配置文件 InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 创建 SqlSessionFactory SqlSession sqlSession = sqlSessionFactory.openSession(); // 获取到 SqlSession sqlSession.delete("yeepay.payplus.mapper.UserMapper.deleteById", 2); sqlSession.commit(); //默认是不自动提交,必须手工提交 }
Example 20
Source File: MybatisDbHelper.java From nuls-v2 with MIT License | 4 votes |
public void commit() { SqlSession sqlSession = sessionHolder.get(); sqlSession.commit(); }