mybatis 中 使用MYSQL存储过程

#mybatis 中 使用MYSQL存储过程;
##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();
            }
    }
    
 //以上仅供初学者参考学习。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值