首页 > 编程语言 >Java(从零到企业级电商项目实战)学习笔记

Java(从零到企业级电商项目实战)学习笔记

时间:2023-08-03 17:14:18浏览次数:28  
标签:return String 企业级 static Java 电商 com public BigDecimal

资料网站: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

相关文章

  • 前端性能优化的利器 ——— 浅谈JavaScript中的防抖和节流
    防抖和节流函数是工作中两种常用的前端性能优化函数,今天我就来总结一下什么是防抖和节流,并详细说明一下如何在工作中应用防抖和节流函数什么是防抖和节流?在JavaScript中,防抖(debounce)和节流(throttle)是用来限制函数执行频率的两种常见技术。防抖(debounce)是指在某个时间段内......
  • Java后端02(jsp)
    jsp​ servlet是无法将后端获取的数据传递给html页面的,无法再servlet中通过转发或者是重定向的方式,给html页面传递响应的后端数据,servlet中由于拼接过于繁琐,是不适合写html的因此引入了jsp,既可以编写html标签,也可以写Java代码,<dependency><groupId>javax.serv......
  • Java后端03(浅谈注解)
    注解功能一:提示信息功能二:存储信息​ 注解需要定义注解类,类对象需要有落实的实体,注解可以出现在类Class上,方法Method上,成员变量Field上以及构造方法Constructor上,注解对象需要被添加注解的实体所对应的反射对象进行获取,人话:要获得注解信息,首先要获得修饰的东西的反射......
  • Java后端04(Ajax异步加载)
    Ajax异步加载jsp涉及到的问题每次哪怕是页面的局部修改,都会导致服务器重新获取,页面的所有信息,导致jsp页面的效率非常低下前端jsp页面无法脱离服务器运行前端和后端无法分离开发,开发效率低下Ajax介绍​ Ajax是由谷歌开发,底层是通过js开发的技术,如果页面内容只是局部......
  • Javascript 方法有多个参数有默认值,但是只想为其中某个参数赋值
    例子:functionlog(a,b=2,c=3,d=4){console.log(a,b,c,d)}log(1);//output:1234log(1,3,6,9);//1369log(1,undefined,9,12);//12912log(1,undefined,0);//1204log(1,undefined,undefined,16);//12316就是在你想......
  • Java入门题-输入一个字母,判断是不是大写,是直接输出,不是转换成大写输出
    重要:利用ASCII码判断字母是否是大小写需要引用Scanner方法 代码:importjava.util.Scanner;1、利用char数组转化的优点在于面对字符串,也可以批量对单个字符操作Scannerget_letter=newScanner(System.in);StringgetLetter=get_letter.next();charss[]=getLetter.to......
  • python以及java环境搭建+解决不同版本环境共存问题
    1.搭建python环境1.安装python3.9.7,选择自定义安装、将文件添加至环境路径,然后选择下一步。2.默认、然后选择下一步。3.选择适用于所有使用者,选择自己想要存放的路径,然后选择点击安装。等待片刻。。。4.点击安装完成。使用cdm命令,输入python查看是否安装成功。5.安装python2......
  • android调用javascript传多个参数
    Stringparam1="Hello";Stringparam2="World";Stringscript="yourJavaScriptFunction('"+param1+"','"+param2+"');";webView.loadUrl("javascript:"+"yourJava......
  • Java入门题-输入3个数字比大小
    重要:能想明白输入数字之间大小比较的逻辑方式与之前相同,有关输入类型相关的,就需要引用Scanner方法 代码:不要忘记先 importjava.util.Scanner;1、复杂版-同时也是最开始思考时的思路版本intsave_number1=0,save_number2=0,save_number3=0;for(inti=1;i<4;......
  • Java入门题-判断输入数字是奇数还是偶数
    重要:需要引入Scanner方法importjava.util.Scanner; 代码:主要是通过求余方式处理解决 Scannerget_number=newScanner(System.in);intgetNumber=get_number.nextInt();if(getNumber%2==0){System.out.println(getNumber+"为偶数");}else{System.o......