Java Code Examples for org.apache.ibatis.session.SqlSession#getMapper()
The following examples show how to use
org.apache.ibatis.session.SqlSession#getMapper() .
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: PageSizeLessThenOrEqualZeroTest.java From Mybatis-PageHelper with MIT License | 6 votes |
/** * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数 */ @Test public void testWithRowbounds() { //注意这里是MybatisRowBoundsHelper,会求count SqlSession sqlSession = MybatisHelper.getSqlSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); try { //limit=0,这时候相当于用分页插件求count,但是前提必须是配置rounbounds方式求count,否则都是-1 List<User> list = userMapper.selectAll(new RowBounds(1, -1)); PageInfo<User> page = new PageInfo<User>(list); assertEquals(0, list.size()); assertEquals(183, page.getTotal()); //limit<0的时候同上 list = userMapper.selectAll(new RowBounds(1, -100)); page = new PageInfo<User>(list); assertEquals(0, list.size()); assertEquals(183, page.getTotal()); } finally { sqlSession.close(); } }
Example 2
Source File: TestUserLogin.java From tk-mybatis with MIT License | 6 votes |
/** * 根据主键更新非null */ @Test public void testUpdateByPrimaryKeySelective() { SqlSession sqlSession = MybatisHelper.getSqlSession(); try { UserLoginMapper mapper = sqlSession.getMapper(UserLoginMapper.class); Map<String, Object> key = new HashMap<String, Object>(); key.put("logid", 1); key.put("username", "test1"); UserLogin userLogin = mapper.selectByPrimaryKey(key); Assert.assertNotNull(userLogin); userLogin.setLogindate(null); userLogin.setLoginip("1.1.1.1"); //不会更新username Assert.assertEquals(1, mapper.updateByPrimaryKeySelective(userLogin)); userLogin = mapper.selectByPrimaryKey(key); Assert.assertNotNull(userLogin.getLogindate()); Assert.assertEquals("1.1.1.1", userLogin.getLoginip()); } finally { sqlSession.close(); } }
Example 3
Source File: KeySqlTest.java From Mapper with MIT License | 6 votes |
@Test public void testUserSqlBefore() { SqlSession sqlSession = getSqlSession(); try { UserSqlBeforeMapper mapper = sqlSession.getMapper(UserSqlBeforeMapper.class); UserSqlBefore user = new UserSqlBefore(); user.setName("liuzh"); Assert.assertEquals(1, mapper.insert(user)); Assert.assertEquals(new Integer(12345), user.getId()); user = mapper.selectByPrimaryKey(12345); Assert.assertEquals("liuzh", user.getName()); } finally { sqlSession.close(); } }
Example 4
Source File: TypeHandlerTest2.java From Mapper with MIT License | 6 votes |
@Test public void testDelete(){ SqlSession sqlSession = getSqlSession(); try { User2Mapper userMapper = sqlSession.getMapper(User2Mapper.class); Assert.assertEquals(1, userMapper.deleteByPrimaryKey(1)); User2 user = new User2(); Address address = new Address(); address.setProvince("Hebei"); address.setCity("Handan"); user.setAddress(address); user.setState(StateEnum.enabled); Assert.assertEquals(0, userMapper.delete(user)); user.setState(StateEnum.disabled); Assert.assertEquals(1, userMapper.delete(user)); } finally { sqlSession.close(); } }
Example 5
Source File: SelectKeyTest.java From mybaties with Apache License 2.0 | 6 votes |
@Test public void testAnnotatedInsertTable2WithSelectKeyWithKeyObjectXml() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { Name name = new Name(); name.setName("barney"); AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class); int rows = mapper.insertTable2WithSelectKeyWithKeyObjectXml(name); assertEquals(1, rows); assertEquals(22, name.getNameId()); assertEquals("barney_fred", name.getGeneratedName()); } finally { sqlSession.close(); } }
Example 6
Source File: TestCache.java From Mapper with MIT License | 6 votes |
@Test public void testCache() { SqlSession sqlSession = MybatisHelper.getSqlSession(); try { CachedCountryMapper mapper = sqlSession.getMapper(CachedCountryMapper.class); Country country = new Country(); country.setCountrycode("CN"); //第一次查询,会被缓存 country = mapper.selectOne(country); //只有close才会真正缓存,而不是用一级缓存 sqlSession.close(); //====================================================================== sqlSession = MybatisHelper.getSqlSession(); mapper = sqlSession.getMapper(CachedCountryMapper.class); country = new Country(); country.setCountrycode("CN"); //第二次查询,会使用第一次的缓存 country = mapper.selectOne(country); //只有close才会真正缓存,而不是用一级缓存 sqlSession.close(); } finally { sqlSession.close(); } }
Example 7
Source File: MybatisCacheTest.java From code with Apache License 2.0 | 6 votes |
/** * 测试二级缓存 */ @Test public void testCache2() { SqlSession sqlSession1 = factory.openSession(); UserMapper dao1 = sqlSession1.getMapper(UserMapper.class); User user1 = dao1.findByUserId(41); System.out.println(user1); sqlSession1.close();//一级缓存消失 SqlSession sqlSession2 = factory.openSession(); UserMapper dao2 = sqlSession2.getMapper(UserMapper.class); User user2 = dao2.findByUserId(41); System.out.println(user2); sqlSession2.close(); //false,由于二级缓存是使用序列化的方式来保存对象,所以对象地址不同 System.out.println(user1 == user2); }
Example 8
Source File: SPTest.java From mybaties with Apache License 2.0 | 6 votes |
@Test public void testEchoDate() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { HashMap<String, Object> parameter = new HashMap<String, Object>(); Date now = new Date(); parameter.put("input date", now); SPMapper spMapper = sqlSession.getMapper(SPMapper.class); spMapper.echoDate(parameter); java.sql.Date outDate = new java.sql.Date(now.getTime()); assertEquals(outDate.toString(), parameter.get("output date").toString()); } finally { sqlSession.close(); } }
Example 9
Source File: MapperExtendTest.java From mybatis with Apache License 2.0 | 5 votes |
@Test public void shouldGetAUserWithAnOverloadedAnnotatedMethod() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { ParentMapper mapper = sqlSession.getMapper(MapperOverload.class); User user = mapper.getUserAnnotated(); Assert.assertEquals("User2", user.getName()); } finally { sqlSession.close(); } }
Example 10
Source File: TestSelectByExample.java From tk-mybatis with MIT License | 5 votes |
@Test public void testSelectByExampleInNotIn2() { SqlSession sqlSession = MybatisHelper.getSqlSession(); try { CountryMapper mapper = sqlSession.getMapper(CountryMapper.class); Example example = new Example(Country.class); example.createCriteria().andIn("id", Arrays.asList(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11})) .andNotIn("id", Arrays.asList(new Object[]{11})); List<Country> countries = mapper.selectByExample(example); //查询总数 Assert.assertEquals(10, countries.size()); } finally { sqlSession.close(); } }
Example 11
Source File: AutomappingTest.java From mybatis with Apache License 2.0 | 5 votes |
@Test public void shouldGetBooks() { // set automapping to default partial sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL); SqlSession sqlSession = sqlSessionFactory.openSession(); try { Mapper mapper = sqlSession.getMapper(Mapper.class); // no errors throw List<Book> books = mapper.getBooks(); Assert.assertTrue("should return results,no errors throw", !books.isEmpty()); } finally { sqlSession.close(); } }
Example 12
Source File: CglibNPETest.java From mybatis with Apache License 2.0 | 5 votes |
@Test public void testSelectWithStringSQLInjection() { SqlSession sqlSession = sqlSessionFactory.openSession(); try { PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class); Person selected1 = personMapper.selectByStringId("1"); Assert.assertEquals(1, selected1.getId().longValue()); } finally { sqlSession.close(); } }
Example 13
Source File: SPTest.java From mybaties with Apache License 2.0 | 5 votes |
@Test public void testGetNamesAndItems_a2() throws SQLException { SqlSession sqlSession = sqlSessionFactory.openSession(); try { SPMapper spMapper = sqlSession.getMapper(SPMapper.class); List<List<?>> results = spMapper.getNamesAndItemsAnnotatedWithXMLResultMap(); assertEquals(2, results.size()); assertEquals(4, results.get(0).size()); assertEquals(3, results.get(1).size()); } finally { sqlSession.close(); } }
Example 14
Source File: BindingTest.java From mybaties with Apache License 2.0 | 5 votes |
@Test public void shouldExecuteBoundSelectListOfBlogsStatementUsingProvider() { SqlSession session = sqlSessionFactory.openSession(); try { BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class); List<Blog> blogs = mapper.selectBlogsUsingProvider(); assertEquals(2, blogs.size()); } finally { session.close(); } }
Example 15
Source File: FileAnalysisInsert.java From blog with BSD 2-Clause "Simplified" License | 5 votes |
private void updateFileAnalysisStatus(FileAnalysis fileAnalysis, String status) { fileAnalysis.setStatus(status); SqlSession session = sqlSessionFactory.openSession(); try { FielAnalysisMapper mapper = session.getMapper(FielAnalysisMapper.class); mapper.updateFileAnalysis(fileAnalysis); session.commit(); } finally { session.close(); } }
Example 16
Source File: NpeExtendsTest.java From mybaties with Apache License 2.0 | 5 votes |
@Test public void testSelectNoName() { SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryWithConstructor(); SqlSession sqlSession = sqlSessionFactory.openSession(); try { StudentConstructorMapper studentConstructorMapper = sqlSession.getMapper(StudentConstructorMapper.class); StudentConstructor testStudent = studentConstructorMapper.selectNoNameById(1); assertEquals(1, testStudent.getConstructors().size()); assertTrue(testStudent.getConstructors().contains(StudentConstructor.Constructor.ID)); assertNull(testStudent.getName()); } finally { sqlSession.close(); } }
Example 17
Source File: TestSelectByExample.java From tk-mybatis with MIT License | 5 votes |
@Test public void testSelectByExample() { SqlSession sqlSession = MybatisHelper.getSqlSession(); try { CountryMapper mapper = sqlSession.getMapper(CountryMapper.class); Example example = new Example(Country.class); example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151); example.or().andLessThan("id", 41); List<Country> countries = mapper.selectByExample(example); //查询总数 Assert.assertEquals(90, countries.size()); } finally { sqlSession.close(); } }
Example 18
Source File: DateTimeTest.java From Mapper with MIT License | 5 votes |
@Test public void testInsert3() { SqlSession sqlSession = getSqlSession(); try { TimeModel3Mapper mapper = sqlSession.getMapper(TimeModel3Mapper.class); TimeModel3 timeModel = new TimeModel3(); timeModel.setId(3); Date now = new Date(); timeModel.setTestDate(now); timeModel.setTestTime(now); timeModel.setTestDatetime(now); /* insert 日志能明显看到制定 jdbcType 后的区别 DEBUG [main] - ==> Preparing: INSERT INTO test_timestamp ( id,test_date,test_time,test_datetime ) VALUES( ?,?,?,? ) DEBUG [main] - ==> Parameters: 3(Integer), 2018-02-25(Date), 11:50:18(Time), 2018-02-25 11:50:18.263(Timestamp) */ Assert.assertEquals(1, mapper.insert(timeModel)); timeModel = mapper.selectByPrimaryKey(3); //保存后数据库中不存在时间部分 Assert.assertEquals(toDate(now), toDate(timeModel.getTestDate())); Assert.assertEquals(toDate(now) + " 00:00:00", toDatetime(timeModel.getTestDate())); //时间 Assert.assertEquals(toTime(now), toTime(timeModel.getTestTime())); //由于插入数据库时指定的 jdbcType=TIME,所以下面是没有日期部分的 Assert.assertEquals("1970-01-01 " + toTime(now), toDatetime(timeModel.getTestTime())); Assert.assertEquals(toDatetime(now), toDatetime(timeModel.getTestDatetime())); } finally { sqlSession.close(); } }
Example 19
Source File: BindingTest.java From mybaties with Apache License 2.0 | 5 votes |
@Test public void shouldSelectRandom() { SqlSession session = sqlSessionFactory.openSession(); try { BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class); Integer x = mapper.selectRandom(); assertNotNull(x); } finally { session.close(); } }
Example 20
Source File: TypeHandlerTest2.java From Mapper with MIT License | 5 votes |
@Test public void testSelect(){ SqlSession sqlSession = getSqlSession(); try { User2Mapper userMapper = sqlSession.getMapper(User2Mapper.class); List<User2> users = userMapper.selectAll(); Assert.assertNotNull(users); Assert.assertEquals(2, users.size()); Assert.assertEquals("abel533", users.get(0).getName()); Assert.assertEquals("Hebei", users.get(0).getAddress().getProvince()); Assert.assertEquals("Shijiazhuang", users.get(0).getAddress().getCity()); Assert.assertEquals(StateEnum.enabled, users.get(0).getState()); Assert.assertEquals("isea533", users.get(1).getName()); Assert.assertEquals("Hebei/Handan", users.get(1).getAddress().toString()); Assert.assertEquals(StateEnum.disabled, users.get(1).getState()); User2 user = userMapper.selectByPrimaryKey(1); Assert.assertEquals("abel533", user.getName()); Assert.assertEquals("Hebei", user.getAddress().getProvince()); Assert.assertEquals("Shijiazhuang", user.getAddress().getCity()); Assert.assertEquals(StateEnum.enabled, user.getState()); } finally { sqlSession.close(); } }