昨天粗粗的写了下后台数据传到微信小程序显示,用来熟悉这个过程,适合刚入门学习案例:

需了解的技术:javaSE,C3p0,jdbcTemplate,fastjson,html,javaScript,css;

需要安装的软件及环境:jdk8,mysql,Navicat for mysql,idea,tomcat,微信开发工具(https://developers.weixin.qq.com/miniprogram/dev/index.html);

项目结构如下:

项目步骤及代码演示:

1. idea(jdk8,tomcat8安装好) 创建javaWeb项目 File->New->Project...->Java Enterpise->next->选中Create project from template,然后next->Project name创建项目名 wechat_route,finish项目建好了。然后在src目录下建几个包com.hcz.bean,com.hcz.dao,com.hcz.dao.daoImpl,com.hcz.service,com.hcz.service.serviceImpl,com.hcz.servlet,com.hcz.utils;

2.创建好这个项目需要的所有模板信息。

在web/WEB-INF下面创建一个叫lib的文件夹,把所需的jar包拷贝到这里来,然后选中所有这些jar包点击右键,有个Add as Library..点击jar包变成下面这样即可;

我的数据库中表创建是这样的

我的数据库用户名和密码都是root,然后创表语句如下:

  1. SET FOREIGN_KEY_CHECKS=0;
  2.  
  3. -- ----------------------------
  4. -- Table structure for t_route
  5. -- ----------------------------
  6. DROP TABLE IF EXISTS `t_route`;
  7. CREATE TABLE `t_route` (
  8. `id` int(11) NOT NULL,
  9. `title` varchar(50) DEFAULT NULL,
  10. `date` varchar(20) DEFAULT NULL,
  11. `routeImg` varchar(30) DEFAULT NULL,
  12. `routeIntroduce` varchar(500) DEFAULT NULL,
  13. `count` int(5) DEFAULT NULL,
  14. PRIMARY KEY (`id`)
  15. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  16.  
  17. -- ----------------------------
  18. -- Records of t_route
  19. -- ----------------------------
  20. INSERT INTO `t_route` VALUES ('1', '风景秀丽,集日月之精华,看一眼神清气爽,再看一眼快乐齐天,岂不美哉!', '2019-04-23 16:40', 'title_pic.jpg', '古老的小镇,有一位撑着油纸伞的姑娘从远方走来,天微微亮,有点小雨,似乎是从那位大师的油画中刚走出来的一样,忽远忽近,影影绰绰,不食人间烟火!', '200');
  21. INSERT INTO `t_route` VALUES ('2', '风景秀丽,集日月之精华,看一眼神清气爽,再看一眼快乐齐天,岂不美哉!', '2019-04-23 16:40', 'route_pic.jpg', '古老的小镇,有一位撑着油纸伞的姑娘从远方走来,天微微亮,有点小雨,似乎是从那位大师的油画中刚走出来的一样,忽远忽近,影影绰绰,不食人间烟火!', '100');

  

然后把C3p0的配置文件拷贝到src下,并进入配置文件改数据库的信息,

一个javaWeb项目模板就创建好了。

3.后台代码实现:

根据第一张图可以知道结构,现在按图示结构粘贴下代码:

实体类Route旅游路线的实体类

  1. package com.hcz.bean;
  2.  
  3. /**
  4. * @author HuChengZhang
  5. * @describtion 旅游路线bean
  6. * @date 2019/4/23 17:08
  7. */
  8.  
  9. public class Route {
  10. private int id;
  11. private String title;
  12. private String date;
  13. private String routeImg;
  14. private String routeIntroduce;
  15. private int count;
  16.  
  17. public Route() {
  18. }
  19.  
  20. public Route(int id, String title, String date, String routeImg, String routeIntroduce, int count) {
  21.  
  22. this.id = id;
  23. this.title = title;
  24. this.date = date;
  25. this.routeImg = routeImg;
  26. this.routeIntroduce = routeIntroduce;
  27. this.count = count;
  28. }
  29.  
  30. public int getId() {
  31. return id;
  32. }
  33.  
  34. public void setId(int id) {
  35. this.id = id;
  36. }
  37.  
  38. public String getTitle() {
  39. return title;
  40. }
  41.  
  42. public void setTitle(String title) {
  43. this.title = title;
  44. }
  45.  
  46. public String getDate() {
  47. return date;
  48. }
  49.  
  50. public void setDate(String date) {
  51. this.date = date;
  52. }
  53.  
  54. public String getRouteImg() {
  55. return routeImg;
  56. }
  57.  
  58. public void setRouteImg(String routeImg) {
  59. this.routeImg = routeImg;
  60. }
  61.  
  62. public String getRouteIntroduce() {
  63. return routeIntroduce;
  64. }
  65.  
  66. public void setRouteIntroduce(String routeIntroduce) {
  67. this.routeIntroduce = routeIntroduce;
  68. }
  69.  
  70. public int getCount() {
  71. return count;
  72. }
  73.  
  74. public void setCount(int count) {
  75. this.count = count;
  76. }
  77.  
  78. @Override
  79. public String toString() {
  80. return "Route{" +
  81. "id=" + id +
  82. ", title='" + title + '\'' +
  83. ", date='" + date + '\'' +
  84. ", routeImg='" + routeImg + '\'' +
  85. ", routeIntroduce='" + routeIntroduce + '\'' +
  86. ", count=" + count +
  87. '}';
  88. }
  89. }

  

RouteDaoImpl

  1. package com.hcz.dao.daoImpl;
  2.  
  3. import com.hcz.bean.Route;
  4. import com.hcz.dao.RouteDao;
  5. import com.hcz.utils.C3p0Utils;
  6. import org.junit.Test;
  7. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  8. import org.springframework.jdbc.core.JdbcTemplate;
  9.  
  10. import java.util.List;
  11.  
  12. /**
  13. * @author HuChengZhang
  14. * @describtion
  15. * @date 2019/4/23 17:15
  16. */
  17.  
  18. public class RouteDaoImpl implements RouteDao {
  19.  
  20. /**
  21. * 单纯的查找数据库数据
  22. * @return
  23. */
  24. @Override
  25. public List<Route> queryRouteList() {
  26. //创建jdbcTemplate核心类
  27. JdbcTemplate jdbcTemplate = new JdbcTemplate(C3p0Utils.getDataSource());
  28. //查询所有数据
  29. String sql = "select * from t_route";
  30. List<Route> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Route.class));
  31. return query;
  32. }
  33.  
  34. /*
  35. 单元测试:查询所有的路线封装到集合中
  36. */
  37. @Test
  38. public void test(){
  39. //创建jdbcTemplate核心类
  40. JdbcTemplate jdbcTemplate = new JdbcTemplate(C3p0Utils.getDataSource());
  41. //查询所有数据
  42. String sql = "select * from t_route";
  43. List<Route> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Route.class));
  44. System.out.println(query);
  45. }
  46. }

  

