小程序项目访问idea后台数据库进行查询,添加数据

先看功能实现图,小程序实现通过id查询该条信息的id,username,password
在这里插入图片描述
使用该条记录的tom,123登陆成功的话跳转
在这里插入图片描述

小程序的代码,index.js
//index.js

const app = getApp()
Page({
  //定义全局变量data
  data: {
    username: "",
    password: "",
    message:"",
    id:"",
    list:{}
  },
  
   
   usernameInput:function(e){
    var username = e.detail.value;//从页面获取到用户输入的用户名/邮箱/手机号
    if (username != ''){
      this.setData({ username: username });//把获取到的密码赋值给全局变量Date中的password
    }
  },
  
  passwordInput:function(e){
      var pwd = e.detail.value;//从页面获取到用户输入的密码
      if (pwd != ''){
        this.setData({ password: pwd });//把获取到的密码赋值给全局变量Date中的password
      }
  },
  idInput:function(e){
    var id = e.detail.value;//从页面获取到用户输入的用户名/邮箱/手机号
    if (id != ''){
      this.setData({ id: id });//把获取到的密码赋值给全局变量Date中的password
    }
  },
  //处理check的触发事件
  check: function (e) {
    wx.request({
    //ip是运行idea项目的电脑cmd中ipconfig得到的ip地址,本网,test是
    //idea项目中UserController中Mapping的后缀,check是再下一级方法的后缀
      url:'http://ip:8080/test/check',
      //定义传到后台的数据
      data: {
        //需要传送给服务器的数据,冒号前面的名字需要和idea里的设置一致,注意idea里username的n是大写改一下传送值
        userName: this.data.username,
        passWord: this.data.password,
        id:this.data.id
      },
      method: 'get',//定义传到后台接受的是post方法还是get方法
      header: {
        'content-type': 'application/json' // 默认值
        //'content-type': 'text/html'
      },
      success: (res => {
        console.log("我的查询结果", res.data),
     
        this.setData({
         list: res.data
        })
     
    
          
      })
 
})
  },
  jump:function(){
    console.log("我的jump"),
  
    wx.navigateTo({
 
      url: '../ding/ding'
       
      })
  },
  //处理login的触发事件
  login: function (e) {
    wx.request({
      url:'http://ip:8080/test/login',
      //定义传到后台的数据
      data: {
        //从全局变量data中获取数据,注意idea里username的n是大写改一下传送值
        userName: this.data.username,
        passWord: this.data.password,
        id:this.data.id
      },
      method: 'get',//定义传到后台接受的是post方法还是get方法
      header: {
        'content-type': 'application/json' // 默认值
        //'content-type': 'text/html'
      },
      success: (res => {
        console.log("我的登陆结果", res.data),
        
        this.setData({
         list: res.data
         
        })
       if(res.data>0)
        this.jump()
      })
 
})
  },
  //处理regist的触发事件
  register: function (e) {
    wx.request({
      url:'http://ip:8080/test/register',
      //定义传到后台的数据
      data: {
        //从全局变量data中获取数据,注意idea里username的n是大写改一下传送值
        userName: this.data.username,
        passWord: this.data.password,
        id:this.data.id
      },
      method: 'get',//定义传到后台接受的是post方法还是get方法
      header: {
        'content-type': 'application/json' // 默认值
        //'content-type': 'text/html'
      },
      success: (res => {
        console.log("我的注册结果", res.data),
        
        this.setData({
         list: res.data
         
        })
      })
 
})
  }
})

index.wxml

<view>账号</view>
    <view ><input bindinput="usernameInput" placeholder="用户名/邮箱/手机号" placeholder-style="color:#999999;"/></view>

    <view >密码</view>
    <view ><input bindblur="passwordInput" placeholder="请输入密码" password placeholder-style="color:#999999;"/></view>
    <view >ID</view>
    <view ><input bindblur="idInput" placeholder="请输入密码" password placeholder-style="color:#999999;"/></view>
  <button class="btn" bindtap='check' type="primary">查询</button>
  <button class="btn" bindtap='login' type="primary">登录</button>
  <button class="btn" bindtap='register' type="primary">注册</button>
	
	
      <text>ID:{{list.id}}</text>
      <text>USERNAME:{{list.userName}}</text>
      <text>PASSWORD:{{list.passWord}}</text>
   

idea项目
文件结构如下
在这里插入图片描述
按顺序,controller里的两个文件用于用户名的只需要
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
//下面这个/test是小程序里的url
@RequestMapping("/test")
@EnableAutoConfiguration
public class UserController {
//下面三个不同的方法,对应小程序里不同功能访问的url:ip/test/check还是ip/test/login
@Autowired
UserService userService;
    @RequestMapping("/check")
    public String GetUser(Integer id){
    System.out.println("id:"+id);
        return userService.Sel(id).toString();
//获取传送过来的id,然后调用userService中的Sel方法,将id作为参数传送
    }
    @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";
    }
}

接下来entity也是只需要使用这个实体类
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 + '\"'
                + "}";
    }
}

mapper下的UserMapper,用于连接数据库,只校验用户名的话,只用前面三个方法就够了dingdan的不用,注意参数的数据类型和返回值

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 {

    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);
}

service下的UserService

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;
    public User Sel(int id){
        return userMapper.Sel(id);
        //调用Usermapper里的Sel函数
    }
    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);
    }
}

启动类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);
    }

}

Mapping下的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>
    <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>

application-dev设置数据库信息

application-dev
server:
  port: 8080

spring:
  datasource:
    username: root
    password: 123456
    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

pom

<?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>

对于idea项目直接下载导入的问题,我说说自己换了电脑遇到的报错信息,首先自己电脑上安装过mysql和java吗,我先按照项目里的版本信息,装了mysql(安装参考:https://blog.csdn.net/qq_41080850/article/details/97495643),并且和idea连接(参考文章:https://blog.csdn.net/Royalic/article/details/119604763),此处还是会显示mysql的connect报错,我也将connect的文件拷贝在了mysql的文件夹隔壁,总之此时,我reload了pom,根据软件自己的提示下载了一些莫名其妙的东西导致报红又全部消失了。。。。
idea的项目功能:启动类TestsqlApplication,数据访问是由UserController,到UserService,到UserMappe,再到数据库rUserMapping,其他基本上是配置文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值