Java Code Examples for org.apache.ibatis.session.SqlSession.getMapper()

The following are Jave code examples for showing how to use getMapper() of the org.apache.ibatis.session.SqlSession class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: WiFiProbeAnalysis   File: PropertyDaoImpl.java   View Source Code Vote up 6 votes
@Override
    public PropertyBean getPropertyById(int id) {
        SqlSession sqlSession = MybatisSqlSession.getSqlSession();
        PropertyBean propertyBean = new PropertyBean();

        try {
            PropertyDao propertyDao = sqlSession.getMapper(PropertyDao.class);
            propertyBean = propertyDao.getPropertyById(id);
        } catch (Exception e) {
//            e.printStackTrace();
//            logger.error(e.getStackTrace());
        } finally {
            sqlSession.close();
        }

        return propertyBean;
    }
 
Example 2
Project: tk-mybatis   File: TestSelectByExample.java   View Source Code Vote up 6 votes
@Test
public void testSelectByExample3() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        CountryExample example = new CountryExample();
        example.createCriteria().andCountrynameLike("A%");
        example.or().andIdGreaterThan(100);
        example.setDistinct(true);
        List<Country> countries = mapper.selectByExample(example);
        //查询总数
        Assert.assertEquals(true, countries.size() > 83);
    } finally {
        sqlSession.close();
    }
}
 
Example 3
Project: MybatisGeneatorUtil   File: TestSelectByExample.java   View Source Code Vote up 6 votes
@Test
public void testSelectByExample4() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        Country ct = new Country();
        ct.setCountryname("China");

        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Example example = new Example(Country.class);
        example.createCriteria().andGreaterThan("id", 20).andEqualTo(ct);
        List<Country> countries = mapper.selectByExample(example);
        //查询总数
        System.out.println(countries.get(0).toString());
        Assert.assertEquals(1, countries.size());
    } finally {
        sqlSession.close();
    }
}
 
Example 4
Project: MybatisGeneatorUtil   File: TestBasic.java   View Source Code Vote up 6 votes
/**
 * 根据主键更新非null
 */
@Test
public void testUpdateByPrimaryKeySelective() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
        UserInfo userInfo = mapper.selectByPrimaryKey(1);
        Assert.assertNotNull(userInfo);
        userInfo.setUsertype(null);
        userInfo.setEmail("[email protected]");
        //不会更新username
        Assert.assertEquals(1, mapper.updateByPrimaryKeySelective(userInfo));

        userInfo = mapper.selectByPrimaryKey(1);
        Assert.assertEquals("1", userInfo.getUsertype());
        Assert.assertEquals("[email protected]", userInfo.getEmail());
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}
 
Example 5
Project: MybatisGeneatorUtil   File: TestInsert.java   View Source Code Vote up 6 votes
/**
 * 插入空数据,id不能为null,会报错
 */
@Test
public void testDynamicInsertAll() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        Country2Mapper mapper = sqlSession.getMapper(Country2Mapper.class);
        Country2 country2 = new Country2();
        country2.setCountrycode("CN");
        Assert.assertEquals(1, mapper.insert(country2));

        country2 = mapper.select(country2).get(0);
        Assert.assertNotNull(country2);

        Assert.assertEquals(1, mapper.deleteByPrimaryKey(country2.getId()));

    } finally {
        sqlSession.close();
    }
}
 
Example 6
Project: startpoint   File: UserAction.java   View Source Code Vote up 6 votes
public static User getUserById(String userId) {
	if (StringUtil.stringIsNull(userId)) {
		return null;
	}
	SqlSession sqlSession = null;
	User user;
	try {
		sqlSession = MybatisManager.getSqlSession();
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		user = userMapper.selectByPrimaryKey(userId);
		if (user == null) {
			MybatisManager.log.warn("通过userId:" + userId + "获取用户为空");
		}
	} catch (Exception e) {
		if (sqlSession != null) {
			sqlSession.rollback();
		}
		MybatisManager.log.error("获取用户异常", e);
		return null;
	} finally {
		if (sqlSession != null) {
			sqlSession.close();
		}
	}
	return user;
}
 
