本文整理匯總了Java中org.apache.ibatis.session.SqlSession.getMapper方法的典型用法代碼示例。如果您正苦於以下問題:Java SqlSession.getMapper方法的具體用法?Java SqlSession.getMapper怎麽用?Java SqlSession.getMapper使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.ibatis.session.SqlSession
的用法示例。
在下文中一共展示了SqlSession.getMapper方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getPropertyById
import org.apache.ibatis.session.SqlSession; //導入方法依賴的package包/類
@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;
}
示例2: testSelectByExample3
import org.apache.ibatis.session.SqlSession; //導入方法依賴的package包/類
@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();
}
}
示例3: testSelectByExample4
import org.apache.ibatis.session.SqlSession; //導入方法依賴的package包/類
@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();
}
}
示例4: testUpdateByPrimaryKeySelective
import org.apache.ibatis.session.SqlSession; //導入方法依賴的package包/類
/**
* 根據主鍵更新非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();
}
}
示例5: testDynamicInsertAll
import org.apache.ibatis.session.SqlSession; //導入方法依賴的package包/類
/**
* 插入空數據,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();
}
}
示例6: getUserById
import org.apache.ibatis.session.SqlSession; //導入方法依賴的package包/類
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;
}
示例7: getOrderRecordById
import org.apache.ibatis.session.SqlSession; //導入方法依賴的package包/類
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;
}
示例8: testINDENTITYInsert
import org.apache.ibatis.session.SqlSession; //導入方法依賴的package包/類
/**
* 插入完整數據
*/
@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();
}
}
示例9: testInsert
import org.apache.ibatis.session.SqlSession; //導入方法依賴的package包/類
/**
* 插入完整數據
*/
//該方法測試需要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();
}
}
示例10: testDynamicSelectZero
import org.apache.ibatis.session.SqlSession; //導入方法依賴的package包/類
/**
* 查詢不存在的結果
*/
@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();
}
}
示例11: testDynamicInsertAll
import org.apache.ibatis.session.SqlSession; //導入方法依賴的package包/類
/**
* 插入空數據,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();
}
}
示例12: getUserByEmail
import org.apache.ibatis.session.SqlSession; //導入方法依賴的package包/類
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();
}
}
}
示例13: testDynamicSelectCount
import org.apache.ibatis.session.SqlSession; //導入方法依賴的package包/類
/**
* 查詢全部
*/
@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();
}
}
示例14: createToken
import org.apache.ibatis.session.SqlSession; //導入方法依賴的package包/類
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();
}
}
}
示例15: testDynamicUpdateByPrimaryKeyAllByNull
import org.apache.ibatis.session.SqlSession; //導入方法依賴的package包/類
/**
* 入參為null時更新全部
*/
@Test
public void testDynamicUpdateByPrimaryKeyAllByNull() {
SqlSession sqlSession = MybatisHelper.getSqlSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
mapper.updateByPrimaryKey(null);
} finally {
sqlSession.close();
}
}