资料网站:http://learning.happymmall.com/env.html
一、mybatis三剑客:generator,plugin,pagehelper
pagehelper->https://github.com/pagehelper/Mybatis-PageHelper
二、spring 例子:
https://github.com/spring-projects/spring-mvc-showcase
https://github.com/spring-projects/spring-petclinic
https://github.com/spring-projects/greenhouse
https://github.com/spring-projects/spring-boot
三、IDEA 实时编译:
打开settings界面:File->Settings
选中Compiler选项:Build,Execution,Deployment->Compiler
选中Make project automatically
四、谷歌插件:Restlet Client(相当于post man)和FE(相当于JSON Viewer)
五、CacheBuilder本地缓存的使用
LoadingCache<String, String> localCache = CacheBuilder.newBuilder().initialCapacity(1000).maximumSize(10000).expireAfterAccess(12, TimeUnit.HOURS).build(
new CacheLoader<String, String>() {
//默认的数据加载实现,当调用get取值的时候,如果key没有对应的值,就调用这个方法进行加载
@Override
public String load(String s) throws Exception {
return null;
}
}
);
localCache.put(key,value);
localCache.get(key);
六、递归算法
//递归算法,算出子节点,Category 要重写eqauls与hashCode
private Set<Category> findChildCategory(Set<Category> categorySet ,Integer categoryId){
Category category = categoryMapper.selectByPrimaryKey(categoryId);
if(category != null){
categorySet.add(category);
}
//查找子节点,递归算法一定要有一个退出的条件
List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
for(Category categoryItem : categoryList){
findChildCategory(categorySet,categoryItem.getId());
}
return categorySet;
}
七、静态块,当classloader加载这个类时,就会先执行静态块里面的内容,并且只会执行一次
static{
...
}
public class PropertiesUtil {
private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
private static Properties props;
static {
String fileName = "mmall.properties";
props = new Properties();
try {
props.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName),"UTF-8"));
} catch (IOException e) {
logger.error("配置文件读取异常",e);
}
}
public static String getProperty(String key){
String value = props.getProperty(key.trim());
if(StringUtils.isBlank(value)){
return null;
}
return value.trim();
}
public static String getProperty(String key,String defaultValue){
String value = props.getProperty(key.trim());if(StringUtils.isBlank(value)){
value = defaultValue;
}
return value.trim();
}
}
八、Date与String转换
public class DateTimeUtil {
//joda-time
//str->Date
//Date->str
public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static Date strToDate(String dateTimeStr,String formatStr){
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
return dateTime.toDate();
}
public static String dateToStr(Date date,String formatStr){
if(date == null){
return StringUtils.EMPTY;
}
DateTime dateTime = new DateTime(date);
return dateTime.toString(formatStr);
}
public static Date strToDate(String dateTimeStr){
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
return dateTime.toDate();
}
public static String dateToStr(Date date){
if(date == null){
return StringUtils.EMPTY;
}
DateTime dateTime = new DateTime(date);
return dateTime.toString(STANDARD_FORMAT);
}
public static void main(String[] args) {
System.out.println(DateTimeUtil.dateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
System.out.println(DateTimeUtil.strToDate("2010-01-01 11:11:11","yyyy-MM-dd HH:mm:ss"));
}
}
九、mybatis-pagehelper实际应用
public ServerResponse<PageInfo> getProductList(int pageNum,int pageSize){
//startPage--start
//填充自己的sql查询逻辑
//pageHelper-收尾
PageHelper.startPage(pageNum,pageSize);
PageHelper.orderBy("name desc");//5.0后没有排序方法
List<Product> productList = productMapper.selectList();
List<ProductListVo> productListVoList = Lists.newArrayList();
for(Product productItem : productList){
ProductListVo productListVo = assembleProductListVo(productItem);
productListVoList.add(productListVo);
}
PageInfo pageResult = new PageInfo(productList);
pageResult.setList(productListVoList);
return ServerResponse.createBySuccess(pageResult);
}
十、ftp上传文件
10.1 FTPUtil
package com.mmall.util;
import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
/**
* Created by geely
*/
public class FTPUtil {
private static final Logger logger = LoggerFactory.getLogger(FTPUtil.class);
private static String ftpIp = PropertiesUtil.getProperty("ftp.server.ip");
private static String ftpUser = PropertiesUtil.getProperty("ftp.user");
private static String ftpPass = PropertiesUtil.getProperty("ftp.pass");
public FTPUtil(String ip,int port,String user,String pwd){
this.ip = ip;
this.port = port;
this.user = user;
this.pwd = pwd;
}
public static boolean uploadFile(List<File> fileList) throws IOException {
FTPUtil ftpUtil = new FTPUtil(ftpIp,21,ftpUser,ftpPass);
logger.info("开始连接ftp服务器");
boolean result = ftpUtil.uploadFile("img",fileList);
logger.info("开始连接ftp服务器,结束上传,上传结果:{}");
return result;
}
private boolean uploadFile(String remotePath,List<File> fileList) throws IOException {
boolean uploaded = true;
FileInputStream fis = null;
//连接FTP服务器
if(connectServer(this.ip,this.port,this.user,this.pwd)){
try {
ftpClient.changeWorkingDirectory(remotePath);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
for(File fileItem : fileList){
fis = new FileInputStream(fileItem);
ftpClient.storeFile(fileItem.getName(),fis);
}
} catch (IOException e) {
logger.error("上传文件异常",e);
uploaded = false;
e.printStackTrace();
} finally {
fis.close();
ftpClient.disconnect();
}
}
return uploaded;
}
private boolean connectServer(String ip,int port,String user,String pwd){
boolean isSuccess = false;
ftpClient = new FTPClient();
try {
ftpClient.connect(ip);
isSuccess = ftpClient.login(user,pwd);
} catch (IOException e) {
logger.error("连接FTP服务器异常",e);
}
return isSuccess;
}
private String ip;
private int port;
private String user;
private String pwd;
private FTPClient ftpClient;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public FTPClient getFtpClient() {
return ftpClient;
}
public void setFtpClient(FTPClient ftpClient) {
this.ftpClient = ftpClient;
}
}
十一、上传文件service
public String upload(MultipartFile file,String path){
String fileName = file.getOriginalFilename();
//扩展名
//abc.jpg
String fileExtensionName = fileName.substring(fileName.lastIndexOf(".")+1);
String uploadFileName = UUID.randomUUID().toString()+"."+fileExtensionName;
logger.info("开始上传文件,上传文件的文件名:{},上传的路径:{},新文件名:{}",fileName,path,uploadFileName);
File fileDir = new File(path);
if(!fileDir.exists()){
fileDir.setWritable(true);
fileDir.mkdirs();
}
File targetFile = new File(path,uploadFileName);
try {
file.transferTo(targetFile);
//文件已经上传成功了
FTPUtil.uploadFile(Lists.newArrayList(targetFile));
//已经上传到ftp服务器上
targetFile.delete();
} catch (IOException e) {
logger.error("上传文件异常",e);
return null;
}
//A:abc.jpg
//B:abc.jpg
return targetFile.getName();
}
十二、富文本(http://simditor.tower.im/)
@RequestMapping("richtext_img_upload.do")
@ResponseBody
public Map richtextImgUpload(HttpSession session, @RequestParam(value = "upload_file",required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response){
Map resultMap = Maps.newHashMap();
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null){
resultMap.put("success",false);
resultMap.put("msg","请登录管理员");
return resultMap;
}
//富文本中对于返回值有自己的要求,我们使用是simditor所以按照simditor的要求进行返回
// {
// "success": true/false,
// "msg": "error message", # optional
// "file_path": "[real file path]"
// }
if(iUserService.checkAdminRole(user).isSuccess()){
String path = request.getSession().getServletContext().getRealPath("upload");
String targetFileName = iFileService.upload(file,path);
if(StringUtils.isBlank(targetFileName)){
resultMap.put("success",false);
resultMap.put("msg","上传失败");
return resultMap;
}
String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
resultMap.put("success",true);
resultMap.put("msg","上传成功");
resultMap.put("file_path",url);
response.addHeader("Access-Control-Allow-Headers","X-File-Name");
return resultMap;
}else{
resultMap.put("success",false);
resultMap.put("msg","无权限操作");
return resultMap;
}
}
十三、guava的使用:
13.1 依赖:
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>17.0</version>
</dependency>
13.2 使用:
将字符串转换成list
List<String> strings = Splitter.on(",").splitToList("sdfsdf,sdfsdf");
Lists.newArrayList();
十四、BigDecimalUtil
public class BigDecimalUtil {
private BigDecimalUtil(){
}
public static BigDecimal add(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.add(b2);
}
public static BigDecimal sub(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2);
}
public static BigDecimal mul(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.multiply(b2);
}
public static BigDecimal div(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2,2,BigDecimal.ROUND_HALF_UP);//四舍五入,保留2位小数
//除不尽的情况
}
}
十五、支付宝支付
15.1 官方文档
沙箱登录:https://openhome.alipay.com/platform/appDaily.htm
沙箱环境使用说明:https://doc.open.alipay.com/doc2/detail.htm?treeId=200&articleId=105311&docType=1
如何使用沙箱环境:https://support.open.alipay.com/support/hotProblemDetail.htm?spm=a219a.7386793.0.0.uS5uZ6&id=251932&tagId=100248
当面付产品介绍:https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.hV5Clx&treeId=193&articleId=105072&docType=1
扫码支付接入指引:https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.Ia6Wqy&treeId=193&articleId=106078&docType=1
当面付快速接入:https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.bROnXf&treeId=193&articleId=105170&docType=1
当面付接入必读:https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.hV5Clx&treeId=193&articleId=105322&docType=1
当面付进阶功能:https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.YFmkxI&treeId=193&articleId=105190&docType=1
当面付异步通知-仅用于扫码支付:https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.BykVSR&treeId=193&articleId=103296&docType=1
当面付SDK&DEMO:https://support.open.alipay.com/docs/doc.htm?spm=a219a.7386797.0.0.k0rwWc&treeId=193&articleId=105201&docType=1
服务端SDK:https://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1
生成RSA密钥:https://doc.open.alipay.com/docs/doc.htm?treeId=291&articleId=105971&docType=1
线上创建应用说明:https://doc.open.alipay.com/doc2/detail.htm?treeId=200&articleId=105310&docType=1#s0
15.2 坑:
回调验签时,调用AlipaySignature.rsaCheckV2(Map<String, String> params, String publicKey, String charset, String signType);
调用前,要去除sign_type,params.remove("sign_type");
@RequestMapping("alipay_callback.do")
@ResponseBody
public Object alipayCallback(HttpServletRequest request){
Map<String,String> params = Maps.newHashMap();
Map requestParams = request.getParameterMap();
for(Iterator iter = requestParams.keySet().iterator();iter.hasNext();){
String name = (String)iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = "";
for(int i = 0 ; i <values.length;i++){
valueStr = (i == values.length -1)?valueStr + values[i]:valueStr + values[i]+",";
}
params.put(name,valueStr);
}
logger.info("支付宝回调,sign:{},trade_status:{},参数:{}",params.get("sign"),params.get("trade_status"),params.toString());
//非常重要,验证回调的正确性,是不是支付宝发的.并且呢还要避免重复通知.
params.remove("sign_type");
try {
boolean alipayRSACheckedV2 = AlipaySignature.rsaCheckV2(params, Configs.getAlipayPublicKey(),"utf-8",Configs.getSignType());
if(!alipayRSACheckedV2){
return ServerResponse.createByErrorMessage("非法请求,验证不通过,再恶意请求我就报警找网警了");
}
} catch (AlipayApiException e) {
logger.error("支付宝验证回调异常",e);
}
//todo 验证各种数据
//
ServerResponse serverResponse = iOrderService.aliCallback(params);
if(serverResponse.isSuccess()){
return Const.AlipayCallback.RESPONSE_SUCCESS;
}
return Const.AlipayCallback.RESPONSE_FAILED;
}
十六、natapp外网穿透
16.1 注册安装
登录https://natapp.cn
注册账号
设置对外开放的端口
下载对应的客户
十七、tomcat remote debug(idea)(参照文档:http://www.cnblogs.com/XuYankang/p/jpda.html)
1、tomcat/bin/文件夹中,新建文件setenv.bat(或者setenv.sh,根据你的操作系统),输入:
export JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
#suspend:如果是y,则需要等B机器上的debugger开启后,程序才会开始运行。否则,程序启动时候不会挂起,直接运行。
2、idea
-edit configurations
-remote
-host中填写远程服务器ip,端口填写5005(与上面的address=5005一致)
3、配置完后,启动服务器tomcat,debug本地工程
4、通过命令lsof -i:5005 可以查看到建立的连接
十八、部署与自动发布
#!/bin/sh
echo "===========进入git项目dida_business目录=============" &
cd /data/web/dida_business&
echo "==========git切换分之到dev===============" &
git checkout dev &
echo "==================git fetch======================" &
git fetch &
echo "==================git pull======================" &
git pull &
echo "===========编译并跳过单元测试====================" &
mvn clean package -Dmaven.test.skip=true &
echo "============删除旧的ROOT.war===================" &
rm /data/developer/tomcat8/webapps/ROOT.war &
echo "======拷贝编译出来的war包到tomcat下-ROOT.war=======" &
cp /data/web/dida_business/target/dida_business-1.0-SNAPSHOT.war /data/developer/tomcat8/webapps/ROOT.war &
echo "============删除tomcat下旧的ROOT文件夹=============" &
rm -rf /data/developer/tomcat8/webapps/ROOT &
echo "====================关闭tomcat=====================" &
/data/developer/tomcat8/bin/shutdown.sh &
echo "================sleep 10s=========================" &
for i in {1..10}
do
echo $i"s"
sleep 1s
done
echo "====================启动tomcat=====================" &
/data/developer/tomcat8/bin/startup.sh 标签:return,String,企业级,static,Java,电商,com,public,BigDecimal From: https://www.cnblogs.com/anquing/p/17603668.html