当前位置: 首页>>代码示例>>Java>>正文


Java SqlSession.getConnection方法代码示例

本文整理汇总了Java中org.apache.ibatis.session.SqlSession.getConnection方法的典型用法代码示例。如果您正苦于以下问题:Java SqlSession.getConnection方法的具体用法?Java SqlSession.getConnection怎么用?Java SqlSession.getConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.ibatis.session.SqlSession的用法示例。


在下文中一共展示了SqlSession.getConnection方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: insert

import org.apache.ibatis.session.SqlSession; //导入方法依赖的package包/类
@Override
public  int  insert(Object... entitys) throws Exception {
	SqlSession session=getSession();
	try {
		SqlRunner sqlRunner = new DefaultSqlRunner(session.getConnection());
		if(ArrayUtils.isNotEmpty(entitys)){
			String tableName=getTableName(entitys[0]);
			List<Object> parameter=getInsertParam(entitys);
			String fieldSql=getInsertFieldSql(entitys[0]);
			String valueSql=getInsertValueSql(parameter.size(),entitys.length);
			String sql=StringUtils.appendAll("insert into ",tableName,fieldSql,"values",valueSql);
			logger.debug(sql);
			int effect=sqlRunner.insert(sql, parameter);
			logger.debug("插入了"+effect+"行");
			return effect;
		}
	} catch (Exception e) {
		logger.error(ExceptionUtils.getStackTrace(e));
		throw e;
	}finally{
		session.close();
		//sqlRunner.closeConnection();
	}
	return 0;
}
 
开发者ID:endend20000,项目名称:mybatisx,代码行数:26,代码来源:BaseMapperImpl.java

示例2: update

import org.apache.ibatis.session.SqlSession; //导入方法依赖的package包/类
@Override
public <T> int update(T entity, ExprBuilder... exprs) throws Exception {
	SqlSession session=getSession();
	try {
		SqlRunner sqlRunner = new DefaultSqlRunner(session.getConnection());
		List<Object> parameters = new LinkedList<>();
		String tableName = getTableName(entity);
		String fieldSql = getUpdateFieldSql(entity, parameters);
		String whereSql = getUpdateWhereSql(entity, parameters, exprs);
		String sql = StringUtils.appendAll("update " + tableName + fieldSql + whereSql);
		logger.debug(sql);
		int effect = sqlRunner.update(sql, parameters);
		logger.debug("修改了" + effect + "行");
		if (effect > 0) {
			clearCache(entity);
		}
		return effect;

	} catch (Exception e) {
		logger.error(ExceptionUtils.getStackTrace(e));
		throw e;
	} finally {
		session.close();
	}
}
 
开发者ID:endend20000,项目名称:mybatisx,代码行数:26,代码来源:BaseMapperImpl.java

示例3: delete

import org.apache.ibatis.session.SqlSession; //导入方法依赖的package包/类
/**
 * 
 * @param expr
 * @return
 * @throws SQLException 
 */
public <T> int delete(T entity,ExprBuilder... exprs) throws Exception{
	SqlSession session=getSession();
	try {
		SqlRunner sqlRunner = new DefaultSqlRunner(session.getConnection());
		List<Object> parameter = new LinkedList<Object>();
		String tableName=getTableName(entity);
		String whereSql=getWhereSql(entity,parameter,exprs);
		String sql=StringUtils.appendAll("delete from ",tableName+whereSql);
		logger.debug(sql);
		int effect=sqlRunner.delete(sql, parameter);
		logger.debug("删除了"+effect+"行");
		if(effect>0){
			clearCache(entity);
		}
		return effect;
	} catch (Exception e) {
		logger.error(ExceptionUtils.getStackTrace(e));
		throw e;
	} finally {
		session.close();
	}
}
 
开发者ID:endend20000,项目名称:mybatisx,代码行数:29,代码来源:BaseMapperImpl.java

示例4: setUp