RouteDao

  1. package com.hcz.dao;
  2.  
  3. import com.hcz.bean.Route;
  4.  
  5. import java.util.List;
  6.  
  7. /**
  8. * @author HuChengZhang
  9. * @describtion
  10. * @date 2019/4/23 17:15
  11. */
  12.  
  13. public interface RouteDao {
  14. List<Route> queryRouteList();
  15. }

RouteServiceImpl

  1. package com.hcz.service.serviceImpl;
  2.  
  3. import com.alibaba.fastjson.JSON;
  4. import com.hcz.bean.Route;
  5. import com.hcz.dao.daoImpl.RouteDaoImpl;
  6. import com.hcz.service.RouteService;
  7.  
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11.  
  12. /**
  13. * @author HuChengZhang
  14. * @describtion
  15. * @date 2019/4/23 17:13
  16. */
  17.  
  18. public class RouteServiceImpl implements RouteService {
  19.  
  20. /**
  21. * 查到所有的路线,并用阿里巴巴的JSON插件把map集合转成字符串形式 dataList[{},{},{},,,]
  22. * @return
  23. */
  24. @Override
  25. public String queryRouteList() {
  26. RouteDaoImpl routeDao = new RouteDaoImpl();
  27. List<Route> list = routeDao.queryRouteList();
  28. Map<String,Object>map = new HashMap();
  29. map.put("dataList",list);
  30. return JSON.toJSONString(map);
  31. }
  32. }

  

RouteService

  1. package com.hcz.service;
  2.  
  3. /**
  4. * @author HuChengZhang
  5. * @describtion
  6. * @date 2019/4/23 17:13
  7. */
  8.  
  9. public interface RouteService {
  10. String queryRouteList();
  11. }

  

