mybatis的mapper实现

在项目里面,我们经常用到mybatis,多年前的hibernate已经慢慢被大家所抛弃。自从iteye被收购以后,好久没有写博客了,今年是要多写一些补一补。今天来聊一下mybatis的mapper实现。

   @Component

public interface ActivityMapper {
/**

成都网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、微信小程序开发、集团成都企业网站定制等服务项目。核心团队均拥有互联网行业多年经验,服务众多知名企业客户;涵盖的客户类型包括:成都宴会酒店设计等众多领域,积累了大量丰富的经验,同时也获得了客户的一致认可!

  • 添加奖品记录
  • @param prizeEntity
  • @return
    */
    int addGift(PrizeEntity prizeEntity);
/**
 * 添加预约记录
 * @param activityPrevueEntity
 * @return
 */
int addActivityPrevue(ActivityPrevueEntity activityPrevueEntity);

/**
 * 根据手机号和活动id获取该用户是否预约过
 * @param phone
 * @param activityId
 * @return
 */
int countPrevueByPhoneAndActivityId(@Param("phone")String phone, @Param("activityId")Integer activityId);

}

这是一个比较常见的mapper类的写法。在xml里面我们需要定义一个和方法名一样的id.




    insert into pumpkin_ius.activity_prize_record(user_id, activity_id, prize_code, create_time)
    values(#{userIdInt}, #{activityIdInt}, #{prizeCodeStr}, now())



    insert into pumpkin_ius.activity_user_enroll(activity_id, user_id, user_phone, user_name, user_email, create_time)
    values(#{activityId}, #{userId}, #{userPhone}, #{userName}, #{userEmail}, now())


在serviceimpl里面,我们通过调用mapper就可以实现xml 的sql执行。

Gift gift = Lottery.lottery(giftList, defaultGift.get(0));
PrizeEntity prizeEntity = setPrizeValue(gift, user.getUserId(), activityId);
// 添加抽奖记录
activityMapper.addGift(prizeEntity);

需要给MyBatis提供Mapper接口和与之匹配的映射文件,就能够让MyBatis按照我们的需求执行到对应的SQL 这里的实现原理就动态代理

public class MapperProxy implements InvocationHandler, Serializable {
private static final long serialVersionUID = -6424540398559729838L;
private final SqlSession sqlSession;
private final Class mapperInterface;
private final Map methodCache;

public MapperProxy(SqlSession sqlSession, Class mapperInterface, Map methodCache) {
    this.sqlSession = sqlSession;
    this.mapperInterface = mapperInterface;
    this.methodCache = methodCache;
}

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if(Object.class.equals(method.getDeclaringClass())) {
        return method.invoke(this, args);
    } else {
        MapperMethod mapperMethod = this.cachedMapperMethod(method);
        return mapperMethod.execute(this.sqlSession, args);
    }
}

private MapperMethod cachedMapperMethod(Method method) {
    MapperMethod mapperMethod = (MapperMethod)this.methodCache.get(method);
    if(mapperMethod == null) {
        mapperMethod = new MapperMethod(this.mapperInterface, method, this.sqlSession.getConfiguration());
        this.methodCache.put(method, mapperMethod);
    }

    return mapperMethod;
}

}

在invoke方法中可以看到,如果我们调用的是Object中的方法,不做任何处理,直接调用,否则执行:
mapperMethod.execute(this.sqlSession, args);

这里是用jdk的动态代理来实现了自动调用。

在开发的时候,我们有时候不写xml ,通过注解的形式直接实现调用,查看例子。

@Component
public interface FeedbackMapper {
int insert(Feedback feedback);

/**
 * 添加用户意见反馈
 * @param userFeedback
 */
@Insert("  INSERT INTO `pumpkin_ius`.`user_feedback`(`phone`, `device`, `pc_device`, `pc_os`, `pc_channel`, `pc_ip`, `pc_platform`, `pc_version`, `pc_browser_name`, `pc_browser_version`, `play_feedback`, `program_feedback`, `other_feedback`) VALUES (#{phone},#{device},#{pc_device},#{pc_os},#{pc_channel},#{pc_ip},#{pc_platform},#{pc_version},#{pc_browser_name},#{pc_browser_version},#{play_feedback},#{program_feedback},#{other_feedback});")
void addUserFeedback(UserFeedback userFeedback);

}

当然,在SQL复杂的时候,还是写xml比较方便维护,看起来不那么难受。


当前文章:mybatis的mapper实现
标题URL:http://pwwzsj.com/article/ijihsg.html