博客
关于我
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/

你可能感兴趣的文章
mysql workbench6.3.5_MySQL Workbench
查看>>
MySQL Workbench安装教程以及菜单汉化
查看>>
MySQL Xtrabackup 安装、备份、恢复
查看>>
mysql [Err] 1436 - Thread stack overrun: 129464 bytes used of a 286720 byte stack, and 160000 bytes
查看>>
MySQL _ MySQL常用操作
查看>>
MySQL – 导出数据成csv
查看>>
MySQL —— 在CentOS9下安装MySQL
查看>>
MySQL —— 视图
查看>>
mysql 不区分大小写
查看>>
mysql 两列互转
查看>>
MySQL 中开启二进制日志(Binlog)
查看>>
MySQL 中文问题
查看>>
MySQL 中日志的面试题总结
查看>>
mysql 中的all,5分钟了解MySQL5.7中union all用法的黑科技
查看>>
MySQL 中的外键检查设置:SET FOREIGN_KEY_CHECKS = 1
查看>>
Mysql 中的日期时间字符串查询
查看>>
mysql 中索引的问题
查看>>
MySQL 中锁的面试题总结
查看>>
MySQL 中随机抽样:order by rand limit 的替代方案
查看>>
MySQL 为什么需要两阶段提交?
查看>>