您好,登錄后才能下訂單哦!
上圖是查詢列表的接口,get方式
上圖是用戶注冊的接口,同樣是get,post方式也很簡單
開發工具:IntelliJ IDEA 2016.3.5
ORM框架:MyBatis
數據庫:MySql
服務器:tomcat7.0
公司使用的的orm框架是Hibernate,使用起來感覺比mybatis好用多了,畢竟經過了公司這么多項目的考驗,總比自己用mybatis寫的項目可靠,但以下分享的還是mybatis的代碼
注冊接口方法:http://192.168.1.116:8080/register?username=111&password=222
@RequestMapping(value = "register", method = RequestMethod.GET) @ResponseBody public Map<String, Object> register(@RequestParam("username") String username, @RequestParam("password") String password) { out.println("welcome to register,username=" + username + ";password=" + password); Map<String, Object> map = new HashMap<>(); ResultBean result = onRegister(username, password); out.println("result==>" + result); map.put("code", result.getCode()); map.put("reason", result.getReason()); map.put("success", result.isSuccess()); return map; }
具體的注冊方法,獲取session基本和Hibernate差不多
private ResultBean onRegister(String username, String password) { ResultBean resultBean = new ResultBean(); SqlSession session = null; try { session = sqlSessionFactory.openSession(); LoginMapper loginMapper = session.getMapper(LoginMapper.class); Map<String, Object> map = new HashMap<>(); map.put("name", username); map.put("password", password); LoginBean bean = new LoginBean(); bean.setName(username); bean.setPassword(password); // 查詢用戶是否存在 LoginBean userExist = loginMapper.findUserByName(map); if (userExist != null) { // 存在后無法注冊 resultBean.setCode("001"); resultBean.setSuccess(false); resultBean.setReason("用戶已存在"); } else { loginMapper.addUser(bean); session.commit();// 重要,一定要commit,否則無法insert System.out.println("當前增加的用戶id為:" + bean.getId()); resultBean.setCode("200"); resultBean.setSuccess(true); resultBean.setReason("注冊成功"); } } catch (Exception e) { e.printStackTrace(); out.println("注冊異常==>" + e.getMessage()); resultBean.setCode("001"); resultBean.setSuccess(false); resultBean.setReason("注冊異常"); } finally { session.close(); } return resultBean; }
這個Mapper需要在配置文件中指定
public interface LoginMapper { public LoginBean findUserByName(Map<String,Object> map) throws Exception; public void addUser(LoginBean bean) throws Exception; }
這是對應的LoginMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <mapper namespace="com.xm.travel.LoginMapper"> <select id="loadRunList" parameterType="java.util.Map" resultType="com.xm.travel.RunBean"> select * from run </select> <select id="loginUser" parameterType="java.util.Map" resultType="com.xm.travel.LoginBean"> select * from user where name = #{name} and password = #{password} </select> <select id="findUserByName" parameterType="java.util.Map" resultType="com.xm.travel.LoginBean"> select * from user where name = #{name} </select> <insert id="addUser" useGeneratedKeys="true" keyProperty="id" > insert into user(id,name,password) values(#{id},#{name},#{password}) </insert> </mapper>
以上這篇 Java后臺接口開發初步實戰教程就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。