當前位置: 首頁>>代碼示例>>Java>>正文


Java SqlSession類代碼示例

本文整理匯總了Java中org.apache.ibatis.session.SqlSession的典型用法代碼示例。如果您正苦於以下問題:Java SqlSession類的具體用法?Java SqlSession怎麽用?Java SqlSession使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


SqlSession類屬於org.apache.ibatis.session包,在下文中一共展示了SqlSession類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: deleteUserGroupById

import org.apache.ibatis.session.SqlSession; //導入依賴的package包/類
public static boolean deleteUserGroupById(String userGroupId) {
	SqlSession sqlSession = null;
	try {
		sqlSession = MybatisManager.getSqlSession();
		UserGroupMapper userGroupMapper = sqlSession.getMapper(UserGroupMapper.class);
		int result = userGroupMapper.deleteByPrimaryKey(userGroupId);
		if (result == 0) {
			MybatisManager.log.warn("刪除用戶組異常");
			return false;
		}
		sqlSession.commit();
	} catch (Exception e) {
		if (sqlSession != null) {
			sqlSession.rollback();
		}
		MybatisManager.log.error("刪除用戶組異常", e);
		return false;
	} finally {
		if (sqlSession != null) {
			sqlSession.close();
		}
	}
	return true;
}
 
開發者ID:dianbaer,項目名稱:startpoint,代碼行數:25,代碼來源:UserGroupAction.java

示例2: testSelectOneByExample

import org.apache.ibatis.session.SqlSession; //導入依賴的package包/類
/**
 * 測試 selectOneByExample
 */
@Test
public void testSelectOneByExample() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/SelectOneByExamplePlugin/mybatis-generator.xml");
    tool.generate(new AbstractShellCallback() {
        @Override
        public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception {
            ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));

            ObjectUtil TbExample = new ObjectUtil(loader, packagz + ".TbExample");
            ObjectUtil criteria = new ObjectUtil(TbExample.invoke("createCriteria"));
            criteria.invoke("andIdEqualTo", 1l);

            // sql
            String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectOneByExample", TbExample.getObject());
            Assert.assertEquals(sql, "select id, field1, field2 from tb WHERE (  id = '1' )  limit 1");
            Object result = tbMapper.invoke("selectOneByExample", TbExample.getObject());
            ObjectUtil Tb = new ObjectUtil(result);
            Assert.assertEquals(Tb.get("id"), 1l);
            Assert.assertEquals(Tb.get("field1"), "fd1");
            Assert.assertNull(Tb.get("field2"));
        }
    });
}
 
開發者ID:itfsw,項目名稱:mybatis-generator-plugin,代碼行數:27,代碼來源:SelectOneByExamplePluginTest.java

示例3: getChannel

import org.apache.ibatis.session.SqlSession; //導入依賴的package包/類
@Override
public Channel getChannel(int id) {

    try (SqlSession sqlSession = dbManager.getSqlSession()) {

        ChannelMapper mapper = sqlSession.getMapper(ChannelMapper.class);
        ChannelEntity channelEntity = mapper.selectByPrimaryKey(id);
        if (channelEntity == null) {

            return null;
        }

        Channel channel = DozerMapperUtil.map(channelEntity, Channel.class);
        channel.setLink("/channel/".concat(channel.getSlug()));
        channel.setColor("#".concat(Integer.toHexString(channel.getMainColor())));

        return channel;
    }
}
 
開發者ID:thundernet8,項目名稱:Elune,代碼行數:20,代碼來源:ChannelServiceImpl.java

示例4: testDelete

import org.apache.ibatis.session.SqlSession; //導入依賴的package包/類
/**
     * 主要測試刪除
     */
//    @Test
    public void testDelete() {
        SqlSession sqlSession = MybatisHelper.getSqlSession();
        try {
            UserInfoMapMapper mapper = sqlSession.getMapper(UserInfoMapMapper.class);
            //查詢總數
            Assert.assertEquals(5, mapper.selectCount(new UserInfoMap()));
            //查詢100
            UserInfoMap userInfoMap = mapper.selectByPrimaryKey(1);

            //根據主鍵刪除
            Assert.assertEquals(1, mapper.deleteByPrimaryKey(1));

            //查詢總數
            Assert.assertEquals(4, mapper.selectCount(new UserInfoMap()));
            //插入
            Assert.assertEquals(1, mapper.insert(userInfoMap));
        } finally {
            sqlSession.close();
        }
    }
 
開發者ID:godlike110,項目名稱:tk-mybatis,代碼行數:25,代碼來源:TestMap.java

示例5: getNewProperty

import org.apache.ibatis.session.SqlSession; //導入依賴的package包/類
@Override
    public PropertyBean getNewProperty() {
        SqlSession sqlSession = MybatisSqlSession.getSqlSession();
        PropertyBean propertyBean = new PropertyBean();
        try {
            PropertyDao propertyDao = sqlSession.getMapper(PropertyDao.class);
            propertyBean = propertyDao.getNewProperty();
        } catch (Exception e) {
//            e.printStackTrace();
//            logger.error(e.getStackTrace());
        } finally {
            sqlSession.close();
        }

        return propertyBean;
    }
 