RouteServlet

  1. package com.hcz.servlet;
  2.  
  3. import com.hcz.service.serviceImpl.RouteServiceImpl;
  4.  
  5. import javax.servlet.ServletException;
  6. import javax.servlet.annotation.WebServlet;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11.  
  12. /**
  13. * @author HuChengZhang
  14. * @version v1.0
  15. * @date 2019/4/23 17:11
  16. * @description TODO
  17. **/
  18. @WebServlet(urlPatterns = "/queryRouteList")
  19. public class RouteServlet extends HttpServlet {
  20. @Override
  21. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  22. doGet(request, response);
  23. }
  24.  
  25. @Override
  26. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  27.  
  28. //2.处理数据
  29. request.setCharacterEncoding("UTF-8");
  30. RouteServiceImpl routeService = new RouteServiceImpl();
  31. String dataList = routeService.queryRouteList();
  32. System.out.println(dataList);
  33.  
  34. //3、响应数据
  35. response.setContentType("text/html;charset=utf-8");
  36. response.getWriter().print(dataList);
  37. }
  38. }

  

C3p0Utils工具类

  1. package com.hcz.utils;
  2.  
  3. import com.mchange.v2.c3p0.ComboPooledDataSource;
  4.  
  5. import javax.sql.DataSource;
  6.  
  7. /**
  8. * @author HuChengZhang
  9. * @describtion c3p0工具类
  10. * @date 2019/4/23 17:21
  11. */
  12.  
  13. public class C3p0Utils {
  14.  
  15. //初始化C3p0连接池
  16. private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
  17.  
  18. //得到数据源
  19. public static DataSource getDataSource(){
  20. return dataSource;
  21. }
  22.  
  23. }

  

至此,后台的简单代码完成。

4.微信小程序:

结构图,红线表示自己添加或者改动过的。

第1步在全局json这个“pages”快速创建文件夹:

第2步把images放图片的文件拷贝到项目所在的本地硬盘路径里:

share.png

star.png

其它这些图片自己下载一些好看的图片吧,哈哈坑一下

好了,现在又要按顺序粘贴代码了:

route.js

  1. /**
  2. * 生命周期函数--监听页面加载
  3. */
  4. onLoad: function (options) {
  5. var that = this;
  6. //页面加载完成之后 发送请求查询线路列表数据
  7. wx.request({
  8. url: 'http://127.0.0.1:8080/queryRouteList',
  9. method: 'get',
  10. dataType: 'json',
  11. success: function (res) {
  12. console.log(res);
  13. that.setData({
  14. dataList: res.data.dataList
  15. })
  16. }
  17. })
  18. },

  

route.wxml

  1. <import src='../../templates/route-template/route-template.wxml' />
  2. <!-- 页面布局 -->
  3. <view class='route-container'>
  4.  
  5. <!-- 轮播图 -->
  6. <view class='route-swiper'>
  7. <swiper autoplay='true' interval='2000' indicator-dots='true' indicator-active-color='#fff'>
  8. <swiper-item>
  9. <image src='../../images/route/banner_1.jpg'></image>
  10. </swiper-item>
  11. <swiper-item>
  12. <image src='../../images/route/banner_2.jpg'></image>
  13. </swiper-item>
  14. <swiper-item>
  15. <image src='../../images/route/banner_3.jpg'></image>
  16. </swiper-item>
  17. </swiper>
  18. </view>
  19.  
  20. <block wx:for='{{dataList}}' wx:for-item='detail' wx:key="">
  21. <template is='route_template' data='{{detail:detail}}'/>
  22. </block>
  23.  
  24. </view>

  

route.wxss

  1. @import '../../templates/route-template/route-template.wxss';
  2.  
  3. .route-swiper{
  4. width: 100%;
  5. height: 300rpx;
  6. }
  7.  
  8. .route-swiper image{
  9. width: 100%;
  10. }

  

welcome.js

  1. /**
  2. * 页面的初始数据
  3. */
  4. data: {
  5.  
  6. },
  7.  
  8. /**
  9. * onTap点击事件触发
  10. */
  11. onTap:function(){
  12. wx.navigateTo({
  13. url: '../route/route',
  14. })
  15. },
  16.  
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad: function (options) {
  21.  
  22. },

  

