@[TOC]maven项目实现微信小程序获取数据库数据 参考自 点击链接跳转
**
实现功能:小程序登录界面,通过修改SQL语句,可实现登录,查询,注册用户
IDE使用到的开发工具:小程序开发工具,Idea,Mysql
1、创建maven项目**
单击 next -> next -> finish
2、文件结构展示
3、各文件功能和详情
controller 实现的是网页数据功能
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("/login")
public String GetUser(Integer id) {
System.out.println("id:"+id);
return userService.Sel(id).toString();
}
}
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 是映射接口文件
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);
}
service 是服务层
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);
}
}
testsql 是启动类
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);
}
}
application 是启动配置文件
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
application.yml
spring:
profiles:
active: dev
4、数据库准备
5、小程序代码
index.js
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
}
},
login: function (e) { //处理login的触发事件
wx.request({
//url: 'http://localhost:8080/API/login',//后面详细介绍
//url:'http://192.168.0.64:8080/HttpDemo/MyServlet',
url:'http://localhost:8080/login',
// url:'https://p.3.cn/prices/mgets?skuIds=J_10026711061553',
// url:'http://192.168.0.25:8080/helloWorld1/HelloServlet',
//定义传到后台的数据
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
})
})
})
}
})
6、传递成功