java—如何使用hibernate在spring boot中调用mysql存储过程?

ergxz8rk  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(406)

我已经在mysql存储过程中编写了一些逻辑,我正在使用带hibernate的spring boot。我有一个带有输入输出参数的登录过程。从我的登录过程中,我想把消息传递给用户。但我不知道如何在SpringBoot中调用存储过程。我的密码是贝娄。。
我的登录程序是

CREATE PROCEDURE login(IN  in_user_id    varchar(100),
                           IN  in_password   varchar(100),
                           OUT out_code      INT,
                           OUT out_message   varchar(100))
 BEGIN
    IF in_user_id IS NULL OR in_user_id = ''
     THEN
     SET out_code = 1;
     SET out_message = 'Please Enter Your First Name.';
   END IF;
 /*Logi Here*/

 END;

我使用了实体类

@Entity
@Table(name = "user")
 @NamedStoredProcedureQueries({
   @NamedStoredProcedureQuery(
   name = "do_login", 
   procedureName = "login", 
   resultClasses = { LoginModel.class },    
   parameters = { 
      @StoredProcedureParameter( name = " in_user_id",  type = String.class,  mode = ParameterMode.IN),
      @StoredProcedureParameter( name = "in_password",  type = String.class,  mode = ParameterMode.IN),
      @StoredProcedureParameter( name = "out_code",  type = Integer.class,  mode = ParameterMode.OUT), 
      @StoredProcedureParameter( name = "out_message",  type = String.class,  mode = ParameterMode.OUT)

     }),

 })

 public class LoginModel implements Serializable {

   @NotEmpty
   private String userid;

   @NotEmpty
   private String password;

  //Here is getter setter
  }

在我的登录控制器中,我想调用我的过程,以便将用户转发到 Jmeter 板。如果用户输入了错误的用户id或密码,我想显示来自过程的消息。我在我的登录控制器中使用了下面的代码

@RequestMapping(value = "/login", method = RequestMethod.POST)
 public String doLogin(@ModelAttribute("webLoginForm") @Valid LoginModel registrationModel,
    BindingResult bindingResult, Model model, Errors error) {
     if(error.hasErrors()) {
         return "login";
     }

   // Here I want to check My Procedure result & redirect to welcome page
   //return "redirect:/welcome";

  return "login";
}

我使用过存储库,但它没有在这里写任何东西。我曾经用过像。。

public interface LoginRepository  extends CrudRepository<LoginModel, Integer>{

 }
vwoqyblh

vwoqyblh1#

这对我使用jdbctempate很有效

SimpleJdbcCall jdbcCall =  new SimpleJdbcCall(jdbcTemplate).withProcedureName("your procedure name");

    Map<String, Object> inParamMap = new HashMap<>();

    inParamMap.put("input of your procedure", value of input);

    SqlParameterSource in = new MapSqlParameterSource(inParamMap);

    Map<String, Object> call = jdbcCall.execute(in);
mkh04yzy

mkh04yzy2#

试试这个简单的解决方案,我在你的存储库中使用SpringBoot和SQLServer
1) 不带参数 @Query(value = "{call yourSpName()}", nativeQuery = true) List<Map<String, Object>> methodName(); 2) 带参数 @Query(value = "{call yourSpName(:param1)}", nativeQuery = true) List<Map<String, Object>> methodName(@Param("param1")Long param1);

zbwhf8kr

zbwhf8kr3#

您可以尝试使用此方法从spring调用过程

import java.util.HashMap;
import java.util.Map;
import oracle.jdbc.driver.OracleTypes;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;

public class MainApplicationMethod 

{

public static void main(String[] args) throws Exception {

    JdbcTemplate jdbcTemplate = new JdbcTemplate();

    SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName("procedureName");
    jdbcCall.addDeclaredParameter(new SqlParameter("parameter1", OracleTypes.VARCHAR));
    jdbcCall.addDeclaredParameter(new SqlOutParameter("outParam1",OracleTypes.VARCHAR));
    String value=null;
    Map<String, String> callParams = new HashMap<String, String>();
    callParams.put("parameter1", value);

    Map<String, Object> outputMap = jdbcCall.execute(callParams);
}

}

kknvjkwl

kknvjkwl4#

可以使用javax.persistence.storedprocedurequery调用存储过程。你甚至不需要在你的实体上声明任何东西。
我建议将过程调用逻辑移到服务,然后从控制器调用服务方法。
例如:

@Service
public class LoginServiceImpl implements LoginService {

    @PersistenceContext
    private EntityManager entityManager;

    public Boolean checkUsernameAndPassword(String username, String password) {

        //"login" this is the name of your procedure
        StoredProcedureQuery query = entityManager.createStoredProcedureQuery("login"); 

        //Declare the parameters in the same order
        query.registerStoredProcedureParameter(1, String.class, ParameterMode.IN);
        query.registerStoredProcedureParameter(2, String.class, ParameterMode.IN);
        query.registerStoredProcedureParameter(3, Integer.class, ParameterMode.OUT);
        query.registerStoredProcedureParameter(4, String.class, ParameterMode.OUT);

        //Pass the parameter values
        query.setParameter(1, username);
        query.setParameter(2, password);

        //Execute query
        query.execute();

        //Get output parameters
        Integer outCode = (Integer) query.getOutputParameterValue(3);
        String outMessage = (String) query.getOutputParameterValue(4);

        return true; //enter your condition
    }
}

然后,在注入 LoginService .

相关问题