welcome.wxml

  1. <view class='welcome-container'>
  2. <view class='welcome-img'>
  3. <image src='../../images/welcome/welcome.png'></image>
  4. </view>
  5.  
  6. <view class='welcome-text'>
  7. <text>胡成长</text>
  8. </view>
  9.  
  10. <!-- 按钮跳转页面 -->
  11. <view class='welcome-btn'>
  12. <button size='mini' bindtap='onTap' type='primary'>
  13. 进入另一片天地
  14. </button>
  15. </view>
  16. </view>

  

welcome.wxss

  1. /* pages/welcome/welcome.wxss */
  2.  
  3. .welcome-container{
  4. display: flex;
  5. flex-direction: column;
  6. align-items: center;
  7. }
  8.  
  9. page{
  10. background-color:#F6F8F8
  11. }
  12.  
  13. .welcome-img{
  14. margin-top: 140rpx;
  15. }
  16.  
  17. .welcome-img image{
  18. width: 200rpx;
  19. height: 200rpx;
  20. }
  21.  
  22. .welcome-text{
  23. margin-top: 120rpx;
  24. }
  25.  
  26. .welcome-text text{
  27. font-size: 30rpx;
  28. font-weight: bold;
  29. }
  30.  
  31. .welcome-btn{
  32. margin-top: 130rpx;
  33. }

  

route-template.wxml

  1. <!--templates/route-template/route-template.wxml-->
  2.  
  3. <!-- 模板要template 包起来 -->
  4. <template name='route_template'>
  5. <view class='route-list'>
  6. <!-- 图片和日期 -->
  7. <view class='img-date'>
  8. <image src='../../images/route/title_pic.jpg'></image>
  9. <text>{{detail.date}}</text>
  10. </view>
  11.  
  12. <!-- 标题 -->
  13. <view class='route-title'>
  14. <text>{{detail.title}}</text>
  15. </view>
  16.  
  17. <!-- 旅游线路图片 -->
  18. <view class='route-img'>
  19. <image src='../../images/route/{{detail.routeImg}}'></image>
  20. </view>
  21.  
  22. <!-- 介绍信息 -->
  23. <view class='route-introduce'>
  24. <text>{{detail.routeIntroduce}} </text>
  25. </view>
  26.  
  27. <!-- 收藏数量文字和图片 -->
  28. <view class='route-count'>
  29. <image src='../../images/icon/star.png'></image>
  30. <text>{{detail.count}}</text>
  31. <image src='../../images/icon/share.png'></image>
  32. <text>{{detail.count}}</text>
  33. </view>
  34. </view>
  35. </template>

  

route-template.wxss

  1. /* templates/route-template/route-template.wxss */
  2. .img-date{
  3. margin-top: 10rpx;
  4. }
  5.  
  6. .img-date image{
  7. width: 48rpx;
  8. height: 48rpx;
  9. }
  10.  
  11. .img-date text{
  12. margin-left: 10rpx;
  13. font-size: 30rpx;
  14. }
  15.  
  16. .route-title text{
  17. font-size: 35rpx;
  18. font-weight: bold;
  19. }
  20.  
  21. .route-img{
  22. margin-top: 5rpx;
  23. }
  24.  
  25. .route-img image{
  26. width: 100%;
  27. height: 340rpx;
  28. margin-bottom: 5rpx;
  29. }
  30.  
  31. .route-introduce text{
  32. font-size: 28rpx;
  33. color: #666;
  34. font-weight: 400;
  35. margin-left: 20rpx;
  36. letter-spacing: 2rpx;
  37. line-height: 40rpx;
  38. }
  39.  
  40. .route-count{
  41. display: flex;
  42. flex-direction: row;
  43. }
  44.  
  45. .route-count image{
  46. width: 16px;
  47. height: 16px;
  48. margin-right: 8rpx;
  49. vertical-align: middle;
  50. }
  51.  
  52. .route-count text{
  53. font-size: 30rpx;
  54. vertical-align: middle;
  55. margin-right: 20px;
  56. }
  57.  
  58. .route-list{
  59. margin-top: 20rpx;
  60. border-top: 1px solid #ededed;
  61. border-bottom: 1px solid #ededed;
  62. background-color: #fff;
  63. }

  