Example 7
Project: epay   File: OrderRecordAction.java   View Source Code Vote up 6 votes
public static OrderRecordExt getOrderRecordById(String orderRecordId) {
	if (StringUtil.stringIsNull(orderRecordId)) {
		return null;
	}
	SqlSession sqlSession = null;
	OrderRecordExt orderRecord;
	try {
		sqlSession = MybatisManager.getSqlSession();
		OrderRecordMapperExt orderRecordMapper = sqlSession.getMapper(OrderRecordMapperExt.class);
		orderRecord = orderRecordMapper.selectByPrimaryKey(orderRecordId);
		if (orderRecord == null) {
			MybatisManager.log.warn("通过orderRecordId:" + orderRecordId + "获取支付记录为空");
		}
	} catch (Exception e) {
		if (sqlSession != null) {
			sqlSession.rollback();
		}
		MybatisManager.log.error("获取支付记录异常", e);
		return null;
	} finally {
		if (sqlSession != null) {
			sqlSession.close();
		}
	}
	return orderRecord;
}
 
Example 8
Project: MybatisGeneatorUtil   File: TestIndentity.java   View Source Code Vote up 6 votes
/**
 * 插入完整数据
 */
@Test
public void testINDENTITYInsert() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryIMapper mapper = sqlSession.getMapper(CountryIMapper.class);
        CountryI country = new CountryI();
        country.setCountrycode("CN");
        Assert.assertEquals(1, mapper.insert(country));
        //ID会回写
        Assert.assertNotNull(country.getId());
        //删除插入的数据,以免对其他测试产生影响
        Assert.assertEquals(1, mapper.deleteByPrimaryKey(country.getId()));
    } finally {
        sqlSession.close();
    }
}
 
Example 9
Project: tk-mybatis   File: TestMysql.java   View Source Code Vote up 6 votes
/**
 * 插入完整数据
 */
//该方法测试需要mysql或者h2数据库,所以这里注释掉
//@Test
public void testInsert() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Country country = new Country();
        country.setCountrycode("CN");
        country.setCountryname("天朝");
        int count = mapper.insertUseGeneratedKeys(country);
        Assert.assertEquals(1, count);
        Assert.assertNotNull(country.getId());
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}
 
Example 10
Project: MybatisGeneatorUtil   File: TestSelect.java   View Source Code Vote up 6 votes
/**
 * 查询不存在的结果
 */
@Test
public void testDynamicSelectZero() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Country country = new Country();
        country.setCountrycode("CN");
        country.setCountryname("天朝");//实际上是 China
        List<Country> countryList = mapper.select(country);

        Assert.assertEquals(0, countryList.size());
    } finally {
        sqlSession.close();
    }
}
 
Example 11
Project: tk-mybatis   File: TestInsertSelective.java   View Source Code Vote up 6 votes
/**
 * 插入空数据,id不能为null,会报错
 */
@Test
public void testDynamicInsertAll() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        Country2Mapper mapper = sqlSession.getMapper(Country2Mapper.class);
        Country2 country2 = new Country2();
        country2.setCountrycode("CN");
        Assert.assertEquals(1, mapper.insertSelective(country2));

        country2 = mapper.select(country2).get(0);
        Assert.assertNotNull(country2);

        Assert.assertEquals(1, mapper.deleteByPrimaryKey(country2.getId()));
    } finally {
        sqlSession.close();
    }
}
 
Example 12
Project: startpoint   File: UserAction.java   View Source Code Vote up 5 votes
public static User getUserByEmail(String userEmail) {
	if (StringUtil.stringIsNull(userEmail)) {
		return null;
	}
	SqlSession sqlSession = null;
	try {
		sqlSession = MybatisManager.getSqlSession();
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		UserCriteria userCriteria = new UserCriteria();
		UserCriteria.Criteria criteria = userCriteria.createCriteria();
		criteria.andUserEmailEqualTo(userEmail);
		List<User> userList = userMapper.selectByExample(userCriteria);
		if (userList == null || userList.size() == 0) {
			MybatisManager.log.warn("通过userEmail:" + userEmail + "获取用户为空");
			return null;
		}
		return userList.get(0);
	} catch (Exception e) {
		if (sqlSession != null) {
			sqlSession.rollback();
		}
		MybatisManager.log.error("获取用户异常", e);
		return null;
	} finally {
		if (sqlSession != null) {
			sqlSession.close();
		}
	}
}
 
Example 13
Project: tk-mybatis   File: TestSelectCount.java   View Source Code Vote up 5 votes
/**
 * 查询全部
 */
