我可以使用UserRepository将用户/保存到MYSQL数据库,但是当我尝试使用相同的信息/登录时,我会遇到一个错误,我无法弄清楚。
java. sql. sql语法错误异常:SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,了解在第1行"123 !@#"附近使用的正确语法
储存库
package com.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.entity.User;
@Repository
public interface UserRepository extends JpaRepository<User, String> {
///builds successfully but crashes saying sql syntax error, look up manual. Will allow me to save users to db
@Query(value = "Select S from User S where S.email = ?1 and S.pass ?2", nativeQuery = true)
User login( String user, String pass);
//doesnt build correct but is like my other repository
// @Query("Select S from User S where S.email = ?1 and S.pass ?2")
// User login( String user, String pass);
}
用户控制器
package com.controller;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.entity.Saved;
import com.entity.User;
import com.repository.SavedRepository;
import com.repository.UserRepository;
@CrossOrigin
@RestController
public class UserController {
@Autowired
UserRepository userRepository;
@Autowired
SavedRepository savedRepository;
//~~~~~~~~~~~~~~~~~~user details group ~~~~~~~~~~~~
//saving user details from new user login page
@RequestMapping(value = "/save",
consumes=MediaType.APPLICATION_JSON_VALUE,
produces=MediaType.APPLICATION_JSON_VALUE,
method=RequestMethod.POST
)
public void submitUserDetails(@RequestBody User user) {
userRepository.save(user);
}
//finds user details
@RequestMapping(value="/findUser",
produces=MediaType.APPLICATION_JSON_VALUE,
method=RequestMethod.GET
)
private ResponseEntity<Optional<User>>findUser(String email){
Optional<User> user = userRepository.findById(email);
return new ResponseEntity<>(user,HttpStatus.OK);
}
//user Login details
@RequestMapping(value="/login",
produces=MediaType.APPLICATION_JSON_VALUE,
method=RequestMethod.POST
)
public ResponseEntity<User>login(@RequestBody User user){
User userLogin = userRepository.login(user.getUser(), user.getPass());
if(userLogin != null) {
return new ResponseEntity<>(userLogin, HttpStatus.OK);
}else {
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
}
我试过使用原生查询和Jpla查询,并将它们参数化。我不确定还能做什么。
1条答案
按热度按时间zfycwa2u1#
您可以选择在SpringDataJPA中创建自定义方法,而不是使用原生查询。
您可以获取更多详细信息:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.details