上图是查询列表的接口,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
1
2
3
4
5
6
7
8
9
10
11
12
|
@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差不多
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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需要在配置文件中指定
1
2
3
4
|
public interface loginmapper { public loginbean finduserbyname(map<string,object> map) throws exception; public void adduser(loginbean bean) throws exception; } |
这是对应的loginmapper.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?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后台接口开发初步实战教程就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/diyangxia/article/details/64122387