Mybatis+mysql uses stored procedures to generate serial number implementation code

Mybatis+mysql uses stored procedures to generate serial number implementation code

Use stored procedures to start transactions when operating the database to avoid data duplication caused by concurrent operations

CREATE DEFINER=`root`@`localhost` PROCEDURE `GetSerialNo`(IN tsCode VARCHAR(50),OUT result VARCHAR(200) )
BEGIN 
 DECLARE tsValue VARCHAR(50); 
 DECLARE tdToday VARCHAR(20);  
 DECLARE nowdate VARCHAR(20);  
 DECLARE tsQZ VARCHAR(50); 
 DECLARE t_error INTEGER DEFAULT 0; 
 DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1; 
 START TRANSACTION; 
  /* UPDATE sys_sno SET sValue=sValue WHERE sCode=tsCode; */
  SELECT sValue INTO tsValue FROM sys_sno WHERE sCode=tsCode; 
  SELECT sQz INTO tsQZ FROM sys_sno WHERE sCode=tsCode; 
 -- There is no record in the factor table, insert the initial value IF tsValue IS NULL THEN 
   SELECT CONCAT(DATE_FORMAT(NOW(),'%y%m'),'0001') INTO tsValue; 
   UPDATE sys_sno SET sValue=tsValue WHERE sCode=tsCode; 
   SELECT CONCAT(tsQZ,tsValue) INTO result; 
  ELSE     
   SELECT SUBSTRING(tsValue,1,4) INTO tdToday; 
   SELECT CONVERT(DATE_FORMAT(NOW(),'%y%m'),SIGNED) INTO nowdate;
   -- Determine whether the year and month need to be updated IF tdToday = nowdate THEN 
    SET tsValue=CONVERT(tsValue,SIGNED) + 1; 
   ELSE 
    SELECT CONCAT(DATE_FORMAT(NOW(),'%y%m') ,'0001') INTO tsValue; 
   END IF; 
   UPDATE sys_sno SET sValue = tsValue WHERE sCode = tsCode; 
   SELECT CONCAT(tsQZ,tsValue) INTO result; 
  END IF; 
  IF t_error =1 THEN 
  ROLLBACK; 
  SET result = 'Error'; 
  ELSE 
  COMMIT; 
  END IF; 
  SELECT result ;  
END;
dao
Integer getFaultNo(Map<String, String> parameterMap);

xml

<update id="getFaultNo" parameterMap="getFaultMap" statementType="CALLABLE">
  CALL GetSerialNo(?,?)
 </update>
 <!--
  parameterMap.put("tsCode", 0);
  parameterMap.put("result", -1);
  -->
 <parameterMap type="java.util.Map" id="getFaultMap">
  <parameter property="tsCode" mode="IN" jdbcType="VARCHAR"/>
  <parameter property="result" mode="OUT" jdbcType="VARCHAR"/>
 </parameterMap>

Call

Map<String, String> parameterMap = new HashMap<String, String>();
  parameterMap.put("tsCode", "a");
  parameterMap.put("result", "-1");
  faultMapper.getFaultNo(parameterMap);
  // insert failure log (main table)
  if (!parameterMap.get("result").equals("-1") && 
    !parameterMap.get("result").equals("Error")) {
   //Success} else {
   throw new RuntimeException();
  }

Summarize

The above is the implementation code of mybatis+mysql using stored procedures to generate serial numbers introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • How to use Mysql stored procedures in Mybatis
  • mysql+mybatis implements stored procedure + transaction + multi-concurrent serial number acquisition

<<:  Summary of Linux user groups and permissions

>>:  Steps to introduce PWA into Vue project

Recommend

Example of stars for CSS rating effect

What? What star coat? Well, let’s look at the pic...

MySQL 5.6.37 (zip) download installation configuration graphic tutorial

This article shares the download, installation an...

Detailed explanation of the principle and function of Vue list rendering key

Table of contents The principle and function of l...

Introduction to the method attribute of the Form form in HTML

1 method is a property that specifies how data is ...

HTML weight loss Streamline HTML tags to create web pages

HTML 4 HTML (not XHTML), MIME type is text/html, ...

Detailed explanation of the steps of using ElementUI in actual projects

Table of contents 1. Table self-sorting 2. Paging...

Analysis of parameter transfer process of driver module in Linux

Declare the parameter name, type and permission y...

Analysis of the configuration process of installing mariadb based on docker

1. Installation Search the mariadb version to be ...

Record the whole process of MySQL master-slave configuration based on Linux

mysql master-slave configuration 1. Preparation H...

Detailed tutorial on building a local idea activation server

Preface The blogger uses the idea IDE. Because th...

MySQL 8.0.12 installation configuration method and password change

This article records the installation and configu...

JavaScript implements the detailed process of stack structure

Table of contents 1. Understanding the stack stru...

How to configure redis sentinel mode in Docker (on multiple servers)

Table of contents Preface condition Install Docke...