首页 > 编程 > Java > 正文

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

2019-11-26 10:08:28
字体:
来源:转载
供稿:网友

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.有学生表student(id,name,age,money)##2.创建查询学生表信息的存储过程:delimiter | create Procedure showAllstu() BEGIN  SELECT * FROM student ORDER BY id DESC LIMIT 6; ENDdelimiter##2.创建(通过学生id)删除记录的存储过程:```delimiter | create Procedure delById(d int(11)) BEGIN  delete from student where id=d; ENDdelimiter##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 创建存储过程

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存储过程的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表