博客
关于我
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 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>
MySQL 用 limit 为什么会影响性能?
查看>>
MySQL 用 limit 为什么会影响性能?有什么优化方案?
查看>>
MySQL 用户权限管理:授权、撤销、密码更新和用户删除(图文解析)
查看>>
mysql 用户管理和权限设置
查看>>
MySQL 的 varchar 水真的太深了!
查看>>
mysql 的GROUP_CONCAT函数的使用(group_by 如何显示分组之前的数据)
查看>>
MySQL 的instr函数
查看>>
MySQL 的mysql_secure_installation安全脚本执行过程介绍
查看>>
MySQL 的Rename Table语句
查看>>
MySQL 的全局锁、表锁和行锁
查看>>
mysql 的存储引擎介绍
查看>>
MySQL 的存储引擎有哪些?为什么常用InnoDB?
查看>>
Mysql 知识回顾总结-索引
查看>>
Mysql 笔记
查看>>
MySQL 精选 60 道面试题(含答案)
查看>>
mysql 索引
查看>>
MySQL 索引失效的 15 种场景!
查看>>
MySQL 索引深入解析及优化策略
查看>>