spring boot jpa 使用原生sql查詢,特此記錄。
阿新 • • 發佈:2019-02-19
嗯哼,2018年7月18號特此記錄,使用java spring boot + jpa 開發後端,進行資料庫增刪改查(使用原生sql)進行。因為是第一次寫,僅僅是為了記錄,方便以後檢視,寫的不好還請見諒。
package com.jony.keer.repository; import com.jony.keer.entity.PersonModel; import com.jony.keer.entity.RegisterModel; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; @Repository public interface AccountRepository extends JpaRepository<RegisterModel, Long> { @Override RegisterModel save(RegisterModel s); /** * 自定義sql查詢(原生sql) * 查詢賬號是否已經存在 */ //根據手機號 賬號查找出相應賬號 1表示存在 >=1表示不存在 @Query(value = "SELECT 1 FROM tab_register WHERE cellphone=?1 OR act=?1 ", nativeQuery = true) int getActIsExistByCellPhone(String cellphone); //新增一條(註冊) @Query(value = "INSERT IGNORE INTO tab_register (cellphone,psd) VALUES (?1, ?2)", nativeQuery = true) @Modifying int insertRegisterModelOne(String cellphone, String psd); //登入 @Query(value = "SELECT * FROM tab_register WHERE cellphone=?1 AND psd=?2 ", nativeQuery = true) RegisterModel loginAct(String act, String psd); //修改密碼 @Query(value = "UPDATE tab_register SET psd=?2 WHERE id=?1", nativeQuery = true) @Modifying int upDateActPsdById(Integer id, String psd); //刪除賬號 @Query(value = "DELETE FROM tab_register WHERE id=?1", nativeQuery = true) @Modifying int delAct(Integer id); }
下面是dao層(service)這裡面寫的是具體業務流程
package com.jony.keer.service; import com.jony.keer.entity.CallBackBean; import com.jony.keer.entity.RegisterModel; import com.jony.keer.repository.AccountRepository; import com.jony.keer.utils.MobileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; /** * 賬戶體系管理 */ @Transactional @Service public class AccountService { @Autowired private AccountRepository accountRepository; /** * 根據賬號/手機號查詢,用來判斷該賬號/手機號是否已經被註冊過 * 如果可以查到相關資訊,擇表示該賬號/手機號已經被註冊 否則反之 */ public int getActIsExist(String act){ int success=-1; try { success=accountRepository.getActIsExistByCellPhone(act); }catch (Exception e){ success=0; } return success; } /** * 插入資訊(註冊賬號)INSERT */ public CallBackBean insertRegister(String cellphone,String psd){ if (!MobileUtils.isMobileNO(cellphone)) { return new CallBackBean(false, "手機號不正確!"); } int success = getActIsExist(cellphone); if (success == 1) { return new CallBackBean(false, "該手機號已經註冊,請前往登入。"); } int i=0; try { i = accountRepository.insertRegisterModelOne(cellphone,psd); }catch (Exception e){ i=0; } if (i > 0) { return new CallBackBean(true, "註冊成功!"); } else { return new CallBackBean(false, "註冊失敗!"); } } /** * 登入 */ public CallBackBean loginService(String cellphone,String psd){ if (!MobileUtils.isMobileNO(cellphone)) { return new CallBackBean(false, "手機號不正確!"); } if (cellphone.isEmpty()) { return new CallBackBean(false, "手機號不能為空"); } if (psd.isEmpty()) { return new CallBackBean(false, "密碼不能為空"); } RegisterModel registerModel; try { registerModel=accountRepository.loginAct(cellphone,psd); }catch (Exception e){ registerModel=null; } if (registerModel==null){ return new CallBackBean(false, "賬號/密碼不正確"); } return new CallBackBean(false, "登入成功",registerModel); } /** * 修改密碼 */ public CallBackBean updateService(Integer id,String psd){ if (id<=0) { return new CallBackBean(false, "賬號不存在"); } if (psd.isEmpty()) { return new CallBackBean(false, "密碼不能為空"); } int i=0; try { i=accountRepository.upDateActPsdById(id,psd); }catch (Exception e){ i=0; } if (i<=0){ return new CallBackBean(false, "修改失敗"); } return new CallBackBean(true, "修改成功"); } public CallBackBean deleteActService(Integer id){ if (id<=0) { return new CallBackBean(false, "賬號不存在"); } int i=0; try { i=accountRepository.delAct(id); }catch (Exception e){ i=0; } if (i<=0){ return new CallBackBean(false, "刪除失敗"); } return new CallBackBean(true, "刪除成功"); } }
最後是 Controller
package com.jony.keer.controller; import com.google.gson.Gson; import com.jony.keer.entity.CallBackBean; import com.jony.keer.entity.RegisterModel; import com.jony.keer.service.AccountService; import com.jony.keer.service.PersonService; import com.jony.keer.utils.MobileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; /** * Created by Song on 2017/2/15. * 賬戶管理介面類 */ @Controller @RequestMapping(value = "/account") public class AccountController { @Autowired AccountService service; /** * 註冊 * @param cellphone * @param psd * @return */ @RequestMapping(value = "register", method = RequestMethod.POST) @ResponseBody public CallBackBean registerController(@RequestParam(value = "cellphone", required = true) String cellphone, @RequestParam(value = "psd", required = true) String psd) { return service.insertRegister(cellphone,psd); } /** * 登入 * @param cellphone * @param psd * @return */ @RequestMapping(value = "login", method = RequestMethod.POST) @ResponseBody public CallBackBean loginContorller(@RequestParam(value = "cellphone", required = true) String cellphone, @RequestParam(value = "psd", required = true) String psd) { return service.loginService(cellphone, psd); } /** * 修改密碼 * @param id * @param psd * @return */ @RequestMapping(value = "updatePsd", method = RequestMethod.POST) @ResponseBody public CallBackBean updateContorller(@RequestParam(value = "id", required = true) Integer id, @RequestParam(value = "psd", required = true) String psd) { return service.updateService(id, psd); } /** * 刪除賬號 * @param id * @return */ @RequestMapping(value = "deleteAct", method = RequestMethod.POST) @ResponseBody public CallBackBean deleteActContorller(@RequestParam(value = "id", required = true) Integer id) { return service.deleteActService(id); } }