Simple implementation of mybatis calling MySQL stored procedure
1. Introduction to stored procedures
The SQL statement of our commonly used operation database language needs to be compiled before execution, and the stored procedure is a set of SQL statements to complete specific functions, which are compiled and stored in the database. Users call and execute the stored procedure by specifying its name and giving parameters (if the stored procedure has parameters).
A stored procedure is a programmable function that is created and saved in a database. It can be composed of SQL statements and some special control structures. Stored procedures are useful when you want to execute the same functions on different applications or platforms, or encapsulate specific functions. The stored procedure in the database can be regarded as a simulation of the object-oriented method in programming. It allows you to control how data is accessed.
2. Advantages of stored procedures
3. Disadvantages of stored procedures
4. Syntax of stored procedure
4.1 creating stored procedures
4.2 calling stored procedures
Note: the stored procedure name must be followed by parentheses, even if the stored procedure has no parameters passed
4.3 deleting stored procedures
Note: you cannot delete another stored procedure in one stored procedure. You can only call another stored procedure
4.4 other common commands
Displays the basic information of all stored procedures in the database, including the database, stored procedure name, creation time, etc
Displays the details of a MySQL stored procedure
5. Case implementation of mybatis calling MySQL stored procedure
5.1 brief description of the case
The case is mainly realized by simply counting the total number of devices with a certain name.
5.2 creation of database tables
5.3 creating stored procedures
Take the name of the device as the input parameter and the total number of devices counted as the output parameter
5.4 mybatis calling MySQL stored procedure
1. mybatis-config. xml
2. CusDevice. java
3. Implementation of devicedao
4. Mapper implementation
Note: statementtype = "callable" must be callable. Tell mybatis to execute the stored procedure, otherwise an error exception in thread "main" org will be reported apache. ibatis. exceptions. PersistenceException
Mode = in input parameter mode = out output parameter jdbctype is the field type defined by the database. Writing mybatis like this will help us automatically backfill the value of the output devicecount.
5. Test
result
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.