The above picture is the interface for query list, get method
The above picture is the user registration interface, which is also a get, and the post method is also very simple.
Development Tools: IntelliJ IDEA 2016.3.5
ORM framework: MyBatis
Database: MySql
Server: tomcat7.0
The orm framework used by the company is Hibernate, which feels much easier to use than mybatis. After all, after being tested by so many projects in the company, it is always more reliable than the projects written by mybatis, but the following is the code of mybatis
Register interface method: 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; }The specific registration method is basically the same as Hibernate to obtain the session
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); // Check whether the user exists LoginBean userExist = loginMapper.findUserByName(map); if (userExist != null) { // Cannot register resultBean.setCode("001"); resultBean.setSuccess(false); resultBean.setReason("User already exists"); } else { loginMapper.addUser(bean); session.commit();// Important, be sure to commit, otherwise it cannot insert System.out.println("The current increased user id is: " + bean.getId()); resultBean.setCode("200"); resultBean.setSuccess(true); resultBean.setReason("Register successful"); } } catch (Exception e) { e.printStackTrace(); out.println("Register exception==>" + e.getMessage()); resultBean.setCode("001"); resultBean.setSuccess(false); resultBean.setReason("Register exception"); } finally { session.close(); } return resultBean; }This mapper needs to be specified in the configuration file
public interface LoginMapper { public LoginBean findUserByName(Map<String,Object> map) throws Exception; public void addUser(LoginBean bean) throws Exception;}This is the corresponding 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>The above preliminary practical tutorial on Java backend interface development is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.