postman请求JSON类型数据,获取后台数据库中的数据并进行响应

第一次使用postman工具,进行前后台数据交互,简单的一次请求,能够灵活运用mybatis的性能

1.postman请求的数据:


2.使用IDEA单独使用测试,导入相关jar包

3.把需要进行操作的数据库中的那个表进行实体类封装 domain


4.dao

package com.xxx.dao;
import com.cq.domain.Doorkeeper;
import java.util.List;

public interface DoorkeeperDao {
    //查询所有
    public List<Doorkeeper> getAll();
}

5 resources/mapper/

6.连接IO流进行读写操作

package com.xxx;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public class HttpRequest {
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            //1.获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            //2.中文有乱码的需要将PrintWriter改为如下
//            out=new OutputStreamWriter(conn.getOutputStream(),"UTF-8");

            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常!" + e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        System.out.println("post推送结果:" + result);
        return result;
    }


}

7.调用函数

 public static void sendInfo(String devcieCode,String cardNumber){
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("token", "xxxxxxxxxxxxxxxx");
        jsonParam.put("method","write");
        jsonParam.put("device", devcieCode+".1.1");
        jsonParam.put("timeout","3");
        jsonParam.put("value","add,"+cardNumber);
        String urls="http://192.168.xx.xxx:30002/xxx";
        String data=GetJsonData.getJsonData(jsonParam,urls);
    }

8.SqlMapperConfig.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 1.环境使用,连接数据库 -->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://192.168.xx.xxx/数据库表名称?useSSL=false"/>
                <property name="username" value="root"/>
                <property name="password" value="123456789"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 2.关于动态sql语句的文件引入 -->
    <mappers>
        <mapper resource="Mapper/DoorkeeperMapper.xml"/>
    </mappers>
</configuration>

9.main方法中:

public static void main(String[] args) throws IOException {
        // 1.加载SqlMapConfig.xml
        String location = "SqlMapConfig.xml";
        InputStream is = Resources.getResourceAsStream(location);
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        // 2.获取session
        SqlSessionFactory factory = builder.build(is);
        SqlSession session = factory.openSession();

        DoorkeeperPrivilegeDao m2 = session.getMapper(DoorkeeperPrivilegeDao.class);
        List<DoorkeeperPrivilege> list2 = m2.getAll();
        for(DoorkeeperPrivilege doorkeeperPrivilege : list2){
            sendInfo(doorkeeperPrivilege.getDeviceCode(),doorkeeperPrivilege.getCardNumber());
        }
        session.commit();
        session.close();

    }

总结:一个是post请求数据需要进行IO流的数据读写,一个是能够的单独使用mybatis进行数据库连接,掌握这两点,基本上就可以根据实际开发来操作。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值