0

table column

  `start_time` TIMESTAMP NOT NULL COMMENT '秒杀结束时间',
  `end_time` TIMESTAMP NOT NULL COMMENT '秒杀结束时间',
  `create_time` TIMESTAMP NOT NULL DEFAULT current_timestamp COMMENT '创建时间',

There are several properties in entity(com.hxy.entity.Seckill)

Entity

private Date   start_time;
private Date   end_time;
private Date   create_time;

Mapper

<select id="queryById" resultType="com.hxy.entity.Seckill" parameterType="long">
    SELECT seckill.seckill_id,seckill.name,seckill.number,seckill.start_time,seckill.end_time,seckill.create_time
    FROM
    seckill.seckill
    WHERE seckill_id=#{seckillId}
</select>

DAO

 Seckill queryById(long seckillid);

Service

 public Exposer exportSecKillUrl(final long seckillid) {
    Seckill seckill = seckillDao.queryById(seckillid);

    logger.info(seckill.toString());
}

Test

    @Test
public void getById() throws Exception {
    long    id      = 1000;
    Seckill seckill = secKillService.getById(id);

    logger.info("seckill={}", seckill);
}

Log

  19:08:34.579 [main] DEBUG o.m.s.t.SpringManagedTransaction -JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@5ef5c734 [wrapping: com.mysql.jdbc.JDBC4Connection@d771cc9]] will not be managed by Spring
  19:08:34.586 [main] DEBUG com.hxy.dao.SeckillDao.queryById -==>  Preparing: SELECT seckill.seckill_id,seckill.name,seckill.number,seckill.start_time,seckill.end_time,seckill.create_time FROM seckill.seckill WHERE seckill_id=? 
  19:08:34.620 [main] DEBUG com.hxy.dao.SeckillDao.queryById -==> Parameters: 1000(Long)
  19:08:34.709 [main] DEBUG com.hxy.dao.SeckillDao.queryById -<==      Total: 1
  19:08:34.713 [main] DEBUG org.mybatis.spring.SqlSessionUtils -Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@48793bef]
  19:08:34.715 [main] INFO  c.hxy.service.impl.SecKilServiceimpl -Seckill{seckillid=1000, name='1000元秒杀ipone6', number=100, start_time=null, end_time=null, create_time=null}

  java.lang.NullPointerException
at com.hxy.service.impl.SecKilServiceimpl.exportSecKillUrl(SecKilServiceimpl.java:126)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)

SQL Directly

  SELECT seckill.seckill_id,seckill.name,seckill.number,seckill.start_time,seckill.end_time,seckill.create_time
  FROM
  seckill.seckill
  WHERE seckill_id=1000;

Result

                               start_time             end_time         create_time
  1000  1000元秒杀ipone6   100 2015-11-01 00:00:00 2015-11-02 00:00:00 2016-06-11 22:05:07
黄旭阳
  • 11
  • 7
  • What is type of these three column `start_time, end_time,create_time` in database? – Blank Jun 14 '16 at 01:55
  • @Reno I can get the column star_time's value directly by SQL ,so I think the exception happens when spring try to set the value to my bean's property by setter. – 黄旭阳 Jun 14 '16 at 06:49
  • When did this exception happen, `mybatis` result map to java object? Or ? Post more stack trace exception log please. – Blank Jun 14 '16 at 06:56
  • this may be useful http://stackoverflow.com/questions/383783/oracle-sql-date-conversion-problem-using-ibatis-via-java-jdbc – Romeo Sheshi Jun 14 '16 at 08:17
  • @Reno the stack trace log – 黄旭阳 Jun 14 '16 at 11:24
  • So, either the DAO is null, or the returned seckill is null, depending on which line is line 126 in SecKilServiceimpl. – JB Nizet Jun 14 '16 at 11:25
  • @JBNizet I think Mybatis failed to setter "starttime" to entity – 黄旭阳 Jun 14 '16 at 13:48
  • I fix my problem by refactor Entity -------------------------------------------- private Date start_time; private Date end_time; private Date create_time; -------------------------------------------- -------------------------------------------- private Date startTime; private Date endTime; private Date createTime; ------------------------------------------- It seems that the framework(mybatis) cannot resolve getter and setter that contains special Character(I failed to use for my code block,so I use ------------------) – 黄旭阳 Jun 14 '16 at 15:53

0 Answers0