spring boot jpa 使用原生sql查询,特此记录。

 嗯哼,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);
    }

}
展开阅读全文

没有更多推荐了,返回首页