From 1804f828c14c3007d08efeb011853d9eab58b00b Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Wed, 28 Aug 2013 13:35:44 +0200 Subject: [PATCH] MyBatis: Criteria example. --- .../src/main/java/de/example/mybatis/TestMain.java | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/MyBatis/src/main/java/de/example/mybatis/TestMain.java b/MyBatis/src/main/java/de/example/mybatis/TestMain.java index bffa47f..60e9bb2 100644 --- a/MyBatis/src/main/java/de/example/mybatis/TestMain.java +++ b/MyBatis/src/main/java/de/example/mybatis/TestMain.java @@ -11,6 +11,7 @@ import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.log4j.Logger; import de.example.mybatis.model.Ad; +import de.example.mybatis.model.AdCriteria; import de.example.mybatis.repository.mapper.AdMapper; @@ -86,6 +87,40 @@ public class TestMain { } finally { session.close(); } + + session = sqlSessionFactory.openSession(); + + try { + logger.info("Using criteria"); + + final AdCriteria adCriteria = new AdCriteria(); + + adCriteria.or().andAdMobileImageEqualTo("mobileImage.jpg") + .andCreatedAtNotEqualTo(new Date()); + + adCriteria.or().andAdMobileImageNotEqualTo("noMobileImage.jpg") + .andAdMobileImageIsNotNull(); + + // where (ad_mobile_image = "mobileImage.jpg" and created_at <> Now()) + // or (ad_mobile_image <> "noMobileImage.jpg" and ad_mobile_image is not null) + + final AdMapper adMapper = session.getMapper(AdMapper.class); + final List adLists = adMapper.selectByExampleWithBLOBs(adCriteria); + for (final Ad ad : adLists) { + logger.info("Ad id: " + ad.getId()); + if (ad.getAdGps() != null) { + logger.info("Ad GPS: " + new String(ad.getAdGps(), "UTF-8")); + } + logger.info("Ad mobileImage: " + ad.getAdMobileImage()); + logger.info("Ad companyCategId: " + ad.getCompanyCategId()); + logger.info("Ad companyId: " + ad.getCompanyId()); + logger.info("Ad createdAt: " + ad.getCreatedAt()); + logger.info("Ad updatedAt: " + ad.getUpdatedAt()); + logger.info("\n"); + } + } finally { + session.close(); + } } } -- 2.1.4