博客
关于我
SpringBoot代码片段,方便自己查看[持续添加]
阅读量:496 次
发布时间:2019-03-07

本文共 3202 字,大约阅读时间需要 10 分钟。

应用程序配置文件

[更新至Джava优化版本]

配置文件版本:

  • 服务端口:9000
  • Spring配置
    • 数据源配置:
      • 用户名:root
      • 密码:123456
      • 数据库连接字符串: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
      • 驱动类名:com.mysql.cj.jdbc.Driver
    • 模板引擎配置:
      • 缓存状态:false(关闭Thymeleaf缓存)
      • 模板模式:HTML
      • 模板文件扩展名:.html
      • 模板路径:classpath:/templates/
      • 编码格式:UTF-8
      • 内容类型:text/html
  • Spring MVC配置
    • static文件路径:/static/**
  • mybatis配置
    • 类型别名包:com.sxf.web.mapper
    • mapper文件路径:classpath:mapper/*.xml

封装返回结果类

package com.scaf.util;import java.util.List;import java.util.Map;public class Result extends HashMap
{ public Result put(String key, Object value) { super.put(key, value); return this; } public Result() {} public static Result error(int code, String msg) { Result r = new Result(); r.put("code", code); r.put("msg", msg); return r; } public static Result error() { return error(500, "未知异常,请联系管理员"); } public static Result error(String msg) { return error(500, msg); } public static Result ok(Map
map) { Result r = new Result(); r.put("code", 0); r.putAll(map); return r; } public static Result ok(List
> list) { Result r = new Result(); r.put("code", 0); r.put("msg", list); return r; } public static Result ok() { Result r = new Result(); r.put("code", 0); r.put("msg", "操作成功"); return r; } public static Result ok(Object msg) { Result r = new Result(); r.put("code", 0); r.put("msg", msg); return r; }}

使用示例:

public Result getUserInfo(String openid) {    Result result = userMapper.getInfomation(openid);    if (result != null) {        return Result.ok(result);    } else {        return Result.error("系统错误");    }}@RequestMapping("/test")public Result test() {    List
> list = new ArrayList<>(); Map
item1 = new HashMap<>(2); item1.put("itemId", 1); list.add(item1); Map
item2 = new HashMap<>(2); item2.put("itemId", 2); list.add(item2); return new Result().ok(list);}

Python调用示例:

public String executePythonCommand() {    try {        String[] args = new String[]{"python", "tool.py"};        Process process = Runtime.getRuntime().exec(args);        BufferedReader successMsg = new BufferedReader(new InputStreamReader(process.getInputStream()));        BufferedReader errorMsg = new BufferedReader(new InputStreamReader(process.getErrorStream()));        String line;        while ((line = successMsg.readLine()) != null) {            System.out.println(line);        }        while ((line = errorMsg.readLine()) != null) {            System.out.println(line);        }        successMsg.close();        errorMsg.close();        process.waitFor();    } catch (Exception e) {        e.printStackTrace();    }    return "操作成功";}

字符编码问题解决方案

为了避免中文字符乱码,可以将默认字符集设置为UTF-8,并指定时区参数:

public class CharSetFixer {    private CharsetFactory factory;    public CharsetFactory getCharsetFactory() {        return this.factory;    }}

此外,建议在VM配置中添加如下参数:

-Dfile.encoding=UTF-8 -D.locale極.verify谎字真 Urs士

如果需要进一步解决乱码问题,可参考MySQL官方文档或相关技术博客。

转载地址:http://fbscz.baihongyu.com/

你可能感兴趣的文章
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
MongoDB学习笔记(8)--索引及优化索引
查看>>
ms sql server 2008 sp2更新异常
查看>>
MS UC 2013-0-Prepare Tool
查看>>
msbuild发布web应用程序
查看>>
MSCRM调用外部JS文件
查看>>
MSEdgeDriver (Chromium) 不适用于版本 >= 79.0.313 (Canary)
查看>>
msf
查看>>
MSSQL数据库查询优化(一)
查看>>
MSSQL日期格式转换函数(使用CONVERT)
查看>>
MSTP多生成树协议(第二课)
查看>>
MSTP是什么?有哪些专有名词?
查看>>
Mstsc 远程桌面链接 And 网络映射
查看>>
Myeclipse常用快捷键
查看>>
MyEclipse用(JDBC)连接SQL出现的问题~
查看>>