作者:浮云騎士LIN
cnblogs.com/ckfeng/p/12812214.html
項(xiàng)目描述:在微信小程序中通過與Springboot操作數(shù)據(jù)庫實(shí)現(xiàn)簡單的增刪改查,其中我是用springboot整合mybatis-plus 和mysql使用的
1. 開發(fā)前準(zhǔn)備
1.1 前置知識(shí)
java基礎(chǔ)
SpringBoot簡單基礎(chǔ)知識(shí)
1.2 環(huán)境參數(shù)
開發(fā)工具:IDEA
基礎(chǔ)環(huán)境:Maven+JDK8
主要技術(shù):SpringBoot、lombok、mybatis-plus、mysql 、微信小程序
SpringBoot版本:2.2.6
2.開發(fā)者服務(wù)器
項(xiàng)目結(jié)構(gòu):
2.1 初始配置
(1)pom.xml配置
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.1
<!--模板引擎-->
org.springframework.boot
spring-boot-starter-thymeleaf
<!--?引入阿里數(shù)據(jù)庫連接池?-->
com.alibaba
druid
1.1.14
<!--?mysql依賴-->
mysql
mysql-connector-java
5.1.42
runtime
<!--?mybatisPlus?核心庫?-->
com.baomidou
mybatis-plus-boot-starter
3.1.0
<!--生成實(shí)體成get?set-->
org.projectlombok
lombok
true
<!--?pagehelper?分頁插件?-->
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
<!--junit?測試-->
junit
junit
test
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-maven-plugin
(2)application.yml
#?Spring?Boot?的數(shù)據(jù)源配置
spring:
datasource:
name:?wx
url:?jdbc:mysql://localhost:3306/wx_mini_program?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username:?root
password:?root
#?使用druid數(shù)據(jù)源
type:?com.alibaba.druid.pool.DruidDataSource
driver-class-name:?com.mysql.jdbc.Driver
filters:?stat
maxActive:?20?initialSize:?1?maxWait:?60000?minIdle:?1?timeBetweenEvictionRunsMillis:?60000?minEvictableIdleTimeMillis:?300000?validationQuery:?select?'x'?testWhileIdle:?true?testOnBorrow:?false?testOnReturn:?false?poolPreparedStatements:?true?maxPoolPreparedStatementPerConnectionSize:?20?maxOpenPreparedStatements:?20?#?mybatis-plus相關(guān)配置
mybatis-plus:
#?xml掃描,多個(gè)目錄用逗號(hào)或者分號(hào)分隔(告訴?Mapper?所對(duì)應(yīng)的?XML?文件位置)
mapper-locations:?classpath:mapper/*.xml
#?以下配置均有默認(rèn)值,可以不設(shè)置
global-config:
db-config:
#主鍵類型?AUTO:"數(shù)據(jù)庫ID自增"?INPUT:"用戶輸入ID",ID_WORKER:"全局唯一ID?(數(shù)字類型唯一ID)",?UUID:"全局唯一ID?UUID";
id-type:?auto
#字段策略?IGNORED:"忽略判斷"??NOT_NULL:"非?NULL?判斷")??NOT_EMPTY:"非空判斷"
field-strategy:?NOT_EMPTY
#數(shù)據(jù)庫類型
db-type:?MYSQL
#?指定實(shí)體類的包
type-aliases-package:?com.ckf.login_wx.entity
configuration:
#?是否開啟自動(dòng)駝峰命名規(guī)則映射:從數(shù)據(jù)庫列名到Java屬性駝峰命名的類似映射
map-underscore-to-camel-case:?true
#?如果查詢結(jié)果中包含空值的列,則?MyBatis?在映射的時(shí)候,不會(huì)映射這個(gè)字段
call-setters-on-nulls:?true
#?這個(gè)配置會(huì)將執(zhí)行的sql打印出來,在開發(fā)或測試的時(shí)候可以用
log-impl:?org.apache.ibatis.logging.stdout.StdOutImpl
#?PageHelper分頁插件
pagehelper:
helperDialect:?mysql
reasonable:?true
supportMethodsArguments:?true
params:?count=countSql
2.2 小程序用戶表
CREATEtableusers(
idintnotnullPRIMARYkeyauto_increment,
namevarchar(255)notnull,
ageintnotnull);
insertintousersvalue(null,'陳克鋒',18);
insertintousersvalue(null,'陳克帥',11);
insertintousersvalue(null,'陳克兵',14);select*fromusers;
2.3 pojo
2.4 mapper
2.5 service
2.5 serviceImpl
配置SpringBoot掃描mapper
2.6 controller
LoginController
packagecom.ckf.login_wx.controller;
importorg.springframework.web.bind.annotation.PostMapping;
importorg.springframework.web.bind.annotation.RestController;
importjava.util.HashMap;
importjava.util.Map;/**
*@author安詳?shù)目喽〔?/p>
*@date2020/4/30?11:46?*/
@RestControllerpublicclassLoginController{/**
*?登錄
*@paramphone
*@parampassword
*@return*/
@PostMapping("/doLogin")publicMapdoLogin(String?phone,?String?password){
Map?map?=newHashMap();if((phone.equals("10086")&&?password.equals("123456"))){
map.put("code",200);
map.put("result","登錄成功");
System.out.println("登錄成功");
}else{
map.put("result","no");
}returnmap;
}
}
UserController
packagecom.ckf.login_wx.controller;
importcom.ckf.login_wx.entity.User;
importcom.ckf.login_wx.servic.UserService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.web.bind.annotation.*;/**
*@author安詳?shù)目喽〔?/p>
*@date2020/4/30?13:39?*/
@RestController
@RequestMapping("/test")publicclassUserController{
@AutowiredprivateUserService?userService;/**
*?查詢?nèi)?/p>
*@return*/
@GetMapping("/list")publicObjectlist(){
System.out.println("查詢成功");returnuserService.list();
}/**
*?根據(jù)id刪除
*@paramid
*@return*/
@GetMapping("/delete")publicbooleandelete(Integer?id){
System.out.println("刪除成功");returnuserService.removeById(id);
}/**
*??根據(jù)id查詢
*@paramid
*@return*/
@GetMapping("/byid")publicObjectbyid(Integer?id){
System.out.println("查詢成功");returnuserService.getById(id);
}/**
*??修改
*@paramuser
*@return*/
@PostMapping("/update")publicbooleanupdate(@RequestBody?User?user){
System.out.println("修改成功");returnuserService.updateById(user);
}/**
*?添加
*@paramuser
*@return*/
@PostMapping("/add")publicbooleanadd(@RequestBody?User?user){
System.out.println("添加成功");returnuserService.save(user);
}
}
3. 微信小程序
項(xiàng)目結(jié)構(gòu):
3.1 初始配置
3.2 bing.wxml
<!--pages/bind/bind.wxml-->
<!--賬號(hào)-->
賬號(hào)
<!--密碼-->
密碼
<!--按鈕-->
登錄
微信登錄
?-->
3.3 bing.js
3.3 list.wxml
<!--pages/list/list.wxml-->
添加
編號(hào)
姓名
年齡
操作
{{item.id}}
{{item.name}}
{{item.age}}
編輯|
刪除
3.4 list.js
//?pages/list/list.js
Page({/**
*?頁面的初始數(shù)據(jù)?*/
data:?{
list:[]
},/**
*?生命周期函數(shù)--監(jiān)聽頁面加載?*/
onLoad:function(options){
},/**
*?生命周期函數(shù)--監(jiān)聽頁面初次渲染完成?*/
onReady:function(){
},/**
*?生命周期函數(shù)--監(jiān)聽頁面顯示?*/
onShow:function(){varthat=this;
wx.request({
url:'http://localhost:8080/test/list',
method:'GET',
data:{},
success:function(res){varlist=res.data;if(list==null){vartoastText='獲取數(shù)據(jù)失敗';
wx.showToast({
title:?toastText,
icon:'',
duration:2000//彈出時(shí)間
})
}else{
that.setData({
list:list
})
}
}
})
},/**
*?生命周期函數(shù)--監(jiān)聽頁面隱藏?*/
onHide:function(){
},/**
*?生命周期函數(shù)--監(jiān)聽頁面卸載?*/
onUnload:function(){
},/**
*?頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動(dòng)作?*/
onPullDownRefresh:function(){
},/**
*?頁面上拉觸底事件的處理函數(shù)?*/
onReachBottom:function(){
},/**
*?用戶點(diǎn)擊右上角分享?*/
onShareAppMessage:function(){
},
addArea:function(){
wx.navigateTo({
url:'../operation/operation'})
},
deleteArea:function(e){varthat=this;
wx.showModal({
title:'提示',
content:'確定要?jiǎng)h除['+?e.target.dataset.areaname?+']嗎?',
success:function(sm){if(sm.confirm){
wx.request({
url:'http://localhost:8080/test/delete',
data:?{id:?e.target.dataset.areaid},
method:'GET',
success:function(res){varresult=res.statusCode;vartoastText="刪除成功";if(result!=200){
toastText?="刪除失敗";
}else{
that.data.list.splice(e.target.dataset.index,1);
that.setData({
list:that.data.list
});
}
wx.showToast({
title:?toastText,
icon:'',
duration:2000});
}
})
}
}
})
}
})
3.5 app.json
{"pages":?["pages/bind/bind","pages/list/list","pages/logs/logs","pages/operation/operation","pages/index/index"],"window":?{"backgroundColor":"#F6F6F6","backgroundTextStyle":"light","navigationBarBackgroundColor":"#29d","navigationBarTitleText":"login","navigationBarTextStyle":"black"},"sitemapLocation":"sitemap.json","style":"v2"}
4. 測試
啟動(dòng)開發(fā)者服務(wù)器,啟動(dòng)SpringBoot的main方法。
打開微信小程序開發(fā)者工具
登錄頁面
首頁
添加頁面
修改頁面
刪除
到處基本的增刪改查操作已經(jīng)完成了
如有需要前往 Gitee(碼云)下載
前臺(tái):https://gitee.com/ckfeng/applet_of_wechat.git
后臺(tái):https://gitee.com/ckfeng/wx_login.git