小程序访问idea后台数据库执行查询,登录,注册功能

实现功能:小程序访问后台服务器实现通过ID查询数据并反馈,登录校验,注册添加数据功能

开发工具:微信开发工具,IDEA,Mysql

微信开发上的代码比较简单,先写Idea后台的,
上文件结构
小程序访问idea后台数据库执行查询,登录,注册功能_第1张图片
从上至下,UserController

package com.example.testsql.controller;

import com.example.testsql.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@EnableAutoConfiguration
public class UserController {

@Autowired
UserService userService;
//@RequestMapping后接得是访问url的地址,在客户端也就是小程序端访问url=http://localhost:8080/login就是查询,访问url=http://localhost:8080/getlogin就是登录,访问url=http://localhost:8080/getUser就是注册,
    @RequestMapping("/login")
    public String GetUser(Integer id){
    System.out.println("id:"+id);
        return userService.Sel(id).toString();
    }
    @RequestMapping("/getlogin")
    public int GetLogin(String userName,String passWord){
        System.out.println("注册"+userName+passWord);
        int count=userService.login(userName,passWord);
        return  count;
    }
    @RequestMapping("/getUser")
    public String GetUser(String userName,String passWord){
        System.out.println("注册"+userName+passWord);
       userService.getUserByLoginName(userName,passWord);
        return "success";
    }
}

User

package com.example.testsql.entity;

public class User {
    private Integer id;
    private String userName;
    private String passWord;
    private String realName;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassWord() {
        return passWord;
    }
    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }
    public String getRealName() {
        return realName;
    }
    public void setRealName(String realName) {
        this.realName = realName;
    }
    @Override
    public String toString() {
        return "{"
                + "\"id\":"
                + id
                + ",\"userName\":\""
                + userName + '\"'
                + ",\"passWord\":\""
                + passWord + '\"'
                + ",\"realName\":\""
                + realName + '\"'
                + "}";
    }
}

UserMapper

package com.example.testsql.mapper;

import com.example.testsql.entity.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper {

    User Sel(@Param("id") int id);
    User getUserByLoginName(@Param("userName") String userName,@Param("passWord")  String password);
    int login(@Param("userName") String userName,@Param("passWord")  String password);
}

UserService

package com.example.testsql.service;

import com.example.testsql.entity.User;
import com.example.testsql.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    public User Sel(int id){
        return userMapper.Sel(id);
    }
    public User getUserByLoginName(String userName,String passWord){
        return userMapper.getUserByLoginName(userName,passWord);
    }
    public int login(String userName,String passWord){
        return userMapper.login(userName,passWord);
    }
}

TestsqlAppliation

package com.example.testsql;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;

@MapperScan("com.example.testsql.mapper")
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class TestsqlApplication{
    public static void main(String[] args) throws Exception {
        System.out.println(("success"));
        SpringApplication.run(TestsqlApplication.class, args);
    }

}

UserMapping

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.testsql.mapper.UserMapper">

    <resultMap id="BaseResultMap" type="com.example.testsql.entity.User">
        <result column="id" jdbcType="INTEGER" property="id" />
        <result column="userName" jdbcType="VARCHAR" property="userName" />
        <result column="passWord" jdbcType="VARCHAR" property="passWord" />
        <result column="realName" jdbcType="VARCHAR" property="realName" />
    </resultMap>

    <select id="Sel" resultType="com.example.testsql.entity.User">
        select * from user where id = #{id}
    </select>
    <select id="login" resultType="java.lang.Integer">
        select  count(id) from user where userName = #{userName} and passWord=#{passWord}
    </select>
    <insert id="getUserByLoginName" parameterType="com.example.testsql.entity.User">
        insert into user
         (userName,passWord) values(#{userName},#{passWord})
    </insert>


</mapper>

你可能感兴趣的:(微信小程序,intellij-idea,小程序,数据库)