開發者ID:wanghan0501,項目名稱:WiFiProbeAnalysis,代碼行數:17,代碼來源:PropertyDaoImpl.java

示例6: testUpdateByPrimaryKey

import org.apache.ibatis.session.SqlSession; //導入依賴的package包/類
/**
     * 根據主鍵全更新
     */
//    @Test
    public void testUpdateByPrimaryKey() {
        SqlSession sqlSession = MybatisHelper.getSqlSession();
        try {
            UserInfoMapMapper mapper = sqlSession.getMapper(UserInfoMapMapper.class);
            UserInfoMap userInfoMap = mapper.selectByPrimaryKey(2);
            Assert.assertNotNull(userInfoMap);
            userInfoMap.setUserType(null);
            userInfoMap.setRealName("liuzh");
            //不會更新user_type
            Assert.assertEquals(1, mapper.updateByPrimaryKey(userInfoMap));

            userInfoMap = mapper.selectByPrimaryKey(userInfoMap);
            Assert.assertNull(userInfoMap.getUserType());
            Assert.assertEquals("liuzh",userInfoMap.getRealName());
        } finally {
            sqlSession.close();
        }
    }
 
開發者ID:godlike110,項目名稱:tk-mybatis,代碼行數:23,代碼來源:TestMap.java

示例7: testDynamicSelectNotFoundKeyProperties

import org.apache.ibatis.session.SqlSession; //導入依賴的package包/類
/**
 * 繼承類可以使用,但多出來的屬性無效
 */
@Test
public void testDynamicSelectNotFoundKeyProperties() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        //根據主鍵刪除
        Assert.assertEquals(183, mapper.select(new Key()).size());

        Key key = new Key();
        key.setCountrycode("CN");
        key.setCountrytel("+86");
        Assert.assertEquals(1, mapper.select(key).size());
    } finally {
        sqlSession.close();
    }
}
 
開發者ID:godlike110,項目名稱:tk-mybatis,代碼行數:20,代碼來源:TestSelect.java

示例8: testDeleteByExample2

import org.apache.ibatis.session.SqlSession; //導入依賴的package包/類
@Test
public void testDeleteByExample2() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
        Example example = new Example(Country.class);
        example.createCriteria().andLike("countryname", "A%");
        example.or().andGreaterThan("id", 100);
        example.setDistinct(true);
        int count = mapper.deleteByExample(example);
        //查詢總數
        Assert.assertEquals(true, count > 83);
    } finally {
        sqlSession.rollback();
        sqlSession.close();
    }
}
 
開發者ID:Yanweichen,項目名稱:MybatisGeneatorUtil,代碼行數:18,代碼來源:TestDeleteByExample.java

示例9: getRelease

import org.apache.ibatis.session.SqlSession; //導入依賴的package包/類
private CMSEvent getRelease(CMSEventRecord record) {
    CMSEvent event;

    try (SqlSession session = sqlsf.openSession()) {
        DJMapper djMapper = session.getMapper(DJMapper.class);
        CmsRelease release = djMapper.getReleaseById(record.getSourcePk());
        if (release != null) {
            long rfcCount = djMapper.countCiRfcByReleaseId(release.getReleaseId());
            release.setCiRfcCount(rfcCount);
        }
        event = new CMSEvent();
        event.setEventId(record.getEventId());
        event.addHeaders("source", "release");
        event.addHeaders("clazzName", "Release");
        event.addHeaders("action", record.getEventType());
        event.addHeaders("sourceId", String.valueOf(record.getSourcePk()));
        event.setPayload(release);
    }
    return event;
}
 
開發者ID:oneops,項目名稱:oneops,代碼行數:21,代碼來源:ControllerEventReader.java

示例10: testOtherMethods

import org.apache.ibatis.session.SqlSession; //導入依賴的package包/類
/**
 * 測試關聯生成的方法和常量
 */
@Test
public void testOtherMethods() throws IOException, XMLParserException, InvalidConfigurationException, InterruptedException, SQLException {
    MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LogicalDeletePlugin/mybatis-generator.xml");
    tool.generate(new AbstractShellCallback() {
        @Override
        public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception{
            ObjectUtil tbMapper = new ObjectUtil(sqlSession.getMapper(loader.loadClass(packagz + ".TbMapper")));

            ObjectUtil tbExample = new ObjectUtil(loader, packagz + ".TbExample");
            ObjectUtil criteria = new ObjectUtil(tbExample.invoke("createCriteria"));
            criteria.invoke("andDeleted", true);
            criteria.invoke("andIdEqualTo", 3l);


            // 驗證sql
            String sql = SqlHelper.getFormatMapperSql(tbMapper.getObject(), "selectByExample", tbExample.getObject());
            Assert.assertEquals(sql, "select id, del_flag, and_logical_deleted, ts_1, ts_3, ts_4 from tb WHERE (  del_flag = '1' and id = '3' )");
            // 驗證執行
            Object result = tbMapper.invoke("selectByExample", tbExample.getObject());
            Assert.assertEquals(((List)result).size(), 1);
        }
    });
}
 
