博客
关于我
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备份工具xtrabackup
查看>>
mysql备份恢复出错_尝试备份/恢复mysql数据库时出错
查看>>
mysql复制内容到一张新表
查看>>
mysql复制表结构和数据
查看>>
mysql复杂查询,优质题目
查看>>
MySQL外键约束
查看>>
MySQL多表关联on和where速度对比实测谁更快
查看>>
MySQL多表左右连接查询
查看>>
mysql大批量删除(修改)The total number of locks exceeds the lock table size 错误的解决办法
查看>>
mysql如何做到存在就更新不存就插入_MySQL 索引及优化实战(二)
查看>>
mysql如何删除数据表,被关联的数据表如何删除呢
查看>>
MySQL如何实现ACID ?
查看>>
mysql如何记录数据库响应时间
查看>>
MySQL子查询
查看>>
Mysql字段、索引操作
查看>>
mysql字段的细节(查询自定义的字段[意义-行列转置];UNION ALL;case-when)
查看>>
mysql字段类型不一致导致的索引失效
查看>>
mysql字段类型介绍
查看>>