服务器之家

服务器之家 > 正文

关于Mybatis 中使用Mysql存储过程的方法

时间:2021-04-16 13:23     来源/作者:onlylele

 

1.存储过程的简介

 

我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给定参数(如果该存储过程带有参数)来调用执行它。

一个存储过程是一个可编程的函数,它在数据库中创建并保存。它可以有SQL语句和一些特殊的控制结构组成。当希望在不同的应用程序或平台上执行相同的函数,或者封装特定功能时,存储过程是非常有用的。数据库中的存储过程可以看做是对编程中面向对象方法的模拟。它允许控制数据的访问方式。

 

2.存储过程优点

 

1.存储过程增强了SQL语言的功能和灵活性。存储过程可以用流控制语句编写,有很强的灵活性,可以完成复杂的判断和较复杂的运算。

2.存储过程允许标准组件是编程。存储过程被创建后,可以在程序中被多次调用,而不必重新编写该存储过程的SQL语句。而且数据库专业人员可以随时对存储过程进行修改,对应用程序源代码毫无影响。

3.存储过程能实现较快的执行速度。如果某一操作包含大量的Transaction-SQL代码或分别被多次执行,那么存储过程要比批处理的执行速度快很多。因为存储过程是预编译的。在首次运行一个存储过程时查询,优化器对其进行分析优化,并且给出最终被存储在系统表中的执行计划。而批处理的Transaction-SQL语句在每次运行时都要进行编译和优化,速度相对要慢一些。

4.存储过程能过减少网络流量。针对同一个数据库对象的操作(如查询、修改),如果这一操作所涉及的Transaction-SQL语句被组织程存储过程,那么当在客户计算机上调用该存储过程时,网络中传送的只是该调用语句,从而大大增加了网络流量并降低了网络负载。

5.存储过程可被作为一种安全机制来充分利用。系统管理员通过执行某一存储过程的权限进行限制,能够实现对相应的数据的访问权限的限制,避免了非授权用户对数据的访问,保证了数据的安全。

 

3.存储过程缺点

 

1.不易维护,一旦逻辑变了修改起来麻烦

2.如果写此存储过程的人离职了,对于接手她代码的人估计是一场灾难,因为别人还要去读懂你程序逻辑,还要读懂你存储逻辑。不利于扩展。

3.最大的缺点! 虽然存储过程可以减少代码量,提高开发效率。但是有一点非常致命的就是太耗性能。

下面通过代码介绍下mybatis 中 使用MYSQL存储过程;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
##1.有学生表student(id,name,age,money)
##2.创建查询学生表信息的存储过程:
 
delimiter |
 create Procedure showAllstu()
 BEGIN
  SELECT * FROM student ORDER BY id DESC LIMIT 6;
 END
delimiter
 
