wx.request({url: '/dl',method: 'get',data: {latitude: wei.latitude,//longitude:longitude,//longitude: wei.longitudelongitude:wei.longitude,jici:that.data.jici},header: {'content-type': 'application/json' // 默认值// 'Content-Type': 'application/x-www-form-urlencoded'},success: function (res) {that.setData({ nr6: res.data });// console.log(res.data.lo);//测试//var json=JSON.parse(res.data);//console.log(res.data[0].id);},fail: function (res) {console.log(".....fail.....");}})});
将成功后返回的值存到nr6中
that.setData({ nr6: res.data });
在wxml中写个view
wx:for="{{nr6}}"
这个是从后台取回的数据
view中包含的view用{{item.值}}
取得nr6中包含的数据
<view wx:for="{{nr6}}" ><view>用户名:{{item.username}}</view><view>密码:{{item.password}}</view><view>{{item.latitude}}</view><view>{{item.longitude}}</view><view>{{item.nr}}</view><view>{{item.data}}</view><view>{{item.id}}</view></view>
下面是后台controller
入门是看得这位大哥的文
作者:云淡风轻58
来源:CSDN
原文:/dwenxue/article/details/81735864
版权声明:本文为博主原创文章,转载请附上博文链接!
这个是前面wx request中的地址用@RequestMapping("/dl")
拦截
写了个service层接口 用来控制dao层
@Resource(name="hqnr")private serviceusername s2;
下面是调用hqrn接口 获取内容
s2.hqnr(la, lo,jici)
hqnr调用dao
package control;import java.util.HashMap;import java.util.List;import java.util.Map;import bean.nrbean;import bean.hfbean;public interface serviceusername {public boolean insert(nrbean nr);//jie shou chua di de nr yong hq shi xian leipublic List<nrbean> hqnr(double la,double lo);//jie shou chua di de nr yong hq shi xian leipublic HashMap<String,List> jiezhexie(int o);//jie shou chua di de nr yong hq shi xian leipublic boolean getallopenid(int i,String a);public boolean inserthf(hfbean nr);public String openid(String code);List<nrbean> hqnr(double la, double lo, int pagenumber);}
在servlet.xml中需要配置接口
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"xmlns:mvc="/schema/mvc"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd/schema/context /schema/context/spring-context-4.1.xsd/schema/mvc /schema/mvc/spring-mvc-4.1.xsd"> <!-- 定义要扫描 controller的包 --><context:component-scan base-package="control" /><!-- src下面的control 然后好找到controller --><mvc:default-servlet-handler /> <!-- 启动注解驱动 SpringMVC 功能 --><mvc:annotation-driven /><!-- 配置视图解析器解析路径 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"><!-- 定义视图存放路径 --><property name="prefix" value="" /></bean><bean id="hql" class="control.hq" /><bean id="hqnr" class="control.hqnr" /><bean id="jiezhexie" class="control.dojiezhexie" /><bean id="panduan" class="control.dopanduan" /><bean id="hf6" class="control.dohf" /><bean id="jiemi6" class="control.dojiemi" /></beans>
下面是接口调用dao中的getall 返回list给controller
package control;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import bean.dao;import bean.nrbean;import bean.hfbean;public class hqnr implements serviceusername{@Overridepublic List<nrbean> hqnr(double la, double lo,int pagenumber) {// TODO Auto-generated method stubdao d=new dao();List<nrbean> li=new ArrayList<nrbean>();// po=null;li=d.getAll(la,lo,pagenumber);//(la, lo);System.out.print("..................."+li);return li;}@Overridepublic boolean insert(nrbean nr) {// TODO Auto-generated method stubreturn false;}@Overridepublic HashMap<String, List> jiezhexie(int o) {// TODO Auto-generated method stubreturn null;}@Overridepublic boolean getallopenid(int i, String a) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean inserthf(hfbean nr) {// TODO Auto-generated method stubreturn false;}@Overridepublic String openid(String code) {// TODO Auto-generated method stubreturn null;}@Overridepublic List<nrbean> hqnr(double la, double lo) {// TODO Auto-generated method stubreturn null;}}
下面是dao
public ArrayList getAll(double latitude,double longitude,int pagenumber) {//public ArrayList getAll(int id)需要找id查找时//nrbean nr=new nrbean();//int i=book6.getId();Connection con = null; Statement stmt = null; PreparedStatement pstmt = null;ResultSet rs = null; ArrayList nr1 = new ArrayList(); try{ con=sjk.con6();//con由import coo中con6 封装查询// Class.forName("com.mysql.jdbc.Driver"); //con = DriverManager.getConnection("jdbc:mysql://localhost:3306/qqg?useUnicode=true&characterEncoding=utf8","数据库用户名","数据库密码"); double lajia,lajian,lojia,lojian;lajia=latitude+0.6;lajian=latitude-0.6;lojia=longitude+0.6;lojian=longitude-0.6;System.out.print(lajia+"()"+lajian+"()"+lojia+"()"+lojian);pstmt = con.prepareStatement("select * from nr where longtitude<='"+lojia+"' and longtitude>='"+lojian+"' and latitude>='"+lajian+"' and latitude<='"+lajia+"' order by id desc limit "+pagenumber+""); // select * from tb_test6 order by mid desc limit "+pagenumber+",26 //在这where上面的id//pstmt.setInt(1,id);stmt = con.createStatement(); rs = pstmt.executeQuery(); while (rs.next()) {int rowCount = rs.getRow(); //获得ResultSet的总行数System.out.print("//"+rowCount+"");nrbean nr=new nrbean();nr.setUsername(rs.getString("username"));nr.setNr(rs.getString("nr"));nr.setId(rs.getInt("id"));//b.setId(rs.getInt("id"));nr.setLatitude(rs.getDouble("latitude"));nr.setLongitude(rs.getDouble("longtitude"));nr.setData(rs.getString("data"));//resultBook = new Book();nr1.add(nr);}}catch(Exception e){System.out.println(e.getMessage());}return nr1;// return resultBook;}
最后是controller
用@ResponseBody
返回list给小程序
package control;import java.io.IOException;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.sql.Connection;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import net.sf.json.JSONArray;import org.json.simple.JSONObject;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import bean.dao;import bean.indexdao;import bean.nrbean;import bean.sjk;import bean.userbean;@Controller /////public class username {// @RequestMapping("/username") // public String test(@RequestParam("username")String name) { //; //System.out.println(name); // // return "index"; //}//}//都能用@RequestMapping("/nr")//用post方式接受表单信息public class nr {@Resource(name="hqnr")private serviceusername s2;//@RequestMapping("/log") @RequestMapping("/dl")@ResponseBody//////返回list jsonpublic List<nrbean> e(HttpServletRequest request,HttpSession session,HttpServletResponse response) throws IOException {//需要request的话需要在这加 response.setContentType("text/html;charset=utf-8");//����������request.setCharacterEncoding("utf-8");//��Ӧ�������response.setCharacterEncoding("utf-8");String longitude=request.getParameter("longitude");System.out.print(longitude+"++++++++++++++++++++++++++");String latitude=request.getParameter("latitude");System.out.print(latitude+"_________________________");String ji=request.getParameter("jici");System.out.print(ji);int jici=Integer.valueOf(ji).intValue();double la,lo;la=Double.parseDouble(latitude);lo=Double.parseDouble(longitude);// nrbean ne=new nrbean();// ArrayList nr6=null;dao d=new dao();List<nrbean> li=new ArrayList<nrbean>();// po=null;// List aa=new List()li=s2.hqnr(la, lo,jici);// li=d.getAll(la, lo);System.out.print(li);return li;// @RequestMapping("/dl")}//@RequestMapping("/dll")//@ResponseBody//public void cc(String username,String password){//System.out.print(username+password);//userbean b=new userbean();//}@RequestMapping("/dll")@ResponseBodypublic userbean bean(userbean bean){bean.setUsername("ll");bean.setPassword("ll");return bean;}//--------------------- //作者:云淡风轻58 //来源:CSDN //原文:/dwenxue/article/details/81735864 //版权声明:本文为博主原创文章,转载请附上博文链接!}