Springboot中文乱码问题(还有一个未解决)
视频地址:http://www.imooc.com/learn/587
这个视频感觉废话有点多,看的有点困。
一、创建项目
1、创建maven项目

2、默认创建的项目,用的servlet的版本是2.3,太老,换成3.1

3、junit默认是3.8,改为4.11,因为4.x才支持注解

二、添加依赖
1、日志依赖


2、db依赖

3、dao依赖

4、servlet web依赖

5、Spring依赖




三、秒杀业务


用户角度:


为什么需要事务:


数据落地:
NoSQL(如Redis、HBase、ES等)的优势是高性能、高可用、分布式,对事务的支持还是不如MySQL。

四、秒杀系统的难点

事务

行级锁


五、实现哪些秒杀功能

本课程实现的功能

代码开发阶段

六、数据库设计


七、dao


Mybatis的特点

mybatis-config.xml

SeckillDao.xml


SuccessKilledDao.xml
- 第一个sql中的ignore,表示如果插入了重复主键,sql不抛出异常,而是返回0,方便业务处理。
-
第二个sql中的别名,是为了把对象设置为SuccessKilled.seckill属性
image.png
高并发的点:

优化总结

最后一点有两种方式:
1、修改mysql源码,在后面增加一个注释,如果达到某个条件(比如发现库存为0),就直接回滚,这样就可以减少java和mysql的交互次数

2、采用存储过程,效果跟上面类似。
redis
更好的序列化对象
protostuff来做序列化,可以提供比java自带序列化更快、更强。体积大概是原来的0.2甚至0.1,时间也是数量级的提升。



锁优化

把insert提前,缩短update和commit/rollback之间的时间


部署架构
![Uploading image_335182.png . . .]


1、编码统一:
所有编码都采用utf-8,包括文件编码、数据库链接编码
2、修改application.properties文件,增加如下配置:
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
3、PathVariable含有中文(未解决)
以上方式可以解决http://xxx.com/test?name=中文这种
但是对于http://xxx.com/test/中文这种还是乱码,网上找了几种解决方式,测试了一下都没奏效,找问题ing。
1)设置mapping的produces
@RequestMapping(value = "/query/{keyword}", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
public String query(@PathVariable String keyword) {
}
2)自己做编码转换
@RequestMapping(value="/search/{key}", method = {RequestMethod.GET})
public String searchFaceListIndex(@PathVariable(value="key") String key,Map<String, Object> result) throws UnsupportedEncodingException {
key = new String(key.getBytes("ISO-8859-1"), "utf8");
return "searchFaceList";
}
目前尚未解决,欢迎评论指导。