博客
关于我
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 group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>
MySQL InnoDB事务隔离级别与锁机制深入解析
查看>>
Mysql InnoDB存储引擎 —— 数据页
查看>>
Mysql InnoDB存储引擎中的checkpoint技术
查看>>
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>
MySQL InnoDB引擎的锁机制详解
查看>>
Mysql INNODB引擎行锁的3种算法 Record Lock Next-Key Lock Grap Lock
查看>>
mysql InnoDB数据存储引擎 的B+树索引原理
查看>>
mysql innodb通过使用mvcc来实现可重复读
查看>>
mysql interval显示条件值_MySQL INTERVAL关键字可以使用哪些不同的单位值?
查看>>
Mysql join原理
查看>>
mysql order by多个字段排序
查看>>
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>