实习修炼之第七天

具体项目的实施

在今天开始了一个简单功能的实现,就是实现下面sql语句使得在后台管理端进行用户的删除,由于项目原因所以没有进行物理删除,而是将相关字段设置成不合理的字符串使得达到删除用户的效果,要实现的具体sql代码如下,并将其操作的相应数据表和操作方法在注释部分显示

//对应ShardingWechatAccountDo类
//deleteWechatAccountByOpenID方法
update account.wechat_account_openid3 set union_id = 'TestDeleteUnionid' , open_id = 'TestDeleteOpenid' where open_id = 'o-Pop0XgFOIyjk5BW0poisy4jMGQ'
update account.wechat_account_openid5 set union_id = 'TestDeleteUnionid' , open_id = 'TestDeleteOpenid' where open_id = 'oC7_TwuMW_ovzMrZ4GJcRvbKPoPI'

//对应ShardingWechatAccountDo类
//deleteWechatAccountByUnionID方法
update account.wechat_account_unionid0 set union_id = 'TestDeleteUnionid' , open_id = 'TestDeleteOpenid' where union_id = 'oV1zfwgjlVvfeS_Kd_tqWGYWS1DE'

//对应WechatAccountDo类
//deleteWechatAccountByUserID方法
update account.account7 set wechat_union_id = 'TestDeleteUnionid' where user_id = 4750351

//deleteWechatAccountByUserID方法
update account.wechat_account_userid7 set union_id = 'TestDeleteUnionid', open_id = 'TestDeleteOpenid'  where user_id = 4750351

//deleteTradeByUserId方法
update account.user_trade_status set weixin_unionid = 'TestDeleteUnionid' where user_id = 4750351

整体业务实现逻辑顺序

Xml文件配置数据库—dao层持久化(有两种方法:利用insert等函数或者利用注释直接添加依赖)—facde层面向接口声明—facde.impl层实现方法----dubbo配置加入提供者方
因为原理基本相同,所以在此后罗列的代码只是对于一个数据表的修改,即语句

update account.wechat_account_unionid0
set union_id = ‘TestDeleteUnionid’ , open_id = ‘TestDeleteOpenid’
where union_id = ‘oV1zfwgjlVvfeS_Kd_tqWGYWS1DE’

的执行

  1. mybatis.xml文件
    在这里xml文件里主要的配置是
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//dal.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="WechatAccountUnionIdDao">

    <sql id="tableName">##wechat_account_unionid##</sql>

    <resultMap id="wechatAccountUnionIdMap" type="com.mine.account.dal.domainobject.ShardingWechatAccountDo">
        <result property="id" column="id"/>
        <result property="unionId" column="union_id"/>
        <result property="userId" column="user_id"/>
        <result property="openId" column="open_id"/>
        <result property="appId" column="app_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="columns">
        union_id,user_id,open_id,app_id,create_time,update_time
    </sql>

    <sql id="allColumns">
        id,<include refid="columns"/>
    </sql>

通过昨天对此xml文件的学习,先在xml文件里添加如下代码

<!--newCode-->
<update id="updateAllIdByUnionId" parameterType="com.zhicong.account.dal.domainobject.ShardingWechatAccountDo">
    update
    <include refid="tableName"></include>
    <set>
        union_id = ‘TestDeleteUnionid‘,
        open_id = ‘TestDeleteOpenid‘,
        updatetime = now()
    </set>
    where union_id= #{openId}
</update>
  1. dao层持久化实现
    具体的updateAllIdByUnionId方法实现代码是
public int updateAllIdByUnionId (String unionId){
        return mdsSqlSessionTemplate.update("WechatAccountUnionIdDao.updateAllIdByUnionId",ShardingRule.getNumbericUniqueKeyRule(unionId),unionId);
    }

其中update()方法是自己这边实现的一个接口方法,MdsSqlSessionTemplate是一个基于DisposableBean , MdsSqlSession , InitializingBean接口的数据类,其中封装了数据库操作函数。MdsSqlSessionTemplate部分代码如下所示

public class MdsSqlSessionTemplate implements DisposableBean , MdsSqlSession , InitializingBean {
    @Setter
    private MdsSqlsessionFactoryBean sqlSessionFactory;
    private Map<String,SqlSession> sqlSessions = new HashMap<>();
    private ShardStrategy shardStrategy;

其实,除了利用MdsSqlSessionTemplate外还可以直接利用注释来进行数据的依赖注入,如下面的通过注释的代码

/**
     * 根据userid删除订单
     * @param userId
     * @return
     */
    int deleteTradeByUserId(@Param("userId") Long userId);

就对以下数据库操作代码进行了执行

<update id="deleteTradeByUserId" parameterType="com.zhicong.account.dal.domainobject.UserTradeStatusDo">
        update
        <include refid="tableName"></include>
        <set>
            weixin_unionid = 'TestDeleteUnionid',
            updatetime = now()
        </set>
        where
        user_id = #{userId}
    </update>
  1. facde层接口实现

使用如下接口方法deleteWechatAccountByUnionID实现上述功能

	 // 根据unionid删除用户
    public boolean deleteWechatAccountByUnionID(SyncWechatAccountReq request);
  1. facde.impl层实现方法
    在facde.impl层下对于deleteWechatAccountByUnionID的继承如下所示
@AutoLogger
    private boolean isValidDeleteWechatAccount(SyncWechatAccountReq request){
        ServiceResponse<Boolean> response = null;
        try{
            validateWechatRequest(request);
        }catch(AccountException e){
            response = e.getResponse();
        }
        if(response != null){
            logger.error("deleteWechatAccount error"+response.getDesc());
            return false;
        }
        else return true;
    }
    
 @Override
    @AutoLogger
    public boolean deleteWechatAccountByUnionID(SyncWechatAccountReq request){
        if(isValidDeleteWechatAccount(request)){
            WechatAccountDo accountDo = convertWechatAccountVo2Do(request.getWechatAccountVo());
            return wechatAccountUnionIdDao.updateAllIdByUnionId(accountDo.getUnionId())>0;
        }
        else return false;
    }
  1. dubbo配置加入提供者方
    前面已经介绍过dubbo的配置就不再赘述,可以参照
    再学习。
<dubbo:service interface="com.zhicong.account.facade.DataMigrationFacade" ref="dataMigrationFacade" timeout="2000" retries="0"/>

这样的话,基本上的业务逻辑就完成了。

补充拓展

mybatis.xml中sql语句中对于标签引用的部分可以参考
https://blog.csdn.net/jslcylcy/article/details/65628390

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值