Java background interface development preliminary practical tutorial, java background practical tutorial

Source: Internet
Author: User

Java background interface development preliminary practical tutorial, java background practical tutorial

Is the interface for querying the list, get Method

Is the user-registered interface, the same is get, post method is also very simple

Development Tool: IntelliJ IDEA 2016.3.5

ORM framework: MyBatis

Database: MySql

Server: tomcat7.0

The orm framework used by the company is Hibernate. It feels much easier to use than mybatis. After all, it has been tested by the company for so many purposes and is always more reliable than the project written by myself using mybatis, however, the following is the mybatis code.

Registration 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 similar to that of Hibernate in obtaining sessions.

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 (usern Ame); bean. setPassword (password); // query whether the user has LoginBean userExist = loginMapper. findUserByName (map); if (userExist! = Null) {// The resultBean cannot be registered if it exists. setCode ("001"); resultBean. setSuccess (false); resultBean. setReason ("User already exists");} else {loginMapper. addUser (bean); session. commit (); // important. Make sure to use commit. Otherwise, you cannot insert System. out. println ("the added user ID is:" + bean. getId (); resultBean. setCode ("200"); resultBean. setSuccess (true); resultBean. setReason ("registered successfully") ;}} catch (Exception e) {e. printStackTrace (); out. println ("registration exception ==>" + e. getMessage (); resultBean. setCode ("001"); resultBean. setSuccess (false); resultBean. setReason ("registration exception");} finally {session. close () ;}return resultBean ;}

This Mapper must 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 is a preliminary practical tutorial on Java background interface development. I hope you can give me a reference and support me a lot.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.