本文主要是介绍ATM提款机的转账,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
基于springboot,微服务springcloud和eurekaserver服务。
实现在ATM提款机转账功能。在前台界面上输入要转账人的卡号,通过卡号在数据库中查询数据库中是否存在此用户。
以下在springcloud的notice服务中的实现此功能的代码块:
Mapper.xml
//mapper.xml文件中的sql语句,通过userid(卡号)查询此人信息
<select id="getUserById" parameterType="java.lang.String" resultType="four.entities.User">//参数类型为String,返回值类型为Userselect userid,name,pwd,money from user where userid=#{userid};</select>
dao层 (接口)
@Mapper
public interface ChangeMapper {public User getUserById(String userid);
}
service层
@Service
public class ChangeServiceImpl {@Resourceprivate ChangeMapper dao;public User getUserById(String userid){return dao.getUserById(userid);}
}
controller层
@RestController
public class ChangeController {@Resourceprivate ChangeServiceImpl changeService;@GetMapping("/getbyid/{userid}")//get类型接口public JsonResult getUserById(@PathVariable String userid, HttpServletRequest request){//返回值为Json类型JsonResult res;if(changeService.getUserById(userid)==null||changeService.getUserById(userid).equals("")){res=new JsonResult(200,"没有此用户");}else{User user=changeService.getUserById(userid);//将从数据库中查到的数据返回一个User类型res=new JsonResult(0,"成功");res.setData(user);//将User类型对象写给Json类型的datarequest.setAttribute("user",user);}return res;}}
下面是ums服务调用notice服务的代码块:
将ums与notice服务中心共同需要的entity层的实体类和model层的JsonResult类从
notice中复制到ums中。
在ums中新建client层创建client层接口。
client层
import four.model.JsonResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;import javax.servlet.http.HttpServletRequest;@FeignClient(value = "NOTICE-SERVICE") //微服务名称
@Component
public interface ChangeClient {@GetMapping(value = "/getbyid/{userid}")public JsonResult getUserById(@PathVariable(value = "userid") String userid);//注解@PathVariable必须要写value参数否则会报bug//启动报错:PathVariable annotation was empty on param 0
}
application.yml中notice服务名称,将它加在@FeignClient注解的value中
controller层
import four.client.ChangeClient;
import four.model.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
@Slf4j //加载Slf4j日志对象
public class ChangeController {@Resourceprivate ChangeClient client;@GetMapping(value = "/getbyid/{userid}")public JsonResult getUserById(@PathVariable String userid){return client.getUserById(userid);}}
这篇关于ATM提款机的转账的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!