"Business logic" based on springmvc+spring+mybatis implementation of second-kill system

Source: Internet
Author: User
Tags date now getmessage md5 encryption

Objective

The main realization of the second-kill business layer, the second-kill business logic mainly includes exposing the seconds to kill the interface address, the realization of the second-kill business logic. At the same time, three business classes were declared: Exposer, Seckillexecution, Seckillresult. Exposer is primarily used to implement an MD5 encryption when exposing an interface, preventing users from tampering with the data on the client. Depending on the seckillid generated MD5, the request to commit a second kill is based on whether the MD5 and Seckillid are legitimate requests. Seckillexecution The return value of the primary package for the second kill.

Seckillexecution has 2 properties, state, Stateinfo, where I don't encapsulate enumeration values, or use integers and strings to pass values to the client, it's intuitive to look at the service.

Preparatory work

1, Spring-service.xml

The key in the business logic is to open the transaction, which is recommended in the form of annotations.

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">    <!--1. Annotation Package Scan -    <Context:component-scanBase-package= "Com.seckill.service"/>    <!--2. Configure Declarative Transactions -    <BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager">        <!--Inject Database -        < Propertyname= "DataSource"ref= "DataSource"/>    </Bean>    <!--3. Configuring declarative transactions based on annotations -    <Tx:annotation-drivenTransaction-manager= "TransactionManager"/></Beans>

Achieve second-kill business

The key method related to the second kill is the last two methods, one is the external burst the second kill address, one is the second kill method.

Public interface Seckillservice {    list<seckill> getseckilllist ();    Seckill getById (long seckillid);    /** external burst seconds Kill interface **/    exposer exposeseckillurl (long seckillid);    /**     * Performs a second kill operation, it is possible to succeed, it is possible to fail, so here we throw a custom exception     * ***/    seckillexecution executeseckill (Long Seckillid,long Phone,string MD5) throws Seckillexception,            repeatkillexception,            seckillcloseexception;}

  

The enumeration of the @Servicepublic class Seckillserviceimpl implements Seckillservice {/*** * sec kill behavior is put here to show that * 1, second kill success * 0, seconds kill knot Bundle *-1, repeat second kill *-2, System exception *-3, data tampering * ***/private Logger Logger = Loggerfactory.getlogger (This.getclass ())    ;    @Autowired Seckilldao Seckilldao;    @Autowired Successkilldao Successkilldao;    Private String Salt = "Zhangfei";    @Override public list<seckill> getseckilllist () {return Seckilldao.queryall (0, 100);    } @Override Public Seckill getById (long seckillid) {return Seckilldao.querybyid (seckillid);        } @Override Public Exposer exposeseckillurl (long seckillid) {Seckill Seckill = getById (seckillid);        Date startTime = Seckill.getstarttime ();        Date endTime = Seckill.getendtime ();        Date now = new Date ();  if (Now.gettime () < Starttime.gettime () | | now.gettime () > Endtime.gettime ()) {return new Exposer (false, Seckillid, Starttime.gettime (), Endtime.gettime(), Now.gettime ());        } String MD5 = GETMD5 (seckillid);    return new Exposer (true, MD5, seckillid);            } @Override @Transactional public seckillexecution executeseckill (long seckillid, long phone, String MD5) Throws Seckillexception,repeatkillexception,seckillcloseexception {if (MD5 = = NULL | |!md5.equals (GETMD5 (Secki        Llid)) {throw new seckillexception ("illegal request");        } Date now = new Date ();            try {int insertcount = successkilldao.insertsuccesskilled (seckillid, phone);            if (insertcount <= 0) {throw new Repeatkillexception ("Repeat second Kill");                } else {int updatecount = Seckilldao.reducenumber (Seckillid, now);                if (updatecount <= 0) {throw new Seckillcloseexception ("Seconds Kill closed"); } else {//sec kill successfully, can kill details and commodity detail entity return successkilled successkilled = Successkilldao.queryb Yidwithseckill (secKillid, phone);                return new Seckillexecution (Seckillid, 1, "Second kill success", successkilled);        }}} catch (Seckillcloseexception e) {throw e;        } catch (Repeatkillexception E1) {throw E1;            } catch (seckillexception E2) {Logger.error (E2.getmessage (), E2);        throw new Seckillexception ("Unkonwn error:" + e2.getmessage ());        }} private String GetMd5 (long seckillid) {string base = Seckillid + "/" + salt;        String MD5 = Digestutils.md5digestashex (Base.getbytes ());    return MD5; }}

  

"Business logic" based on springmvc+spring+mybatis implementation of second-kill system

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.