微信小程序-展示后台传来的json格式数据的更多相关文章

  1. 微信小程序结合后台数据管理实现商品数据的动态展示、维护

    微信小程序给我们提供了一个很好的开发平台,可以用于展现各种数据和实现丰富的功能,本篇随笔介绍微信小程序结合后台数据管理实现商品数据的动态展示.维护,介绍如何实现商品数据在后台管理系统中的维护管理,并通 ...

  2. 微信小程序通过api接口将json数据展现到小程序示例

    这篇文章主要介绍了微信小程序通过api接口将json数据展现到小程序示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧实现知乎客户端的一个重要知识前提就是,要知道怎么通过 ...

  3. 微信小程序:全局配置app.json

    微信小程序:全局配置app.json 一.全局配置app.json app.json文件用来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 tab 等. 以下是一个包 ...

  4. 微信小程序+java后台

    博主是大四学生,毕业设计做的是微信小程序+java后台.陆陆续续经历了三个月(因为白天要实习又碰上过年玩了一阵子),从对微信小程序一无所知到完成毕设,碰到许多问题,在跟大家分享一下自己的经历和一个小程 ...

  5. 微信小程序:页面配置 page.json

    微信小程序:页面配置 page.json 一.页面配置 page.json 如果整个小程序的风格是蓝色调,那么可以在 app.json 里边声明顶部颜色是蓝色即可. 实际情况可能不是这样,可能你小程序 ...

  6. 微信小程序开发:学习笔记[9]——本地数据缓存

    微信小程序开发:学习笔记[9]——本地数据缓存 快速开始 说明 本地数据缓存是小程序存储在当前设备上硬盘上的数据,本地数据缓存有非常多的用途,我们可以利用本地数据缓存来存储用户在小程序上产生的操作,在 ...

  7. 微信小程序php后台实现

    这里简单介绍用php后台实现获取openid并保存到数据库: 微信的登陆流程是这样的 首先前端发送请求到服务器: wx.login({ success: function (res) { var co ...

  8. 行星万象表白墙微信小程序、社交微信小程序,后台完整,支持多区域运营,扫码体验。

    简介 中国目前大概有5000个表白墙,累计用户近3000万,是一个庞大的群体,但现在大都以微信朋友圈为基础进行信息中转,但是这种模式经营者和用户都不友好,尤其是经营者无法变现,用户无法公开评论,这些种 ...

  9. (二)校园信息通微信小程序从后台获取首页的数据笔记

    在从后台获取数据之前,需要先搭建好本地服务器的环境. 确保Apache,MySql处于开启状态.下图为Apache,MySql处于开启时状态 然后进入后台管理平台进行字段和列表的定义 然后在后台添加数 ...

随机推荐

  1. php单例模式的实现

    <?php /** * 设计模式之单例模式 * $_instance必须声明为静态的私有变量 * 构造函数和析构函数必须声明为私有,防止外部程序new * 类从而失去单例模式的意义 * getI ...

  2. C#实现读取IPv6 UDP Socket数据,再发送出去

    C#实现读取IPv6 UDP Socket数据,再发送出去. 不知为何,黑框点一下就停止刷新了,再点一下,就继续刷新了. using System; using System.Collections. ...

  3. day13 JS Dom

    js两种存在形式 1:文件 2:块 放到body标签底部 防止加载js超时页面反应慢的问题 声明变量 name = "sb"; //全局变量 var age=18; //局部变量 ...

  4. grep知识及常用用法梳理

    1. grep语法及其参数说明 grep是文本搜索工具,能根据用户指定的'PATTERN模式'目标文本进行逐行匹配检查,注意grep默认会以 行 为单位打印匹配到的行. 以下是grep命令的语法及常用 ...

  5. vue element 表头添加斜线

    <template> <div class="app-container"> <el-table :data="tableData3&quo ...

  6. webpack--介绍、安装及入门

    最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了.后来,代码越来越多,一个文件不够了,必须分成多个文件,依次加载.下面的网页代码,相信很多人都见过. <scr ...

  7. 进阶之路 | 奇妙的Drawable之旅

    前言 本文已经收录到我的Github个人博客,欢迎大佬们光临寒舍: 我的GIthub博客 学习清单: Drawable简介 Drawable分类 自定义Drawable 一.为什么要学习Drawabl ...

  8. Linux下使用Nginx

    模拟tomcat集群 1.下载tomcat7,/usr/local下新建目录tomcat,将tomcat7剪切到/usr/local/tomcat wget http://mirror.bit.edu ...

  9. 解决Fail to post notification on channel "null"的方法

    mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);mNotifyMgr.cancelAll(); St ...

  10. MySQL索引优化深入

    创建 test 测试表 CREATE TABLE `test` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `c1` varchar(10) DEFAULT N ...