@Test
public void testDynamicSelectCount() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        //查询总数
        Assert.assertEquals(183, mapper.selectCount(new Country()));
    } finally {
        sqlSession.close();
    }
}
 
Example 14
Project: startpoint   File: TokenAction.java   View Source Code Vote up 5 votes
public static Token createToken(String userId) {
	if (StringUtil.stringIsNull(userId)) {
		return null;
	}
	Token token = new Token();
	Date date = new Date();
	token.setTokenCreateTime(date);
	token.setTokenUpdateTime(date);
	Date expireTime = new Date(date.getTime() + CommonConfigUCenter.TOKEN_EXPIRE_TIME);
	token.setTokenExpireTime(expireTime);
	token.setUserId(userId);
	token.setTokenId(IdUtil.getUuid());

	SqlSession sqlSession = null;
	try {
		sqlSession = MybatisManager.getSqlSession();
		TokenMapper tokenMapper = sqlSession.getMapper(TokenMapper.class);
		int result = tokenMapper.insert(token);
		if (result == 0) {
			MybatisManager.log.warn("创建token失败");
			return null;
		}
		sqlSession.commit();
		return token;
	} catch (Exception e) {
		if (sqlSession != null) {
			sqlSession.rollback();
		}
		MybatisManager.log.error("创建token异常", e);
		return null;
	} finally {
		if (sqlSession != null) {
			sqlSession.close();
		}
	}
}
 
Example 15
Project: tk-mybatis   File: TestUpdateByPrimaryKey.java   View Source Code Vote up 5 votes
/**
 * 入参为null时更新全部
 */
@Test
public void testDynamicUpdateByPrimaryKeyAllByNull() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        mapper.updateByPrimaryKey(null);
    } finally {
        sqlSession.close();
    }
}
 
Example 16
Project: tk-mybatis   File: TestSelectCount.java   View Source Code Vote up 5 votes
/**
 * 入参为null时查询全部
 */
@Test
public void testDynamicSelectAllByNull() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        //查询总数
        Assert.assertEquals(183, mapper.selectCount(null));
    } finally {
        sqlSession.close();
    }
}
 
Example 17
Project: tk-mybatis   File: TestSelectCountByExample.java   View Source Code Vote up 5 votes
@Test
public void testSelectCountByExampleForUpdate() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Example example = new Example(Country.class);
        example.setForUpdate(true);
        example.createCriteria().andGreaterThan("id", 100);
        int count = mapper.selectCountByExample(example);
        //查询总数
        Assert.assertEquals(83, count);
    } finally {
        sqlSession.close();
    }
}
 
Example 18
Project: ShiroDemo   File: ShiroDemoRealm.java   View Source Code Vote up 4 votes
private UserCustomMapper getUserCustomMapper(){
	SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
	SqlSession sqlSession = factory.openSession();
	UserCustomMapper mapper = sqlSession.getMapper(UserCustomMapper.class);
	return mapper;
}
 