import org.apache.ibatis.session.SqlSession; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
    SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession();
    Connection conn = session.getConnection();
    Reader reader = Resources.getResourceAsReader("com/baomidou/mybatisplus/test/plugins/paginationInterceptor/CreateDB.sql");
    ScriptRunner runner = new ScriptRunner(conn);
    runner.setLogWriter(null);
    runner.runScript(reader);
    reader.close();
    session.close();
    // 随机当前页和分页大小
    size = RandomUtils.nextInt(1, 50);
    current = RandomUtils.nextInt(1, 200 / size);
    System.err.println("当前页为:" + current + " 分页大小为" + size);
}
 
开发者ID:Caratacus,项目名称:mybatis-plus-mini,代码行数:16,代码来源:PaginationInterceptorTest.java

示例5: insertFillId

import org.apache.ibatis.session.SqlSession; //导入方法依赖的package包/类
@Override
public  int  insertFillId(Object... entitys) throws Exception {
	SqlSession session=getSession();
	try {
	if(ArrayUtils.isNotEmpty(entitys)){
		int effect=0;
		for(Object entity:entitys){
			    SqlRunner sqlRunner = new DefaultSqlRunner(session.getConnection());
				String tableName=getTableName(entity);
				List<Object> parameter=getInsertParam(entity);
				String fieldSql=getInsertFieldSql(entitys[0]);
				String valueSql=getInsertValueSql(parameter.size(),1);
				String sql=StringUtils.appendAll("insert into ",tableName,fieldSql,"values",valueSql);
				logger.debug(sql);
				int id=sqlRunner.insertReturnId(sql, parameter);
				if(id>0){
					EntityUtils.setId(entity, id);
					effect++;
				}
			}
		logger.debug("插入了"+effect+"行");
		return effect;
		}
	} catch (Exception e) {
		logger.error(ExceptionUtils.getStackTrace(e));
		throw e;
	}finally{
		session.close();
	}
	return 0;
}
 
开发者ID:endend20000,项目名称:mybatisx,代码行数:32,代码来源:BaseMapperImpl.java

示例6: selectList

import org.apache.ibatis.session.SqlSession; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public <T> ResultEntity<T> selectList(T entity, ExprBuilder... exprs)  throws Exception{
	ResultEntity<T> pageResult=useCache(entity,exprs);
	String orderSql=getOrderSql(exprs);
	if(orderSql==null){//当有排序时不使用缓存
		if(pageResult!=null){
			return pageResult;
		}
	}
	SqlSession session=getSession();
	try {
		SqlRunner sqlRunner = new DefaultSqlRunner(session.getConnection());
		List<Object> parameters=new LinkedList<>();
		String tableName=getTableName(entity);
		String joinSql=getJoinSql(exprs);
		String whereSql=getWhereSql(entity,parameters,exprs);
		String fieldSql=getSelectField(entity);
		String limitSql=getLimitSql(exprs);			
		String sql=StringUtils.appendAll("select SQL_CALC_FOUND_ROWS ",fieldSql," from "+tableName+" "+joinSql+" "+whereSql+" "+orderSql+limitSql);
		logger.debug(sql);
		boolean needInsertCache=insertCache(entity,exprs);
		return (ResultEntity<T>)sqlRunner.selectList(sql, parameters,entity.getClass(),needInsertCache) ;
	} catch (Exception e) {
		logger.error(ExceptionUtils.getStackTrace(e));
		throw e;
	} finally {
		session.close();
	}
}
 
开发者ID:endend20000,项目名称:mybatisx,代码行数:31,代码来源:BaseMapperImpl.java

示例7: afterStart

