List of usage examples for org.apache.ibatis.session SqlSession getMapper
<T> T getMapper(Class<T> type);
From source file:PersistenceTest.java
License:Open Source License
@Test public void CE1() { SqlSessionFactory sessionfact = getSqlSessionFactory(); SqlSession sqlss = sessionfact.openSession(); PacienteMapper pm = sqlss.getMapper(PacienteMapper.class); //Deberia registrar paciente nuevo con mas de una consulta Paciente p4 = new Paciente(124567890, "TI", "Pepito Perez", Date.valueOf("1996-07-09")); Consulta c5 = new Consulta(Date.valueOf("2009-10-12"), "El paciente tiene fiebre"); Consulta c6 = new Consulta(Date.valueOf("2009-10-13"), "El paciente sigue con fiebre"); Set<Consulta> consultas4 = new HashSet<>(); consultas4.add(c5);// w w w. j a v a 2s.com consultas4.add(c6); p4.setConsultas(consultas4); pm.insertPaciente(p4); sqlss.commit(); for (Consulta c : p4.getConsultas()) { pm.insertConsulta(c, p4.getId(), p4.getTipo_id()); sqlss.commit(); } Paciente test = pm.loadPacienteById(p4.getId(), p4.getTipo_id()); LinkedList<String> cadenas = new LinkedList<>(); boolean funciona = p4.getId() == test.getId() && p4.getTipo_id().equals(test.getTipo_id()) && p4.getNombre().equals(test.getNombre()) && p4.getFechaNacimiento().equals(test.getFechaNacimiento()); for (Consulta c : test.getConsultas()) { cadenas.add(c.toString().split(",")[1] + c.toString().split(",")[2]); } for (Consulta c : p4.getConsultas()) { funciona = funciona && cadenas.contains(c.toString().split(",")[1] + c.toString().split(",")[2]); } Assert.assertTrue(funciona); }
From source file:PersistenceTest.java
License:Open Source License
@Test public void CE2() { SqlSessionFactory sessionfact = getSqlSessionFactory(); SqlSession sqlss = sessionfact.openSession(); PacienteMapper pm = sqlss.getMapper(PacienteMapper.class); Paciente p = new Paciente(98796, "TI", "Carmenzo", Date.valueOf("1995-07-10")); pm.insertPaciente(p);//from www . j a va 2 s . c o m sqlss.commit(); Paciente test = pm.loadPacienteById(p.getId(), p.getTipo_id()); sqlss.close(); Assert.assertEquals(test.toString(), p.toString()); }
From source file:PersistenceTest.java
License:Open Source License
@Test public void CE3() { SqlSessionFactory sessionfact = getSqlSessionFactory(); SqlSession sqlss = sessionfact.openSession(); PacienteMapper pm = sqlss.getMapper(PacienteMapper.class); Paciente p1 = new Paciente(123, "CC", "German Lopez", new Date(1994, 10, 10)); Consulta c1 = new Consulta(new Date(2016, 1, 13), "El paciente presenta fiebre alta"); Set<Consulta> consultas = new LinkedHashSet<Consulta>(); consultas.add(c1);/* w ww.j a va 2s. co m*/ p1.setConsultas(consultas); pm.insertPaciente(p1); sqlss.commit(); for (Consulta c : p1.getConsultas()) { pm.insertConsulta(c, p1.getId(), p1.getTipo_id()); sqlss.commit(); } Paciente test = pm.loadPacienteById(p1.getId(), p1.getTipo_id()); sqlss.close(); LinkedList<String> cadenas = new LinkedList<>(); boolean funciona = p1.getId() == test.getId() && p1.getTipo_id().equals(test.getTipo_id()) && p1.getNombre().equals(test.getNombre()) && p1.getFechaNacimiento().equals(test.getFechaNacimiento()); for (Consulta c : test.getConsultas()) { cadenas.add(c.toString().split(",")[1] + c.toString().split(",")[2]); } for (Consulta c : p1.getConsultas()) { funciona = funciona && cadenas.contains(c.toString().split(",")[1] + c.toString().split(",")[2]); } Assert.assertTrue(funciona); }
From source file:adl.RecordActivitySpeechlet.java
License:Open Source License
public void writeAction(String intent) { SqlSession session = null; try {/*from w ww .j a va2 s . c o m*/ session = SqlSessionHelper.getSessionFactory().openSession(); ActionMapper mapper = session.getMapper(ActionMapper.class); ActionBean actionBean = new ActionBean(); actionBean.setName(camelToUtterance(intent));//+ actionBean.setTime(new Timestamp(System.currentTimeMillis())); mapper.addActionBean(actionBean); session.commit(); } catch (Exception e) { e.printStackTrace(); } finally { session.close(); } }
From source file:cern.c2mon.server.history.dao.LoggerDAO.java
License:Open Source License
/** * Inserts into the database a set of rows containing the data coming in * several IFallback objects// w ww .jav a2 s. co m * * @param data * List of IFallback object whose data has to be inserted in the DB * @throws IDBPersistenceException * An exception is thrown in case an error occurs during the data * insertion. The exception provides also the number of already * committed objects */ @SuppressWarnings("unchecked") // add generics to persistence manager public final void storeData(final List data) throws IDBPersistenceException { SqlSession session = null; int size = data.size(); int commited = 0; T tag; try { // We use batch set of statements to improve performance session = sqlSessionFactory.openSession(ExecutorType.BATCH, false); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Obtained batch transacted SQL session (session: " + session.toString() + ")"); } LoggerMapper<T> persistenceMapper = session.getMapper(mapperInterface); // Iterate through the list of DataTagCacheObjects to insert // them one by one for (int i = 0; i != size; i++) { if ((0 == i % RECORDS_PER_BATCH) && i > 0) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("storeData([Collection]) : Commiting rows for i=" + i); } session.commit(); commited = i; } if (data.get(i) != null) { tag = (T) data.get(i); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Logging object with ID: " + tag.getId()); } persistenceMapper.insertLog(tag); } } // Commit the transaction session.commit(); commited = size; } catch (PersistenceException e) { LOGGER.error("storeData([Collection]) : Error executing/closing prepared statement for " + data.size() + " dataTags", e); try { if (session != null) { session.rollback(); } } catch (Exception sql) { LOGGER.error("storeData([Collection]) : Error rolling back transaction.", sql); } throw new IDBPersistenceException(e.getMessage(), commited); } finally { try { if (session != null) { session.close(); } } catch (Exception e) { LOGGER.error("storeData([Collection]) : Error closing session.", e); } } }
From source file:cern.c2mon.server.history.dao.LoggerDAO.java
License:Open Source License
@Override public void storeData(IFallback object) throws IDBPersistenceException { SqlSession session = null; try {//from w ww .j a v a 2 s. c om session = sqlSessionFactory.openSession(); LoggerMapper<T> loggerMapper = session.getMapper(mapperInterface); loggerMapper.insertLog((T) object); session.commit(); } catch (PersistenceException ex1) { String message = "Exception caught while persisting an object to the history"; LOGGER.error(message, ex1); if (session != null) session.rollback(); throw new IDBPersistenceException(message, ex1); } finally { if (session != null) session.close(); } }
From source file:cn.com.git.udmp.test.mybatis.cursor.UdmpCursorSimpleTest.java
License:Apache License
@Test public void shouldGetAllUser() { SqlSession sqlSession = sqlSessionFactory.openSession(); BatchTestSouceMapper mapper = sqlSession.getMapper(BatchTestSouceMapper.class); Cursor<BatchTestSouce> usersCursor = mapper.findListByCursor(); try {//from w ww . j a va 2s . c o m Assert.assertFalse(usersCursor.isOpen()); // Cursor is just created, current index is -1 Assert.assertEquals(-1, usersCursor.getCurrentIndex()); Iterator<BatchTestSouce> iterator = usersCursor.iterator(); // Check if hasNext, fetching is started Assert.assertTrue(iterator.hasNext()); Assert.assertTrue(usersCursor.isOpen()); Assert.assertFalse(usersCursor.isConsumed()); // next() has not been called, index is still -1 Assert.assertEquals(-1, usersCursor.getCurrentIndex()); BatchTestSouce user = iterator.next(); // Assert.assertEquals("User1", user.getVcharC()); System.out.println(user.getVcharC()); Assert.assertEquals(0, usersCursor.getCurrentIndex()); user = iterator.next(); System.out.println(user.getVcharC()); // Assert.assertEquals("User2", user.getVcharC()); Assert.assertEquals(1, usersCursor.getCurrentIndex()); user = iterator.next(); System.out.println(user.getVcharC()); // Assert.assertEquals("User3", user.getVcharC()); Assert.assertEquals(2, usersCursor.getCurrentIndex()); user = iterator.next(); System.out.println(user.getVcharC()); // Assert.assertEquals("User4", user.getVcharC()); Assert.assertEquals(3, usersCursor.getCurrentIndex()); user = iterator.next(); System.out.println(user.getVcharC()); // Assert.assertEquals("User5", user.getVcharC()); Assert.assertEquals(4, usersCursor.getCurrentIndex()); // Check no more elements Assert.assertFalse(iterator.hasNext()); Assert.assertFalse(usersCursor.isOpen()); Assert.assertTrue(usersCursor.isConsumed()); } finally { sqlSession.close(); } }
From source file:cn.cuizuoli.appranking.service.AppRankingService.java
License:Apache License
/** * batchAdd/* ww w . ja v a 2 s .c o m*/ * @param appRankingList */ @Transactional public void batchAdd(List<AppRanking> appRankingList) { SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); AppRankingRepository batchAppRankingRepository = sqlSession.getMapper(AppRankingRepository.class); int i = 1; for (AppRanking appRanking : appRankingList) { appRanking.setId(appRankingIncrementer.nextStringValue()); appRanking.setDateHour(DateTime.now().toString("yyyyMMddHH")); batchAppRankingRepository.insert(appRanking); if (i % 100 == 0) { sqlSession.commit(); } i++; } sqlSession.commit(); sqlSession.close(); }
From source file:com.albertzhe.mybatis_helloworld.service.AdminService.java
public Admin getAdmin(String key) { Long id = new Long(0); Admin admin = null;/*w w w . j av a 2s. c o m*/ Pattern pattern = Pattern.compile("^[0-9]*$"); Matcher matcher = pattern.matcher(key); if (!matcher.matches()) { return admin; } else { id = Long.valueOf(key); } // AdminMapper adminMapper = new AdminMapperImpl(SqlSessionUtil.getInstance()); // admin = adminMapper.getAdminByID(id); /* 2 MyBatis ?? ?mybatis ? mapper ? */ SqlSession sqlSession = SqlSessionUtil.getInstance().openSession(); AdminMapper adminMapper = sqlSession.getMapper(AdminMapper.class); admin = adminMapper.getAdminByID(id); return admin; }
From source file:com.aol.one.patch.examples.persistence.service.CartService.java
License:Open Source License
public void addProductInfo(SqlSession sqlSession, CartProductInfo info) { sqlSession.getMapper(CartProductsMapper.class).addCartProduct(info); }