我们需要创建与数据表对应的两个pojo类来作为数据表的容器。
package org.seckill.pojo;
import java.util.Date;
public class Seckill {
private long seckillId;
private String name;
private int number;
private Date startTime;
private Date endTime;
private Date createTime;
public long getSeckillId() {
return seckillId;
}
public void setSeckillId(long seckillId) {
this.seckillId = seckillId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
return "Seckill{" +
"seckillId=" + seckillId +
", name='" + name + '\'' +
", number=" + number +
", startTime=" + startTime +
", endTime=" + endTime +
", createTime=" + createTime +
'}';
}
}
package org.seckill.pojo;
import java.util.Date;
public class SuccessKilled {
private long seckillId;
private long userPhone;
private short state;
private Date createTime;
// 多对一
private Seckill seckill;
public long getSeckillId() {
return seckillId;
}
public void setSeckillId(long seckillId) {
this.seckillId = seckillId;
}
public long getUserPhone() {
return userPhone;
}
public void setUserPhone(long userPhone) {
this.userPhone = userPhone;
}
public short getState() {
return state;
}
public void setState(short state) {
this.state = state;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Seckill getSeckill() {
return seckill;
}
public void setSeckill(Seckill seckill) {
this.seckill = seckill;
}
@Override
public String toString() {
return "SuccessKilled{" +
"seckillId=" + seckillId +
", userPhone=" + userPhone +
", state=" + state +
", createTime=" + createTime +
", seckill=" + seckill +
'}';
}
}
值得一提的是,秒杀信息表和商品信息表之间是多对一的关系,即一条商品信息可以对应多次秒杀。所以在“一“类的pojo类中包含了一个“多“类的成员变量,用于存放“多“类。
下面需要设计dao层的接口。每个pojo都要对应一个接口,接口中定义该类需要实现的方法。
package org.seckill.dao;
import org.apache.ibatis.annotations.Param;
import org.seckill.pojo.Seckill;
import java.util.Date;
import java.util.List;
public interface SeckillDao {
// 减库存
int reduceNumber(@Param("seckillId") long seckillId, @Param("killTime") Date killTime);
// 根据id查秒杀对象
Seckill queryById(long seckillId);
// 根据偏移量查询秒杀商品列表
List<Seckill> queryAll(@Param("offset") int offset, @Param("limit") int limit);
}
可以看到商品信息类定义了三个方法。方法列表中的@Param注解的作用:在java中,方法形参的名字不会被保存。比如reduceNumber方法中的两个参数,到了编译器那里就变成了类似于arg0, arg1这样的东西。所以,形参名就无法传到mybatis的配置里面去。@Param注解的意思就是告诉mybatis,arg0就是seckillId,arg1就是killTime,这样就形成了对应,mybatis便可以正常工作。类似的,我们创建successKilled类的dao接口:
package org.seckill.dao;
import org.apache.ibatis.annotations.Param;
import org.seckill.pojo.SuccessKilled;
public interface SuccessKilledDao {
// 插入购买明细,根据联合主键过滤重复
int insertSuccessKilled(@Param("seckillId") long seckillId, @Param("userPhone") long userPhone);
// 根据id查询SuccessKilled并携带秒杀产品对象实体
SuccessKilled queryByIdWithSeckill(@Param("seckillId") long seckillId, @Param("userPhone") long userPhone);
}