NutzCN Logo
问答 使用dao向oracle插入pojo,用返回的pojo中的time作为查询条件再查询查询不到
发布于 2979天前 作者 wukonggg 1522 次浏览 复制 上一个帖子 下一个帖子
标签:

oracle: 11gR2 ojdbc6
测试用例代码

@Test
    public void save() {
        GeneralStatDao dao = IocMaster.getInstance().get(GeneralStatDao.class);

        GeneralStat bean = mockBean();
        log.debug("bean: " + bean);
        log.debug("bean.getTime().getTime(): " + bean.getTime().getTime());

        GeneralStat base = dao.save(StatTableNameConst.STAT_BASE, bean);
        log.debug("base: " + base);
        log.debug("base.getTime().getTime(): " + base.getTime().getTime());
        Assert.assertNotNull(base);
        List<GeneralStat> list = dao.getByAllColums(StatTableNameConst.STAT_BASE, base);
        for (GeneralStat gs : list) {
            log.debug("gs = " + gs);
            log.debug("gs.getTime().getTime() = " + gs.getTime().getTime());
        }
    }

    private GeneralStat mockBean() {
        GeneralStat gs = new GeneralStat();
        gs.setCorpId("0300");
        gs.setKpiId(StatKpiConst.MONITORING_COVERAGE);
        gs.setDimension1(StatDataConst.DICT_TYPE_38_NETDEVICE_ROUTER);
        gs.setDimension2("-1");
        gs.setDimension3("-1");
        gs.setDimension4("-1");
        gs.setTime(new java.sql.Date(new Date().getTime())); //这是没有标准化的时间,为测试数据。真实数据应该是标准化的
        gs.setValue("88");
        gs.setFlag(0);

        return gs;
    }

DAO代码

public GeneralStat save(String tableName, GeneralStat gs) {
        try {
            TableName.set(tableName);
            return dao.insert(gs);
        } finally {
            TableName.clear();
        }
    }

    public List<GeneralStat> getByAllColums(String tableName, final GeneralStat gs) {
        Condition cond = Cnd.where("corporationid", "=", gs.getCorpId()).and("kpiid", "=", gs.getKpiId());

        try {
            TableName.set(tableName);
            return dao.query(GeneralStat.class, cond);
        } finally {
            TableName.clear();
        }
    }

PO代码

@Table("${stat_table_name}")
public class GeneralStat {
    @Column("CORPORATIONID")
    private String corpId;

    @Column("KPIID")
    private String kpiId;

    @Column("TIME")
    private java.sql.Date time;
    .....

建表语句

-- Create table
create table STAT_BASE
(
  corporationid VARCHAR2(10) not null,
  kpiid         VARCHAR2(5) not null,
  time          DATE not null,
  dimension1    VARCHAR2(10) default -1 not null,
  dimension2    VARCHAR2(10) default -1 not null,
  dimension3    VARCHAR2(10) default -1 not null,
  dimension4    VARCHAR2(10) default -1 not null,
  value         VARCHAR2(20),
  flag          NUMBER(1) default 0
)
8 回复

测试用例返回的结果

Log4j: 19:51:28 INFO  com.alibaba.druid.pool.DruidDataSource [init] - {dataSource-1} inited
Log4j: 19:51:28 DEBUG org.nutz.dao.impl.DaoSupport [invoke] - JDBC Driver --> 11.2.0.4.0
Log4j: 19:51:28 DEBUG org.nutz.dao.impl.DaoSupport [invoke] - JDBC Name   --> Oracle JDBC driver
Log4j: 19:51:28 DEBUG org.nutz.dao.impl.DaoSupport [invoke] - JDBC URL    --> jdbc:oracle:thin:@10.20.32.106:1521:sid001
Log4j: 19:51:28 DEBUG org.nutz.dao.impl.DaoSupport [setDataSource] - Database info --> ORACLE:[Oracle - Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options]
Log4j: 19:51:28 DEBUG com.nari.nwims.skengine.dao.stat.GeneralStatDaoTest [save] - bean: GeneralStat{corpId='0300', kpiId='10001', time=2016-03-03, dimension1='0101', dimension2='-1', dimension3='-1', dimension4='-1', value='88', flag=0}
Log4j: 19:51:28 DEBUG com.nari.nwims.skengine.dao.stat.GeneralStatDaoTest [save] - bean.getTime().getTime(): 1457005888316
Log4j: 19:51:28 DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor [_runPreparedStatement] - INSERT INTO STAT_BASE(CORPORATIONID,KPIID,TIME,DIMENSION1,DIMENSION2,DIMENSION3,DIMENSION4,VALUE,FLAG) VALUES(?,?,?,?,?,?,?,?,?) 
    |    1 |     2 |          3 |    4 |  5 |  6 |  7 |  8 | 9 |
    |------|-------|------------|------|----|----|----|----|---|
    | 0300 | 10001 | 2016-03-03 | 0101 | -1 | -1 | -1 | 88 | 0 |
  For example:> "INSERT INTO STAT_BASE(CORPORATIONID,KPIID,TIME,DIMENSION1,DIMENSION2,DIMENSION3,DIMENSION4,VALUE,FLAG) VALUES('0300','10001','2016-03-03','0101','-1','-1','-1','88',0) "
Log4j: 19:51:28 DEBUG com.nari.nwims.skengine.dao.stat.GeneralStatDaoTest [save] - base: GeneralStat{corpId='0300', kpiId='10001', time=2016-03-03, dimension1='0101', dimension2='-1', dimension3='-1', dimension4='-1', value='88', flag=0}
Log4j: 19:51:28 DEBUG com.nari.nwims.skengine.dao.stat.GeneralStatDaoTest [save] - base.getTime().getTime(): 1457005888316
Log4j: 19:51:28 DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor [_runSelect] - SELECT * FROM STAT_BASE  WHERE corporationid=? AND kpiid=?
    |    1 |     2 |
    |------|-------|
    | 0300 | 10001 |
  For example:> "SELECT * FROM STAT_BASE  WHERE corporationid='0300' AND kpiid='10001'"
Log4j: 19:51:28 DEBUG com.nari.nwims.skengine.dao.stat.GeneralStatDaoTest [save] - gs = GeneralStat{corpId='0300', kpiId='10001', time=2016-03-03, dimension1='0101', dimension2='-1', dimension3='-1', dimension4='-1', value='88', flag=0}
Log4j: 19:51:28 DEBUG com.nari.nwims.skengine.dao.stat.GeneralStatDaoTest [save] - gs.getTime().getTime() = 1456934400000

Process finished with exit code 0

嗯嗯,数据库字段类型的精度导致的问题

嗯嗯,数据库字段类型的精度导致的问题

@wendal 怎么解决呢?

类型改成datetime

类型改成datetime

@wendal

已经改成datetime。将数据表中的time列,类型换成timestamps,代码中还是util.Date就可以了。

但是Date不是也是带时分秒的吗?

这个问题算是框架的问题还是数据库精读的问题?

之前好像没遇到过这种问题。网上搜索了一下,貌似也没找到类似的情况。请教一下~

数据库精度

添加回复
请先登陆
回到顶部