IDEA后台服务器+MySQL+双客户端(小程序与Android App)实现数据查询验证添加(三)

实现功能:小程序 或者 Android App作为客户端与idea后台服务器通信,操作数据库进行查询,验证,添加数据。
具体实例,登录,注册,查询用户,权限分级,提交订单。

1.小程序登录和订单页面代码实现
2.Android代码实现

3.idea后台代码+MySQL实现

idea的文件结构
在这里插入图片描述

简单解析一下Idea的文件结构,首先TestsqlApplication是项目的启动类,前面图标有一个绿色的小三角,application-dev.yml
是用来设置连接我们的数据库,UserMapping.xml里是数据库执行语言,接口文件UserMapper调用UserMapping.xml数据库语言,服务层UserService调用UserMapper,然后控制类UserController调用UserService,我们的客户端就是直接访问UserController

UserController
这个是直接与客户端交互的类,注意@RequestMapping后接的/xxx就是客户端要访问的url的后缀,@RequestMapping写在类名上面是上级目录,写在方法上面就是下级目录,如下此例对应的url:http://localhost:8080/test/check

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
@RequestMapping("/test")
@EnableAutoConfiguration
public class UserController {

@Autowired
UserService userService;
    @RequestMapping("/check")
    public String GetUser(Integer id){
    //GetUser的方法获取到客户端传来的id并输出在后台
    System.out.println("id:"+id);
    //返回值调用UserService中的Sel方法将结果转回字符串
        return userService.Sel(id).toString();

    }
    @RequestMapping("/login")

    public int GetLogin(String userName,String passWord){
        System.out.println("注册"+userName+passWord);
        int count=userService.login(userName,passWord);
        return  count;

    }

    @RequestMapping("/register")

    public String GetUser(String userName,String passWord){
        System.out.println("注册"+userName+passWord);
       userService.getUserByLoginName(userName,passWord);
        return "success";
    }


}

DingController

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
@RequestMapping("/ding")
@EnableAutoConfiguration
public class DingController {

    @Autowired
    UserService userService;
    @RequestMapping("/check")
    public String GetUser(Integer id){
        System.out.println("id:"+id);
        if(id==1)
      return userService.check(id).toString();
        else
            return  "Permission denied ";

        //return "ok";
    }

    @RequestMapping("/submit")

    public String GetUser(int time,String address,int tel){
        System.out.println("注册"+time+address+tel);
        userService.submit(time,address,tel);
        return "success";
    }


}

UserMapper

package com.example.testsql.mapper;

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

@Repository
public interface UserMapper {
//Sel的返回值是User实体类与Mapping里的resulttype保持一致
    User Sel(@Param("id") int id);
    void getUserByLoginName(@Param("userName") String userName,@Param("passWord")  String passWord);
    int login(@Param("userName") String userName,@Param("passWord")  String passWord);
    Dingdan check(@Param("id") int id);
    void submit(@Param("time") int time,@Param("address") String address,@Param("tel") int tel);
}

UserServie

package com.example.testsql.service;

import com.example.testsql.entity.Dingdan;
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;
    //Sel返回值和UserMapper里的Sel返回值保持一致
    public User Sel(int id){
        return userMapper.Sel(id);
    }
    public void getUserByLoginName(String userName,String passWord){
      // return userMapper.getUserByLoginName(userName,passWord);
        userMapper.getUserByLoginName(userName,passWord);

    }
    public int login(String userName,String passWord){
        return userMapper.login(userName,passWord);
    }
    public Dingdan check(int id){
        return userMapper.check(id);
    }
    public void submit(int time,String address,int tel){
         userMapper.submit(time,address,tel);
    }
}

两个实体类文件
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 + '\"'
                + "}";
    }
}

Dingdan

package com.example.testsql.entity;

public class Dingdan {
    private Integer id;
    private Integer time;
    private String address;
    private Integer tel;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getTime() {
        return time;
    }

    public void setTime(Integer time) {
        this.time = time;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getTel() {
        return tel;
    }

    public void setTel(Integer tel) {
        this.tel = tel;
    }

    @Override
    public String toString() {
        return "{"
                + "\"id\":"
                + id
                + ",\"Time\":\""
                + time + '\"'
                + ",\"Address\":\""
                + address + '\"'
                + ",\"Tel\":\""
                + tel + '\"'
                + "}";
    }
}

TestsqlApplication

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

<?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>
    <select id="check" resultType="com.example.testsql.entity.Dingdan">
        select * from dingdan where id = #{id}
    </select>
    <insert id="submit" parameterType="com.example.testsql.entity.Dingdan">
        insert into dingdan
         (time,address,tel) values(#{time},#{address},#{tel})
    </insert>

</mapper>

pom.xml,修改后右键找到maven重新reimport一下,注意mysql的版本是否跟我一样,8以上似乎要带版本信息,可以修改成自己的版本号

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example.testsql</groupId>
    <artifactId>testsql</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>testsql</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>8.0.17</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

application-dev.yml,这个文件没有的话自己新建

server:
  port: 8080

spring:
  datasource:
    username: root(数据库用户名)
    password: (自己数据库的密码)
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC&autoReconnect=true&failOverReadOnly=false
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  mapper-locations: classpath:Mapping/UserMapping.xml


#showSql
logging:
  level:
    com:
      example:
        mapper : debug
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值