開發者ID:itfsw,項目名稱:mybatis-generator-plugin,代碼行數:27,代碼來源:LogicalDeletePluginTest.java

示例11: testDynamicDelete

import org.apache.ibatis.session.SqlSession; //導入依賴的package包/類
/**
 * 主要測試刪除
 */
@Test
public void testDynamicDelete() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        UserLoginMapper mapper = sqlSession.getMapper(UserLoginMapper.class);
        //查詢總數
        Assert.assertEquals(10, mapper.selectCount(new UserLogin()));
        UserLogin userLogin = new UserLogin();
        userLogin.setUsername("test1");
        List<UserLogin> userLoginList = mapper.select(userLogin);
        //批量刪除
        Assert.assertEquals(userLoginList.size(), mapper.delete(userLogin));
        //循環插入
        for (int i = 0; i < userLoginList.size(); i++) {
            Assert.assertEquals(1, mapper.insert(userLoginList.get(i)));
            Assert.assertEquals(i + 1, (int) userLoginList.get(i).getLogid());
        }
        //查詢總數
        Assert.assertEquals(10, mapper.selectCount(new UserLogin()));
    } finally {
        sqlSession.close();
    }
}
 
開發者ID:Yanweichen,項目名稱:MybatisGeneatorUtil,代碼行數:27,代碼來源:TestDelete.java

示例12: testDynamicSelect

import org.apache.ibatis.session.SqlSession; //導入依賴的package包/類
/**
 * 根據查詢條件進行查詢
 */
@Test
public void testDynamicSelect() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        CountryTMapper mapper = sqlSession.getMapper(CountryTMapper.class);
        CountryT country = new CountryT();
        country.setId(174);
        country.setCountrycode("US");
        List<CountryT> countryList = mapper.select(country);

        Assert.assertEquals(1, countryList.size());
        Assert.assertEquals(true, countryList.get(0).getId() == 174);
        Assert.assertNotNull(countryList.get(0).getCountryname());
        Assert.assertNull(countryList.get(0).getCountrycode());
    } finally {
        sqlSession.close();
    }
}
 
開發者ID:godlike110,項目名稱:tk-mybatis,代碼行數:22,代碼來源:TestTransient.java

示例13: main

import org.apache.ibatis.session.SqlSession; //導入依賴的package包/類
/**
 * 循環標簽 測試
 */
public static void main(String[] args) {

    // 加載配置文件
    InputStream in = CircularLabelsTest.class.getClassLoader().getResourceAsStream("mysql-config.xml");
    MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder();
    SqlSessionFactory sessionFactory = mf.build(in);
    SqlSession session = sessionFactory.openSession();
    UserMapper userMapper = session.getMapper(UserMapper.class);
    Page<User> page = new Page<>(1, 6);
    List<User> users = userMapper.forSelect(page, Arrays.asList("1", "2", "3"));
    System.out.println(users.toString());
    System.out.println(page);
    User user = new User();
    user.setId(1L);
    User users1 = userMapper.selectOne(user);
    System.out.println(users1);
    TestMapper mapper = session.getMapper(TestMapper.class);
    Test test = new Test();
    test.setCreateTime(new Date());
    test.setType("11111");
    mapper.insert(test);
    session.rollback();
}
 
開發者ID:Caratacus,項目名稱:mybatis-plus-mini,代碼行數:27,代碼來源:CircularLabelsTest.java

示例14: main

import org.apache.ibatis.session.SqlSession; //導入依賴的package包/類
/**
  * <p>
  * 事務測試
  * </p>
  */
 public static void main(String[] args) {
     /*
      * 加載配置文件
*/
     InputStream in = TransactionalTest.class.getClassLoader().getResourceAsStream("mysql-config.xml");
     MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder();
     SqlSessionFactory sessionFactory = mf.build(in);
     SqlSession sqlSession = sessionFactory.openSession();
     /**
      * 插入
      */
     UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
     int rlt = userMapper.insert(new User(IdWorker.getId(), "1", 1, 1));
     System.err.println("--------- insertInjector --------- " + rlt);

     //session.commit();
     sqlSession.rollback();
     sqlSession.close();
 }
 
開發者ID:Caratacus,項目名稱:mybatis-plus-mini,代碼行數:25,代碼來源:TransactionalTest.java

示例15: 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
        Country result = mapper.selectOne(country);

        Assert.assertNull(result);
    } finally {
        sqlSession.close();
    }
}
 
開發者ID:Yanweichen,項目名稱:MybatisGeneatorUtil,代碼行數:19,代碼來源:TestSelectOne.java


注:本文中的org.apache.ibatis.session.SqlSession類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。