import org.apache.ibatis.session.SqlSession; //导入方法依赖的package包/类
@Override
public void afterStart() {
    /*
    测试的数据库(启动后创建)
     */
    Reader config = null;
    Reader sql = null;
    SqlSession sqlSession = null;
    try {
        config = Resources.getResourceAsReader("mybatis-config.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(config);
        sqlSession = sessionFactory.openSession(true);

        Connection connection = sqlSession.getConnection();
        ScriptRunner runner = new ScriptRunner(connection);
        runner.setAutoCommit(true);

        sql = Resources.getResourceAsReader("easycontroller.sql");
        runner.runScript(sql);

        runner.closeConnection();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(sqlSession);
        IOUtils.closeQuietly(sql);
        IOUtils.closeQuietly(config);
    }
}
 
开发者ID:Xlongshu,项目名称:EasyController,代码行数:30,代码来源:DemoConfig.java

示例8: ConnectionWrap

import org.apache.ibatis.session.SqlSession; //导入方法依赖的package包/类
public ConnectionWrap(SqlSession sqlSession)
{
    this.sqlSession = sqlSession;
    connection = sqlSession.getConnection();
}
 
开发者ID:gzxishan,项目名称:OftenPorter,代码行数:6,代码来源:ConnectionWrap.java

示例9: doTest

import org.apache.ibatis.session.SqlSession; //导入方法依赖的package包/类
@Test
public void doTest() {
	HikariDataSource dataSource = new HikariDataSource();// DataSource

	// H2 is a memory database
	dataSource.setDriverClassName("org.h2.Driver");
	dataSource.setJdbcUrl("jdbc:h2:mem:DBName;MODE=MYSQL;DB_CLOSE_DELAY=-1;TRACE_LEVEL_SYSTEM_OUT=0");
	dataSource.setUsername("sa");
	dataSource.setPassword("");

	// MySQL
	// dataSource.setDriverClassName("com.mysql.jdbc.Driver");
	// dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?rewriteBatchedStatements=true&useSSL=false");
	// dataSource.setUsername("root");
	// dataSource.setPassword("root888");

	// MS-SqlServer
	// dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	// dataSource.setJdbcUrl("jdbc:sqlserver://localhost:1433;databaseName=test");
	// dataSource.setUsername("sa");
	// dataSource.setPassword("root888");

	// ORACLE
	// dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
	// dataSource.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521:XE");
	// dataSource.setUsername("root");
	// dataSource.setPassword("root888");

	SqlBoxContext ctx = new SqlBoxContext(dataSource);
	SqlBoxContext.setDefaultContext(ctx);
	ctx.setAllowShowSQL(true);
	String[] ddlArray = ctx.toDropAndCreateDDL(User.class);
	for (String ddl : ddlArray)
		ctx.quiteExecute(ddl);
	for (int i = 1; i <= 100; i++)
		new User().put("firstName", "Foo" + i, "lastName", "Bar" + i, "age", i).insert();

	TransactionFactory transactionFactory = new JdbcTransactionFactory();
	Environment environment = new Environment("demo", transactionFactory, dataSource);
	Configuration configuration = new Configuration(environment);
	configuration.addMapper(UserMapper.class);
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
	configuration.addInterceptor(new JDialectsPlugin());

	SqlSession session = null;
	try {
		session = sqlSessionFactory.openSession();
		Connection conn = session.getConnection();
		Assert.assertEquals(100, ctx.nQueryForLongValue(conn, "select count(*) from users"));

		List<Map<String, Object>> users;
		try {
			paginInfo.set(new Object[] { ctx.getDialect(), 3, 10 });
			users = session.getMapper(UserMapper.class).getOlderThan(50);
		} finally {
			paginInfo.remove();
		}
		Assert.assertEquals(10, users.size());
		for (Map<String, Object> map : users)
			System.out.println("UserName=" + map.get("USERNAME") + ", age=" + map.get("AGE"));
	} finally {
		session.close();
	}
	dataSource.close();
}
 
开发者ID:drinkjava2,项目名称:jDialects,代码行数:66,代码来源:Demo.java


注:本文中的org.apache.ibatis.session.SqlSession.getConnection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。