赞
踩
项目描述:在微信小程序中通过与Springboot操作数据库实现登录验证,其中我是用springboot整合mybatis操作数据库(由于是很久之前的帖子了,暂时找不到完整的项目代码了,不过下面贴的代码并没有缺少)
微信小程序代码:`
account.wxml
<form bindsubmit="formSumbit" bindreset="formRest"> <view> <view class="text">员工签到</view> <view class="input"> <view class="text1">姓名:</view> <input class="input1" type='text' name="username" value="admin"/> </view> <view class="input"> <view class="text1">密码:</view> <input class="input1" password="true" name="password" value="123456" /> </view> </view> <view class="btn"> <button class="btn1" size="mini" form-type="reset">清除</button> <button class="btn2" size="mini" form-type="submit" bindtap="onLoad">登录</button> </view> </form>
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中搜索
pom.xml
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo-1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-1</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version> <druid.version>1.0.28</druid.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</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>
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用户名跟密码的地方
下面我写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<User> 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
<?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.demo.dao.UserMapper" > <resultMap id="BaseResultMap" type="com.example.demo.pojo.User" > <id column="id" property="id" jdbcType="INTEGER" /> <id column="username" property="username" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> </resultMap> <sql id="Example_Where_Clause" > <where > <foreach collection="oredCriteria" item="criteria" separator="or" > <if test="criteria.valid" > <trim prefix="(" suffix=")" prefixOverrides="and" > <foreach collection="criteria.criteria" item="criterion" > <choose > <when test="criterion.noValue" > and ${criterion.condition} </when> <when test="criterion.singleValue" > and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue" > and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue" > and ${criterion.condition} <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," > #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Update_By_Example_Where_Clause" > <where > <foreach collection="example.oredCriteria" item="criteria" separator="or" > <if test="criteria.valid" > <trim prefix="(" suffix=")" prefixOverrides="and" > <foreach collection="criteria.criteria" item="criterion" > <choose > <when test="criterion.noValue" > and ${criterion.condition} </when> <when test="criterion.singleValue" > and ${criterion.condition} #{criterion.value} </when> <when test="criterion.betweenValue" > and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} </when> <when test="criterion.listValue" > and ${criterion.condition} <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," > #{listItem} </foreach> </when> </choose> </foreach> </trim> </if> </foreach> </where> </sql> <sql id="Base_Column_List" > id, username, password </sql> <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.example.demo.pojo.UserExample" > select <if test="distinct" > distinct </if> <include refid="Base_Column_List" /> from user <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> <if test="orderByClause != null" > order by ${orderByClause} </if> </select> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="com.example.demo.pojo.UserKey" > select <include refid="Base_Column_List" /> from user where id = #{id,jdbcType=INTEGER} </select> <!-- 通过用户名查找用户。自己加的 --> <select id="selectUser" parameterType="com.example.demo.pojo.User" resultMap="BaseResultMap"> select * from user where username=#{username} </select> <delete id="deleteByPrimaryKey" parameterType="com.example.demo.pojo.UserKey" > delete from user where id = #{id,jdbcType=INTEGER} and username = #{username,jdbcType=VARCHAR} </delete> <delete id="deleteByExample" parameterType="com.example.demo.pojo.UserExample" > delete from user <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> </delete> <insert id="insert" parameterType="com.example.demo.pojo.User" > insert into user (id, username, password ) values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR} ) </insert> <insert id="insertSelective" parameterType="com.example.demo.pojo.User" > insert into user <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="username != null" > username, </if> <if test="password != null" > password, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="username != null" > #{username,jdbcType=VARCHAR}, </if> <if test="password != null" > #{password,jdbcType=VARCHAR}, </if> </trim> </insert> <select id="countByExample" parameterType="com.example.demo.pojo.UserExample" resultType="java.lang.Integer" > select count(*) from user <if test="_parameter != null" > <include refid="Example_Where_Clause" /> </if> </select> <update id="updateByExampleSelective" parameterType="map" > update user <set > <if test="record.id != null" > id = #{record.id,jdbcType=INTEGER}, </if> <if test="record.username != null" > username = #{record.username,jdbcType=VARCHAR}, </if> <if test="record.password != null" > password = #{record.password,jdbcType=VARCHAR}, </if> </set> <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByExample" parameterType="map" > update user set id = #{record.id,jdbcType=INTEGER}, username = #{record.username,jdbcType=VARCHAR}, password = #{record.password,jdbcType=VARCHAR} <if test="_parameter != null" > <include refid="Update_By_Example_Where_Clause" /> </if> </update> <update id="updateByPrimaryKeySelective" parameterType="com.example.demo.pojo.User" > update user <set > <if test="password != null" > password = #{password,jdbcType=VARCHAR}, </if> </set> where id = #{id,jdbcType=INTEGER} and username = #{username,jdbcType=VARCHAR} </update> <update id="updateByPrimaryKey" parameterType="com.example.demo.pojo.User" > update user set password = #{password,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} and username = #{username,jdbcType=VARCHAR} </update> </mapper>
实体层就不用改了,直接用逆向工程生成的,我就省略不写了
服务层:
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; } }
项目结构:
点击运行Demo1Application.java文件
如下图:
然后到微信开发者工具account.wxml中点击登录
微信开发者工具控制台:
eclipse控制台:
到此项目能运行成功
如果有问题欢迎评论,我看到会第一时间回复(完整项目由于后面我添加了很多功能点整理不出来了,上面代码没有缺少能够正常实现登录功能)
如果帮助到了你,请帮忙点一下赞,谢谢支持
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。