ibatis 动态java类_ibatis实现动态sql处理

package base;

import java.io.ByteArrayInputStream;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.ResultSet;

import java.util.Calendar;

import java.util.Date;

import java.util.List;

import java.util.Map;

import javax.annotation.Resource;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.google.common.collect.Lists;

import com.google.common.collect.Maps;

import com.ibatis.common.resources.Resources;

import com.ibatis.sqlmap.engine.builder.xml.SqlMapParser;

import com.ibatis.sqlmap.engine.builder.xml.XmlParserState;

import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;

import com.ibatis.sqlmap.engine.mapping.result.ResultMap;

import com.ibatis.sqlmap.engine.mapping.sql.Sql;

import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;

import com.ibatis.sqlmap.engine.scope.SessionScope;

import com.ibatis.sqlmap.engine.scope.StatementScope;

import com.wtt.unified.schedule.dal.base.dao.AlipayPushScheduleDAOImpl;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = {"classpath:test.xml"})

public class SqlSourceTest {

static Logger log = LoggerFactory.getLogger(SqlSourceTest.class.getName());

@Resource

private AlipayPushScheduleDAOImpl alipayPushScheduleDAO;

@Test

public void test() throws Exception{

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>").append("\r\n");

stringBuilder.append(""-//ibatis.apache.org//DTD SQL Map 2.0//EN\" \"http://ibatis.apache.org/dtd/sql-map-2.dtd\" >").append("\r\n");

stringBuilder.append("").append("\r\n");

stringBuilder.append("").append("\r\n");

//修改掉

stringBuilder.append("select id as id, gmt_create as gmtCreate, gmt_modified as gmtModified, alipay_id as alipayId, phone_no as phoneNo, balance as balance, threshold as threshold, gmt_warn_time as gmtWarnTime, ext as ext, audit_id as auditId").append(" from alipay_push_schedule where 1=1 ")

.append(" = #startTime# ]]> ")

.append(" ")

.append(" ")

.append(" limit #limit#");

stringBuilder.append(" ");

stringBuilder.append("");

String dsName ="";

System.out.println(stringBuilder.toString());

SqlMapClientTemplate template = alipayPushScheduleDAO.getTemplate();

//方案二

// SQLSelectBuilder sqlBuilder = SQLBuilderFactory.createSelectSQLBuilder(JdbcConstants.MYSQL);

// sqlBuilder.select("")

// .from("");

//sqlMapClient

// InputStream inputStream = Resources.getResourceAsStream("AliPayPushScheduleTask_sqlmap.xml");

//

InputStream inputStream =new ByteArrayInputStream(stringBuilder.toString().getBytes());

String sql =null;

XmlParserState state = new XmlParserState();

SqlMapParser parser = new SqlMapParser(state);

parser.parse(inputStream);

MappedStatement mappedStatement = state.getConfig().getDelegate().getMappedStatement("queryList");

Sql stmtSql = mappedStatement.getSql();

Map map = Maps.newHashMap() ;

map.put("limit", 2);

map.put("startTime",getTimesmorning());

ResultMap resultMap = mappedStatement.getResultMap();

SessionScope sessionScope = new SessionScope();

StatementScope requestScope = new StatementScope(sessionScope);

requestScope.setStatement(mappedStatement);

requestScope.setResultMap(resultMap);

sql = stmtSql.getSql(requestScope, map);

ParameterMap parameterMap = stmtSql.getParameterMap(requestScope, map);

requestScope.setParameterMap(parameterMap);

Object[] parameters = parameterMap.getParameterObjectValues(requestScope, map);

java.sql.PreparedStatement ps = null;

ResultSet rs = null;

Connection conn = template.getDataSource().getConnection();

Integer rsType = requestScope.getStatement().getResultSetType();

if(rsType != null) {

ps = conn.prepareStatement(sql, rsType.intValue(), 1007);

} else {

ps =conn.prepareStatement(sql);

}

//设置参数

parameterMap.setParameters(requestScope, ps, parameters);

ps.execute();

rs = ps.getResultSet();

int resultsFetched = 0;

List> list = Lists.newArrayList();

while ( rs.next()) {

Object[] columnValues = resultMap.resolveSubMap(requestScope, rs).getResults(requestScope, rs);

参与评论 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:游动-白 设计师:我叫白小胖 返回首页

打赏作者

灵也有希望

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值