Java后臺(tái)接口開發(fā)初步實(shí)戰(zhàn)教程

Java后臺(tái)接口開發(fā)初步實(shí)戰(zhàn)教程

長沙一度軟件培訓(xùn)      2022-05-06 02:21:02     76

Java后臺(tái)接口開發(fā)初步實(shí)戰(zhàn)教程,上圖是查詢列表的接口,get方式上圖是用戶注冊(cè)的接口,同樣是get,post方式也很簡單開發(fā)工具:IntelliJ IDEA 2016.3.5ORM框架:M

課程價(jià)格 請(qǐng)咨詢

上課時(shí)段: 授課校區(qū):

詳細(xì)介紹

上圖是查詢列表的接口,get方式

上圖是用戶注冊(cè)的接口,同樣是get,post方式也很簡單

開發(fā)工具:IntelliJ IDEA 2016.3.5

ORM框架:MyBatis

數(shù)據(jù)庫:MySql

服務(wù)器:tomcat7.0

公司使用的的orm框架是Hibernate,使用起來感覺比mybatis好用多了,畢竟經(jīng)過了公司這么多項(xiàng)目的考驗(yàn),總比自己用mybatis寫的項(xiàng)目可靠,但以下分享的還是mybatis的代碼

注冊(cè)接口方法:

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;?}

具體的注冊(cè)方法,獲取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) {????// 存在后無法注冊(cè)????resultBean.setCode("001");????resultBean.setSuccess(false);????resultBean.setReason("用戶已存在");???} else {????loginMapper.addUser(bean);????session.commit();// 重要,一定要commit,否則無法insert????System.out.println("當(dāng)前增加的用戶id為:" + bean.getId());????resultBean.setCode("200");????resultBean.setSuccess(true);????resultBean.setReason("注冊(cè)成功");???}??} catch (Exception e) {???e.printStackTrace();???out.println("注冊(cè)異常==>" + e.getMessage());???resultBean.setCode("001");???resultBean.setSuccess(false);???resultBean.setReason("注冊(cè)異常");??} finally {???session.close();??}??return resultBean;?}

這個(gè)Mapper需要在配置文件中指定

public interface LoginMapper {?public LoginBean findUserByName(Map<String,Object> map) throws Exception;?public void addUser(LoginBean bean) throws Exception;}

這是對(duì)應(yīng)的LoginMapper.xml

xml version="1.0" encoding="UTF-8" ?>??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>

以上就是長沙一度軟件培訓(xùn)java培訓(xùn)機(jī)構(gòu)的小編針對(duì)“Java后臺(tái)接口開發(fā)初步實(shí)戰(zhàn)教程”的內(nèi)容進(jìn)行的回答,希望對(duì)大家有所幫助,如有疑問,請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。

培訓(xùn)啦提醒您:交易時(shí)請(qǐng)核實(shí)對(duì)方資質(zhì),對(duì)于過大宣傳或承諾需謹(jǐn)慎!任何要求預(yù)付定金、匯款等方式均存在風(fēng)險(xiǎn),謹(jǐn)防上當(dāng)。