Example 19
Project: epay   File: OrderRecordAction.java   View Source Code Vote up 4 votes
public static List<OrderRecordExt> getOrderRecordList(String appId, String orderRecordOrderId, String orderRecordCreateTimeGreaterThan, String orderRecordCreateTimeLessThan, int orderRecordPayStatus, int orderRecordStatus, String orderRecordUserId, int orderRecordPayChannel, String orderRecordOrderDetail, int orderRecordNotifyResult, int limitStart, int pageSize) {
	SqlSession sqlSession = null;
	try {
		sqlSession = MybatisManager.getSqlSession();
		OrderRecordMapperExt orderRecordMapper = sqlSession.getMapper(OrderRecordMapperExt.class);
		OrderRecordCriteria orderRecordCriteria = new OrderRecordCriteria();
		OrderRecordCriteria.Criteria criteriaOrderRecord = orderRecordCriteria.createCriteria();
		if (!StringUtil.stringIsNull(appId)) {
			criteriaOrderRecord.andAppIdEqualTo(appId);
		}
		if (!StringUtil.stringIsNull(orderRecordOrderId)) {
			criteriaOrderRecord.andOrderRecordOrderIdEqualTo(orderRecordOrderId);
		}
		if (!StringUtil.stringIsNull(orderRecordCreateTimeGreaterThan)) {
			Date orderRecordCreateTimeGreaterThanDate = TimeUtils.stringToDate(orderRecordCreateTimeGreaterThan);
			if (orderRecordCreateTimeGreaterThanDate != null) {
				criteriaOrderRecord.andOrderRecordCreateTimeGreaterThanOrEqualTo(orderRecordCreateTimeGreaterThanDate);
			}
		}
		if (!StringUtil.stringIsNull(orderRecordCreateTimeLessThan)) {
			Date orderRecordCreateTimeLessThanDate = TimeUtils.stringToDate(orderRecordCreateTimeLessThan);
			if (orderRecordCreateTimeLessThanDate != null) {
				criteriaOrderRecord.andOrderRecordCreateTimeLessThanOrEqualTo(orderRecordCreateTimeLessThanDate);
			}
		}
		if (orderRecordPayStatus == OrderRecordConfig.PAY_STATUS_UNPAID || orderRecordPayStatus == OrderRecordConfig.PAY_STATUS_ALREADY || orderRecordPayStatus == OrderRecordConfig.PAY_STATUS_UNQUALIFIED) {
			criteriaOrderRecord.andOrderRecordPayStatusEqualTo((byte) orderRecordPayStatus);
		}
		if (orderRecordStatus == OrderRecordConfig.STATUS_USABLE || orderRecordStatus == OrderRecordConfig.STATUS_DELETE) {
			criteriaOrderRecord.andOrderRecordStatusEqualTo((byte) orderRecordStatus);
		}
		if (!StringUtil.stringIsNull(orderRecordUserId)) {
			criteriaOrderRecord.andOrderRecordUserIdEqualTo(orderRecordUserId);
		}
		if (orderRecordPayChannel == OrderRecordConfig.PAY_CHANNEL_ALIPAY) {
			criteriaOrderRecord.andOrderRecordPayChannelEqualTo((byte) orderRecordPayChannel);
		}
		if (orderRecordNotifyResult == OrderRecordConfig.NOTIFY_RESULT_YES || orderRecordNotifyResult == OrderRecordConfig.NOTIFY_RESULT_NOT) {
			criteriaOrderRecord.andOrderRecordNotifyResultEqualTo((byte) orderRecordNotifyResult);
		}
		orderRecordCriteria.setOrderByClause("order_record_create_time asc");
		if (pageSize > 0) {
			orderRecordCriteria.setLimitStart(limitStart);
			orderRecordCriteria.setPageSize(pageSize);
		}
		List<OrderRecordExt> orderRecordList = orderRecordMapper.selectByExample(orderRecordCriteria);
		return orderRecordList;
	} catch (Exception e) {
		if (sqlSession != null) {
			sqlSession.rollback();
		}
		MybatisManager.log.error("查询OrderRecordlist失败", e);
		return null;
	} finally {
		if (sqlSession != null) {
			sqlSession.close();
		}
	}
}
 
Example 20
Project: startpoint   File: UserAction.java   View Source Code Vote up 4 votes
public static User createUser(Object... params) {
	User user = (User) params[0];
	String userGroupId = (String) params[1];
	// 不得空判断这棵树是否改变了
	if (userGroupId != null) {
		UserGroup userGroupNew = UserGroupAction.getUserGroupById(userGroupId);
		if (userGroupNew == null) {
			return null;
		}
		String userGroupTopId;
		if (StringUtil.stringIsNull(userGroupNew.getUserGroupTopId())) {
			userGroupTopId = userGroupNew.getUserGroupId();
		} else {
			userGroupTopId = userGroupNew.getUserGroupTopId();
		}
		// 判断锁的树还是这颗树吗?不是就说明树形结构已变化
		if (!userGroupTopId.equals(user.getUserGroupTopId())) {
			return null;
		}
	}
	SqlSession sqlSession = null;
	try {
		sqlSession = MybatisManager.getSqlSession();
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		int result = userMapper.insert(user);
		if (result == 0) {
			MybatisManager.log.warn("创建用户失败");
			return null;
		}
		sqlSession.commit();
		return user;
	} catch (Exception e) {
		if (sqlSession != null) {
			sqlSession.rollback();
		}
		MybatisManager.log.error("创建用户异常", e);
		return null;
	} finally {
		if (sqlSession != null) {
			sqlSession.close();
		}
	}
}