Spring boot+spring data 实现存储过程

emp实体
package com.yanshu.jcsapi.pojo;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.NamedStoredProcedureQueries;
import javax.persistence.NamedStoredProcedureQuery;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureParameter;
import javax.persistence.Table;
/**
 * emp实体对象
 * @author Administrator
 *
 */
@Entity
@Table(name="Emp")
@NamedNativeQueries(value = { @NamedNativeQuery(name = "findByName", query = " select * from Emp e where e.name=?1") })

@NamedStoredProcedureQuery(name="procEmp",procedureName = "procEmp" ,parameters = {
      @StoredProcedureParameter(name="id",mode = ParameterMode.IN,type=Integer.class),
      @StoredProcedureParameter(name="num" ,mode = ParameterMode.OUT,type = Integer.class)
})

public class Emp implements Serializable {

   /**
    * 
    * @NamedStoredProcedureQueries({
     @NamedStoredProcedureQuery(
       name="ReadEmployeeInOut",
        resultClass=org.eclipse.persistence.testing.models.jpa.customfeatures.Employee.class,
       procedureName="Read_Employee_InOut",
       parameters={
         @StoredProcedureParameter(direction=IN_OUT, name="employee_id_v", queryParameter="ID", type=Integer.class),
         @StoredProcedureParameter(direction=OUT, name="nchar_v", queryParameter="NCHARTYPE", type=Character.class)}
       ),
       @NamedStoredProcedureQuery(
         name="ReadEmployeeCursor",
         resultClass=org.eclipse.persistence.testing.models.jpa.customfeatures.Employee.class,
         procedureName="Read_Employee_Cursor",
         parameters={
           @StoredProcedureParameter(direction=IN, name="employee_id_v", queryParameter="ID", type=Integer.class),
           @StoredProcedureParameter(direction=OUT_CURSOR, queryParameter="RESULT_CURSOR")})
   })
    * @author Administrator
    *
    */
   
   /**
    * 
    */
   private static final long serialVersionUID = 1L;
   /**
    * id自动增长
    */
   private Integer id;
   /**
    * emp表的name主要用来存储名字的
    */
   private String name;
   /**
    * emp表的性别
    */
   private String sex;
   /**
    * emp表的爱好字段
    * 
    */
   private String hobas;
   /**
    * 设置emp表的主键
    * @return
    */
   @GeneratedValue
   @Id
   public Integer getId() {
      return id;
   }
   public void setId(Integer id) {
      this.id = id;
   }
   @Column(length=400)
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getSex() {
      return sex;
   }
   public void setSex(String sex) {
      this.sex = sex;
   }
   public String getHobas() {
      return hobas;
   }
   public void setHobas(String hobas) {
      this.hobas = hobas;
   }
   /**
    * 主要是为了测试方便
    */
   @Override
   public String toString() {
      return "Emp [id=" + id + ", name=" + name + ", sex=" + sex + ", hobas=" + hobas + "]";
   }
   

}
spring data jpa的标记接口
 
  
package com.yanshu.jcsapi.dao;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.Repository;
import org.springframework.data.jpa.repository.query.Procedure;
import org.springframework.data.repository.query.Param;
import java.util.List;
import com.yanshu.jcsapi.pojo.Emp;
public interface EmpProcRepsitory extends CrudRepository{
    @Procedure("procEmp")
    int explicitlyNamedProcEmpInt(Integer id);

   //@Query(value = "call update_iring_account(?1,?2,?3)", nativeQuery = true)
    @Query(value = "call procEmp(?1,?2) ", nativeQuery = true)
    boolean explicitlyProc(Integer id);
}
controller里面调用
 
  
package com.yanshu.jcsapi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import  com.yanshu.jcsapi.dao.*;

import com.yanshu.jcsapi.pojo.Emp;

import java.util.List;

@RestController
public class EmpController {


    @Autowired
   private EmpRepsitory empRepsitory;
    @Autowired
    private EmpProcRepsitory EmpProcRepsitory;

    @GetMapping("/show")
   //@RequestMapping("/show")
    public String getList()
    {
        List emp=empRepsitory.findByName("tom");

        System.out.print(emp+"----");

        return "emp";
    }
   @GetMapping("/showparam")
    public String paramList()
    {
        int i=EmpProcRepsitory.explicitlyNamedProcEmpInt(1);
       System.out.print(i+"--刘瑞光-");

        return "success";

    }

}

 
  

你可能感兴趣的:(Spring boot+spring data 实现存储过程)