目录:
移动大脑-SpringMVc搭建RestFul后台服务(一)-环境搭建
移动大脑-SpringMVc搭建RestFul后台服务(二)-配置mysql数据库
移动大脑-SpringMVc搭建RestFul后台服务(三)-RestFul接口编写(模拟用户注册登录)
移动大脑-SpringMVc搭建RestFul后台服务(四)-添加Token拦截器
移动大脑-SpringMVc搭建RestFul后台服务(五)-支付宝支付
移动大脑-SpringMVc搭建RestFul后台服务(六)-微信支付(Android)
移动大脑-SpringMVc搭建RestFul后台服务(七)-增量更新
接着上一篇博客《移动大脑-SpringMVc搭建RestFul后台服务(二)-配置mysql数据库》 ,已经配置好了Mysql数据库,现在开始编写RestFul接口了,模拟实现用户的注册(PUT)和登录(POST)两个接口:
还是最经典的action-service-dao结构
一、创建UserDao.java接口和实现类UserDaoImpl.java
package com.ywl5320.appservice.dao;
import com.ywl5320.appservice.bean.UserBean;
/**
* Created by ywl5320 on 2017/10/12.
*/
public interface UserDao {
/**
* 注册
* @param userBean
*/
UserBean registor(UserBean userBean);
/**
* 登陆
* @return
*/
UserBean login(String phone, String password);
/**
* 根据名字获取用户信息
* @param phone
* @return
*/
UserBean getUser(String phone);
}
package com.ywl5320.appservice.dao;
import com.ywl5320.appservice.bean.UserBean;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import java.util.List;
/**
* Created by ywl5320 on 2017/10/12.
*/
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
public UserBean login(String phone, String password) {
List users = (List) this.getHibernateTemplate().find("from UserBean where phone=? and password=?", phone, password);
if(users != null && users.size() > 0)
{
return users.get(0);
}
return null;
}
public UserBean registor(UserBean userBean) {
this.getHibernateTemplate().save(userBean);
return getUser(userBean.getPhone());
}
public UserBean getUser(String phone) {
List users = (List) this.getHibernateTemplate().find("from UserBean where phone=?", phone);
if(users != null && users.size() > 0)
{
return users.get(0);
}
return null;
}
}
这里实现类继承了HibernateDaoSupport类,就可以使用hibernate的语句操作数据库了。
二、编写UserService.java类
package com.ywl5320.appservice.service;
import com.ywl5320.appservice.bean.RestFulBean;
import com.ywl5320.appservice.bean.UserBean;
import com.ywl5320.appservice.dao.UserDao;
import com.ywl5320.appservice.util.RestFulUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
/**
* Created by ywl5320 on 2017/10/12.
*/
@Transactional
public class UserService {
@Autowired
private UserDao userDao;
public RestFulBean registorServer(UserBean userBean)
{
UserBean user = userDao.getUser(userBean.getPhone());
if(user != null)
{
return RestFulUtil.getInstance().getResuFulBean(null, 1, "已经注册过了");
}
else
{
user = userDao.registor(userBean);
if(user != null)
{
return RestFulUtil.getInstance().getResuFulBean(user, 0, "注册成功");
}
else{
return RestFulUtil.getInstance().getResuFulBean(null, 1, "注册失败");
}
}
}
public RestFulBean login(String phone, String password)
{
UserBean user = userDao.login(phone, password);
if(user != null)
{
return RestFulUtil.getInstance().getResuFulBean(user, 0, "登录成功");
}
else
{
return RestFulUtil.getInstance().getResuFulBean(null, 1, "用户不存在");
}
}
}
这里用了注解@Transactional和@Autowired来自动扫描service和注入实体类。
三、在beans.xml中配置service和dao
四、编写UserAction.java
package com.ywl5320.appservice.action;
import com.ywl5320.appservice.bean.RestFulBean;
import com.ywl5320.appservice.bean.UserBean;
import com.ywl5320.appservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/**
* Created by ywl5320 on 2017-10-2.
*/
@Controller
@RequestMapping("/user")
public class UserAction {
@Autowired
private UserService userService;
/**
* 注册
* @param userBean
* @return
*/
@ResponseBody
@RequestMapping(value="/register.do", method= RequestMethod.PUT)
public RestFulBean register(@RequestBody UserBean userBean)
{
return userService.registorServer(userBean);
}
/**
* 登录
* @param phone
* @return
*/
@ResponseBody
@RequestMapping(value="/loginbypwd.do", method= RequestMethod.POST)
public RestFulBean loginByPwd(String phone, String password)
{
System.out.println("phone:" + phone);
return userService.login(phone, password);
}
}
注册接口:/user/register.do
登录接口:/user/loginbypwd.do
RestFulUtil.java
package com.ywl5320.appservice.util;
import com.ywl5320.appservice.bean.RestFulBean;
/**
* Created by ywl5320 on 2017-10-15.
*/
public class RestFulUtil {
private RestFulUtil(){}
public static RestFulUtil getInstance()
{
return new RestFulUtil();
}
public RestFulBean getResuFulBean(T o, int status, String msg)
{
RestFulBean objectRestFulBean = new RestFulBean();
objectRestFulBean.setStatus(status);
objectRestFulBean.setMsg(msg);
objectRestFulBean.setData(o);
return objectRestFulBean;
}
}
整体目录如下:
五、启动服务,移动端访问测试(移动端是根据我的GitHub RxjavaRetrofit 项目修改而来的)
1、注册:
2、注册2
3、登录
4、数据库数据
源码下载:GitHub 服务端(AppServiceRestFul)和移动端(RxJavaRetrofit)
OK,这样就完成了注册登录接口了,移动端可以顺利访问。下一篇会添加token和拦截器。~~接着看SKT vs EDG了,目测后期EDG有点难了~。