##2.创建(通过学生id)删除记录的存储过程:
```
delimiter |
 create Procedure delById(d int(11))
 BEGIN
  delete from student where id=d;
 END
delimiter
 
##3.maven中创建项目:(略)
 
 //pox.xml 配置:
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.metar</groupId>
  <artifactId>Mybatis-mysql</artifactId>
  <packaging>war</packaging>
  <version>1.0</version>
  <name>Mybatis-mysql Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <version>1.16.20</version>
   <scope>provided</scope>
  </dependency>
   <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.6</version>
   </dependency>
   <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.6</version>
   </dependency>
   <dependency>
   <groupId>ch.qos.logback</groupId>
   <artifactId>logback-classic</artifactId>
   <version>1.2.3</version>
   <scope>test</scope>
   </dependency>
  </dependencies>
  <build>
   <finalName>${project.artifactId}</finalName>
   <testSourceDirectory>src/test/java</testSourceDirectory>
   <sourceDirectory>src/main/java</sourceDirectory>
   <!-- 处理无法加载资源配置文件 -->
   <resources>
    <resource>
     <directory>src/main/java</directory>
     <includes>
      <include>**/*.xml</include>
      <include>**/*.properties</include>
     </includes>
    </resource>
    <resource>
     <directory>src/main/resources</directory>
     <includes>
      <include>**/*.xml</include>
      <include>**/*.properties</include>
     </includes>
    </resource>
   </resources>
  </build>
 </project>
 
##4.链接数据库入口配置(略),配置:mybatis-config.xml:
 
 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE configuration
   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-config.dtd">
 <configuration>
  <!-- 这是资源数据库的配置入口 -->
  <properties resource="db.properties"/>
  <!-- 开启日志设置 -->
  <settings>
   <setting name="logPrefix" value="dao."/>
  </settings>
  <typeAliases>
   <!-- 配置模型类的别名 -->
   <!--<typeAlias type="com.fz.entity.Student" alias="st"/>-->
   <!-- 配置指定包下的所有类别名 //com.fz.entity包下的模型类小字别名 Book.java book就是别名-->
   <package name="com.fz.entity"/>
  </typeAliases>
  <environments default="development">
   <environment id="development">
    <transactionManager type="JDBC"/>
    <dataSource type="POOLED">
     <property name="driver" value="${db.driver}"/>
     <property name="url" value="${db.url}"/>
     <property name="username" value="${db.user}"/>
     <property name="password" value="${db.password}"/>
    </dataSource>
   </environment>
  </environments>
  <mappers>
   <!--<mapper class="com.fz.mapper.StudentMapper"/>-->
   <package name="com.fz.mapper"/>
  </mappers>
 </configuration>
 
##5.创建实体类对象:
 
 //包:com/fz/entity/Student
 @Data
 public class Student {
  private int id;
  private String name;
  private int age;
  private double money;
 }
 
 ##6.创建StudentMapper 接口类和StudentMapper.xml配置;
 
 // StudentMapper 接口类
 public interface StudentMapper {
  //存储过程查询6条记录;
  public List<Student> query();
  //存储过程删除一条记录(通过id)
  public int delById(int id);
 }
 //StudentMapper.xml配置
 ```
 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE mapper
   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.fz.mapper.StudentMapper">
  <select id="query" resultType="student">
   {call showAllstu()}
  </select>
  <delete id="delById" parameterType="int">
   {call delById(#{id})}
  </delete>
 </mapper>
 
 ##7.测试 类:
 
 //test/java/com/Demo01
 package com;
 import com.fz.entity.Student;
 import com.fz.mapper.StudentMapper;
 import org.apache.ibatis.io.Resources;
 import org.apache.ibatis.session.SqlSession;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.List;
 public class Demo01 {
   protected SqlSessionFactory sf;
   protected SqlSession ss;
   @Test
   public void test(){
    StudentMapper sdd = this.ss.getMapper(StudentMapper.class);
    List<Student> atd = sdd.query();
    for (Student sd:atd){
     System.out.println(sd);
    }
    sdd.delById(18);
   }
   @Before
   public void init(){
    InputStream is=null;
    try {
     is= Resources.getResourceAsStream("mybatis-config.xml");
     this.sf=new SqlSessionFactoryBuilder().build(is);
     this.ss=this.sf.openSession();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   @After
   public void close(){
    this.ss.commit();
    this.ss.close();
   }
 }

补充:

下面看下存储过程的语法

1 创建存储过程

?
1
2
3
4
create procedure sp_name()
begin
.........
end

2 调用存储过程

call sp_name()

注意:存储过程名称后面必须加括号,哪怕该存储过程没有参数传递

3 删除存储过程

drop procedure sp_name//

注意:不能在一个存储过程中删除另一个存储过程,只能调用另一个存储过程

4其他常用命令

show procedure status

显示数据库中所有存储的存储过程基本信息,包括所属数据库,存储过程名称,创建时间等

show create procedure sp_name

显示某一个MySQL存储过程的详细信息

总结

以上所述是小编给大家介绍的Mybatis 中使用Mysql存储过程的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://blog.csdn.net/onlylele/article/details/79701845

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部