Springboot+微信小程序

项目描述:在微信小程序中通过与Springboot操作数据库实现登录验证,其中我是用springboot整合mybatis操作数据库
微信小程序代码:`
account.wxml

员工签到 姓名: 密码:

account.js

Page({
  data: {
    username:'',
    password:'',

   
  },

 formSumbit:function (e) {
    console.log("点击提交按钮");
    this.setData({
      username: e.detail.value.username,
      password: e.detail.value.password

    })

  },
  onLoad: function (options) {
    console.log("******");
    var that = this;
    wx.request({
          url:'http://localhost:8080/login', //服务器地址

      menthod: "GET",   
      header: {
        'content-type': 'application/json'
      },
      data: {
        'username':that.data.username,
        'password': that.data.password
      },

    
      success:function (res) {
        console.log(".....回调函数....." +res.data );
        var resData=res.data;
        if(resData==true){
            wx:wx.showToast({
              title: '登录成功',
              duration: 2000,
            })
        }else{
          wx:wx.showToast({
            title: '登录失败',
            duration: 2000,
          })
        }
      
      }  
    })
   
  },
  formRest: function () {
    console.log("点击了重置")
  },
  onReady: function () {
    console.log("onReady")
  },
  onShow: function () {
    console.log("onShow")
  },
  onHide: function () {
    console.log("onHide")
  },
  onUnload: function () {
    console.log("onUnload")
  },
  onPullDownRefresh: function () {
    console.log("onPullDownRefresh")
  },
  onReachBottom: function () {
    console.log("onReachBottom")
  },
  onShareAppMessage: function () {
    console.log("onShareAppMessage")
  }

 
 
})

account.wxss



.text{
    width: 100%;
      height: 60rpx;    
    text-align: center;
    font-size: 40rpx;
    margin-top: 40rpx;
    font-weight: 600;
}
.input{
    width: 100%;
    height: 60rpx;
    font-size: 40rpx;
    margin-top: 40rpx;
    font-weight: 600;
}
.text1{
    width: 20%;
    float: left;
    margin-left: 30rpx;
}
.input1{
      width: 50%;
     height: 60rpx;
    border:1rpx solid #000;
}
.btn{
     width: 100%;
      height: 60rpx;    
    font-size: 50rpx;
    margin-top: 40rpx;
}


.btn1{  
    margin-left: 20%;
    float: left;
    color: white;
    background: cornflowerblue;

}


.btn2{  
    margin-left: 20%;
    color: white;
    background: cornflowerblue;

}

我使用eclipse安装一个springboot的插件,也可以使用idea,都一样,下面我以eclipse为例
安装插件这里我就不写了百度有很多教程,下面这个链接里面也有
https://www.cnblogs.com/zjdxr-up/p/8617242.html

springboot代码:
新建一个springboot项目file>new >Spring Starter Project
如果没找到Spring Starter Project可以去Other中搜索
Springboot+微信小程序_第1张图片
pom.xml



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.4.RELEASE
		 
	
	com.example
	demo-1
	0.0.1-SNAPSHOT
	demo-1
	Demo project for Spring Boot

	
		1.8
		1.3.0
		1.0.28
	

	
		
			org.springframework.boot
			spring-boot-starter
		
		 
            org.springframework.boot
            spring-boot-starter-web
        
		
		
			mysql
			mysql-connector-java
			5.1.34
		

		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			 1.3.0  
		
		
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



Demo1Application.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication
public class Demo1Application {

	public static void main(String[] args) {
		SpringApplication.run(Demo1Application.class, args);
	}

}

配置文件
application.properties

spring.datasource.url = jdbc:mysql://localhost:3306/ssm
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.jdbc.Driver

通过逆向工程生成mapper与mapper.xml和实体类

点击提取逆向工程项目+sql文件 提取码:9eze(自己新建一个数据库然后在运行sql文件,我的数据名为ssm,如果不知道怎么去改逆向工程代码,数据库名就命名跟我一样,就不用改了)
下面就是改数据库名,msyql用户名跟密码的地方
Springboot+微信小程序_第2张图片
下面我写UserMapper.xml与UserMapper.java的原因是因为我要通过用户名查找用户,这个方法逆向工程没有自动生成,所以要自己加,下面我备注有注释
UserMapper.java

package com.example.demo.dao;

import com.example.demo.pojo.User;
import com.example.demo.pojo.UserExample;
import com.example.demo.pojo.UserKey;
import java.util.List;

import org.apache.ibatis.annotations.Mapper;
/*import org.apache.ibatis.annotations.Param;*/

@Mapper
public interface UserMapper {
    int countByExample(UserExample example);

    int deleteByExample(UserExample example);

    int deleteByPrimaryKey(UserKey key);

    int insert(User record);
    int insertSelective(User record);
    // 通过用户名查找用户。自己加的
    User selectUser(String username);

    List selectByExample(UserExample example);

    User selectByPrimaryKey(int key);

    /*int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);

    int updateByExample(@Param("record") User record, @Param("example") UserExample example);
*/
    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

UserMapper.xml




  
    
    
    
  
  
    
      
        
          
            
              
                
                  and ${criterion.condition}
                
                
                  and ${criterion.condition} #{criterion.value}
                
                
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                
                
                  and ${criterion.condition}
                  
                    #{listItem}
                  
                
              
            
          
        
      
    
  
  
    
      
        
          
            
              
                
                  and ${criterion.condition}
                
                
                  and ${criterion.condition} #{criterion.value}
                
                
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                
                
                  and ${criterion.condition}
                  
                    #{listItem}
                  
                
              
            
          
        
      
    
  
  
    id, username, password
  
  
  
  
  
    
   
  
  
  
    delete from user
    where id = #{id,jdbcType=INTEGER}
      and username = #{username,jdbcType=VARCHAR}
  
  
    delete from user
    
      
    
  
  
    insert into user (id, username, password
      )
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
      )
  
  
    insert into user
    
      
        id,
      
      
        username,
      
      
        password,
      
    
    
      
        #{id,jdbcType=INTEGER},
      
      
        #{username,jdbcType=VARCHAR},
      
      
        #{password,jdbcType=VARCHAR},
      
    
  
  
  
    update user
    
      
        id = #{record.id,jdbcType=INTEGER},
      
      
        username = #{record.username,jdbcType=VARCHAR},
      
      
        password = #{record.password,jdbcType=VARCHAR},
      
    
    
      
    
  
  
    update user
    set id = #{record.id,jdbcType=INTEGER},
      username = #{record.username,jdbcType=VARCHAR},
      password = #{record.password,jdbcType=VARCHAR}
    
      
    
  
  
    update user
    
      
        password = #{password,jdbcType=VARCHAR},
      
    
    where id = #{id,jdbcType=INTEGER}
      and username = #{username,jdbcType=VARCHAR}
  
  
    update user
    set password = #{password,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
      and username = #{username,jdbcType=VARCHAR}
  

实体层就不用改了,直接用逆向工程生成的,我就省略不写了

服务层:
UserService.java

package com.example.demo.service;

import com.example.demo.pojo.User;

public interface UserService {
	User selectUser(String username);
	User selectByPrimaryKey(int id);
}

UserServiceImpl.java

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.dao.UserMapper;
import com.example.demo.pojo.User;

@Service
public class UserServiceImpl implements UserService {

	@Autowired(required=false)
	private  UserMapper userMapper;
	
	@Override
	public User selectUser(String username) {
		System.out.println("-------");
		// TODO Auto-generated method stub
		
		//根据用户名查找用户信息
		return userMapper.selectUser(username);
	}

	@Override
	public User selectByPrimaryKey(int id) {
		// TODO Auto-generated method stub
		
		User user=userMapper.selectByPrimaryKey(1);
		return user;
	}

}

控制层:
UserController.java

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.pojo.User;
import com.example.demo.service.UserService;

@RestController
public class UserController {
	@Autowired
	private UserService userService;
	
	

	
	@RequestMapping("/login")
	   public  boolean login(String username, String password){
		System.out.println ( "微信小程序调用接口!!!用户名:" + username + "密码:" + password );
		User user=userService.selectUser(username);
		if(user!=null) {
			String password1=user.getPassword();
			if(password1.equals(password)) {
				System.out.println("登录成功");
				return true;
			}else {
				System.out.println("密码错误"); 
				return false;
			}
			
		}else {
			System.out.println("用户名不存在");
			return false;
		}
	
		}	
	
	
	@RequestMapping("/selectById")
	public User selectById(int id) {
	
		System.out.println("*******");
		User user=userService.selectByPrimaryKey(id);
		
		return user;
	}
	

	
}

项目结构:
Springboot+微信小程序_第3张图片
点击运行Demo1Application.java文件
如下图:
Springboot+微信小程序_第4张图片
然后到微信开发者工具account.wxml中点击登录
微信开发者工具控制台:
Springboot+微信小程序_第5张图片
eclipse控制台:
Springboot+微信小程序_第6张图片
到此项目能运行成功
如果有问题欢迎评论,我看到会第一时间回复
如果帮助到了你,请帮忙点一下赞,谢谢支持

你可能感兴趣的:(微信小程序)