日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

每天一剂开发良药

發布時間:2024/3/13 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 每天一剂开发良药 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

主要介紹一些小技巧之類,是為備忘也。

TypeScript 導入 *.vue 報錯:Cannot find module

如圖

創建一個 shims.d.ts 文件,放置到 src/globalDeclare 中。

declare module '*.vue' {import Vue from 'vue';export default Vue; }

typescript-eslint 自作多情提示 xxx is assigned a value but never used

eslintrc.js 加上

"no-unused-vars": "off","@typescript-eslint/no-unused-vars": ["error"],"@typescript-eslint/ban-ts-comment": "off","@typescript-eslint/explicit-function-return-type": "off","@typescript-eslint/no-explicit-any": ["off"]

ViewUI Table 單元格 文本將不換行,超出部分顯示為省略號

組件寫法,比較麻煩

<Table :columns="columns1" :data="list"><template slot-scope="{ row, index }" slot="action"><a href="javascript:void(0);" @click="handleEdit(row, index)">編輯</a> <Divider type="vertical" /><Poptip confirm transfer title="是否要刪除此行?" @on-ok="handleDelete(index)"><a href="javascript:void(0);" style="color:red;">刪除</a></Poptip></template><template slot-scope="{ row }" slot="url"><Ellipsis :text="row.url" :length="50" tooltip :transfer="true"></Ellipsis></template> </Table>

其實可以在列配置中聲明:

{ title: '鏈接地址', minWidth: 190, key: 'url', ellipsis:true, tooltip:true },

另外每一列設置 width/minWidth 就可以保證不受瀏覽器寬度擠壓

Vue 工程里面怎么引入公共的 Less 樣式庫?

例如 Less 的函數。

安裝下面插件

  • “less”: “^3.0.4”,
  • “less-loader”: “^5.0.0”,
  • “style-resources-loader”: “^1.4.1”

打開 vue.config.js 配置文件,

module.exports = {pluginOptions: {'style-resources-loader': {preProcessor: 'less',patterns: ['C:\\code\\ajaxjs\\aj-js\\aj-ui\\src\\style\\common-functions.less']}},lintOnSave: true,devServer: {overlay: {warnings: true,error: true}} };

路徑寫死,改相對路徑

var path = require("path");module.exports = {pluginOptions: {'style-resources-loader': {preProcessor: 'less',patterns: [path.resolve(__dirname, './src/style/common-functions.less')]}},lintOnSave: true,devServer: {overlay: {warnings: true,error: true}} };

MySQL varchar 文本包含數字的計數器

需求:如果重復值,則自增 1、2、3……
思路:先查詢是否重復:

SELECT id FROM ${tableName} WHERE urlDir = ? AND datasourceId = ? LIMIT 1

如果是,獲取最大值 MaxId,通過正則查詢、排序,注意參數拼接了字符串(參數就是重復值)。

SELECT urlDir FROM ${tableName} WHERE urlDir REGEXP CONCAT(?, '_[0-9]+$') AND datasourceId = ? ORDER BY urlDir DESC LIMIT 1

若無則 1,有則 MaxId++。

快速 SQL 轉換 Java Bean/ POJO

找過好幾個的,都不太符合需求,于是自己寫個腳本,也很快。

<html><head><meta charset="utf-8" /><title>SQL2pojo</title> </head><body><textarea id="sql" rows="20" cols="100"></textarea><br /><br /><button onclick="sql2pojo()">SQL2pojo</button><pre></pre> </body> <script>let tpl = '';function sql2pojo() {let sql = document.querySelector("#sql").value;let arr = sql.match(/CREATE TABLE `(?:\w+|_)` \(((\s|\S)+)(?=PRIMARY KEY)/);let result = arr[1].trim();arr = result.split(',');let output = [];arr.forEach(item => {if (item) {item = item.trim();console.log(item);let _arr = item.match(/^`(\w+)`\s+((?:\w|\(|\))+).*COMMENT '(.*?)'/);let _t = _arr[2], type = 'Object';if (_t.indexOf('VARCHAR') != -1 || _t.indexOf('TEXT') != -1)type = 'String';if (_t.indexOf('TINYINT(1)') != -1)type = 'Boolean';else if (_t.indexOf('TINYINT') != -1)type = 'Integer';else if (_t.indexOf('INT') != -1)type = 'Long';if (_t.indexOf('DATETIME') != -1 || _t.indexOf('DATE') != -1)type = 'Date';tpl = ` /*** ${_arr[3]}*/ private ${type} ${_arr[1]};`;// console.log(tpl);output.push(tpl);}});document.querySelector('pre').innerHTML = output.join('<br />');} </script></html>

Vue+TS 工程發布 npm 組件不能攜帶 *.vue 問題

當前 Vue 工程既有網站,也希望發布為 npm 組件。使用 tsc 編譯結果到 dist 目錄,注意下面問題:

  • 配置 main 文件,不然 import 包時候會 undefined。具體就是在 package.json 配置結果目錄的 index.js 和 index.d.ts。
  • 編譯依靠不能使用 vue-cli-service build,那是編譯網站的。我們目的是打包組件,使用 tsc 即可。其實就是生成 js 和 map,我的tsconfig.json 配置如下。
{"compilerOptions": {"target": "esnext","module": "esnext","strict": false,"jsx": "preserve","importHelpers": true,"moduleResolution": "node","skipLibCheck": true,"esModuleInterop": true,"allowSyntheticDefaultImports": true,"sourceMap": true,"baseUrl": ".","outDir": "./dist","types": ["webpack-env"],"paths": {"@/*": ["src/*"]},"lib": ["esnext", "dom", "dom.iterable", "scripthost"]},"include": ["src/**/*.ts","src/**/*.tsx","src/**/*.vue","tests/**/*.ts","tests/**/*.tsx"],"exclude": ["node_modules", "dist"] }

其中的 include "src/**/*.vue", 其實沒作用,因為 tsc 只管 js/ts/json 的編譯,其他文件它不處理的。那么問題來了——打包就需要 *.vue 文件,——我搜索了很久終于找到一個比較簡單的方法,就是直接復制過去。package.json 增加一個 scripts 命令:

"release": "tsc && xcopy src\\components dist\\components /s /y /d && npm publish --access public",

tsc 編譯后通過 DOS 命令 xcopy 復制目錄,/s 表示包含所有子目錄和文件, /y 表示不確認并進行覆蓋,/d 表示文件日期對比,日期較新的不覆蓋。

另外,tsc 不會覆蓋現有文件,所以最好先刪除一下 dist 目錄再 npm run release。

Vue 中單頁面組件中 render 函數不運行?

要用 render 函數,把 <template> 給去掉。https://segmentfault.com/q/1010000016677825

簡單使得元素可拖動

/*** 使得面板浮動,可拖放*/ export default function float() {setTimeout(() => {let el: HTMLElement = this.$el;let rect: DOMRect = el.getBoundingClientRect();let controls: HTMLElement = (<HTMLElement>el.querySelector('.controls'));let top: number = rect.top - el.offsetTop;let left: number = rect.left - el.offsetLeft - controls.offsetWidth - 10;let style: CSSStyleDeclaration = controls.style;style.top = top + 'px';style.left = left + 'px';makeDD(controls, <HTMLElement>controls.querySelector('.movable'));let btns: HTMLElement = (<HTMLElement>el.querySelector('.btns'));top = rect.top - el.offsetTop - btns.offsetHeight - 10;left = rect.left - el.offsetLeft;style = btns.style;style.top = top + 'px';style.left = left + 'px';makeDD(btns, <HTMLElement>btns.querySelector('.movable'));}, 10); }/*** 拖放* * @param box 被拖放的區域* @param dragBar 拖放的按鈕*/ function makeDD(box: HTMLElement, dragBar: HTMLElement): void {// 鼠標按下的函數dragBar.onmousedown = function (oEvent: MouseEvent) {// 求出鼠標和box的位置差值let x: number = oEvent.clientX - box.offsetLeft, y: number = oEvent.clientY - box.offsetTop;// 鼠標移動的函數// 把事件加在document上,解決因為鼠標移動太快時,鼠標超過box后就沒有了拖拽的效果的問題document.onmousemove = function (oEvent: MouseEvent) {// 只能拖動窗口標題才能移動if (oEvent.target != dragBar) {// return;}// 保證拖拽框一直保持在瀏覽器窗口內部,不能被拖出的瀏覽器窗口的范圍let l: number = oEvent.clientX - x, t = oEvent.clientY - y;let doc: HTMLElement = document.documentElement;if (l < 0)l = 0;else if (l > doc.clientWidth - box.offsetWidth)l = doc.clientWidth - box.offsetWidth;if (t < 0)t = 0;else if (t > doc.clientHeight - box.offsetHeight)t = doc.clientHeight - box.offsetHeight;box.style.left = l + "px";box.style.top = t + "px";}// 鼠標抬起的函數document.onmouseup = function () {document.onmousemove = document.onmouseup = null;}// 火狐瀏覽器在拖拽空div時會出現 bug return false阻止默認事件,解決火狐的bugreturn false;} }

Spring MVC 加入 JSP 支持

/WEB-INF/jsp/ .jsp

解決煩人的 sockjs-node/info 跨域問題

打開 build/webpack.conf.js

const config = {resolve: {alias: {}},devServer: {// host: 'localhost',disableHostCheck: true,public: '0.0.0.0'}, };module.exports = config;

JSP 頁面異常

JSP 需要加上下面代碼,運行時才不會出現 java.lang.IllegalStateException: getOutputStream() has already been called …等異常。

/**** @param ctx 頁面上下文*/ public static void fix(PageContext ctx) {HttpServletResponse response = (HttpServletResponse) ctx.getResponse();try {OutputStream out = response.getOutputStream();out.flush();out.close();response.flushBuffer();ctx.getOut().clear();ctx.pushBody();// out = pageContext.pushBody();} catch (IOException e) {LOGGER.warning(e);} }

參考 JSP 內置對象 out 和 response.getWrite() 的區別

  • http://blog.sina.com.cn/s/blog_7217e4320101l8gq.html
  • http://www.2cto.com/kf/201109/103284.html

響應禁止緩存

/*** 新的輸出,不要緩存** @return 當前對象*/ public MvcOutput noCache() {setHeader("Pragma", "No-cache");setHeader("Cache-Control", "no-cache");setDateHeader("Expires", 0);return this; }

返回到前一頁并刷新

window.location = document.referrer;

靜態的錯誤提示頁

<title>操作錯誤</title> <meta charset="utf-8" /> <div style="height: 100%%; display: flex; justify-content: center; align-items: center;"><table><tr><td align="center"> <svg width="150px" viewBox="0 0 1000 1000"><g><path fill="#ea8010" d="M500,10c-46.7,0-84.5,38-84.5,84.9v573.7c0,46.9,37.8,84.9,84.5,84.9c46.7,0,84.5-38,84.5-84.9V94.9C584.5,48,546.7,10,500,10z M500,821c-46.7,0-84.5,37.8-84.5,84.5c0,46.7,37.8,84.5,84.5,84.5c46.7,0,84.5-37.8,84.5-84.5C584.4,858.9,546.6,821,500,821z" /></g></svg></td></tr><tr><td align="center"><br />%s<br /><a href="javascript:history.go(-1);">返回</a></td></tr></table> </div>

隨時隨地獲取 Request/Response

為獲取請求的上下文,能夠在控制器中拿到最常用的對象,例如 HttpServletRequest 和 HttpServletResponse 等的對象(甚至 Web App 的啟動上下文( 在 web.xml 中配置的參數)),因此還需要設計一個 RequestHelper 類,通過 ThreadLocal 讓控制器能輕易地訪問到這些對象。

一個容器,向這個容器存儲的對象,在當前線程范圍內都可以取得出來,向 ThreadLocal 里面存東西就是向它里面的 Map 存東西的,然后 ThreadLocal 把這個 Map 掛到當前的線程底下,這樣 Map 就只屬于這個線程了。

private static ThreadLocal<HttpServletRequest> threadLocalRequest = new ThreadLocal<>();private static ThreadLocal<HttpServletResponse> threadLocalResponse = new ThreadLocal<>();/*** 保存一個 request 對象** @param req 請求對象*/ public static void setHttpServletRequest(HttpServletRequest req) {threadLocalRequest.set(req); }/*** 獲取請求對象** @return 請求對象*/ public static HttpServletRequest getHttpServletRequest() {return threadLocalRequest.get(); }/*** 保存一個 response 對象** @param resp 響應對象*/ public static void setHttpServletResponse(HttpServletResponse resp) {threadLocalResponse.set(resp); }/*** 獲取上下文中 response 對象** @return response 響應對象*/ public static HttpServletResponse getHttpServletResponse() {HttpServletResponse resp = threadLocalResponse.get();if (resp == null)throw new RuntimeException("響應對象未初始化");return resp; }/*** 清空 request 和 response*/ public static void clean() {threadLocalRequest.set(null);threadLocalResponse.set(null); }

javax.servlet.forward.request_uri 之作用

綁定 JSP 時候,獲取原請求的 uri,而非模版所在的 uri。

生成模擬數據的 SQL

隨機 SET

UPDATE enterprise_tract_meeting SET TYPE = ELT(FLOOR(RAND() * 3 + 1), 1, 2, 3);

IDEA 設置 Javac 編譯參數對于 Maven 無效

Eclipse 不會那樣,Idea 2018 無效。你 re-build 然后 deploy 那樣就可以。下面的 pom.xml 永久解決。

<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.9.0</version><configuration><source>1.8</source><target>1.8</target><compilerArgs><arg>-parameters</arg><!-- IDEA 設置 Javac 編譯參數對于 Maven 無效 --></compilerArgs></configuration></plugin></plugins> </build>

Mysql 創建流水號

方法一,觸發器:

CREATE TRIGGER saledetail_id BEFORE INSERT ON saledetail FOR EACH ROW BEGINdeclare n int;select IFNULL(max(right(ItemID,4)),0) into n from saledetail where mid(ItemID,1,8)=DATE_FORMAT(CURDATE(),'%Y%m%d');set NEW.ItemID=concat(DATE_FORMAT(CURDATE(),'%Y%m%d'),right(10001+n,4)); END;

注意在插入的時候主鍵要設置一個默認值才能插入進去,這里我設置的是空字符串 ""。

方法二:

SELECTsubstr(CONCAT('0000', (IFNULL(MAX(substr(fund_code, -3)),0) + 1)), -3) FROM enterprise_trace_fund WHERE fund_code LIKE 'DF20211223%'

fund_code 字段值就是傳過來的字符串,mysql 數據庫會自動進行匹配,然后自行自增。fund_code 字段值如果加入日期值,三位的流水號一般是夠用的。

但這做法并不能在并發下保證流水號的唯一性。可以用 MySQL 寫鎖(select...for update,也叫 X 鎖,排它鎖)。

方法三:

CREATE DEFINER=`root`@`localhost` PROCEDURE `GetSerialNo`(IN tsCode VARCHAR(50),OUT result VARCHAR(200) ) BEGIN DECLARE tsValue VARCHAR(50); DECLARE tdToday VARCHAR(20); DECLARE nowdate VARCHAR(20); DECLARE tsQZ VARCHAR(50); DECLARE t_error INTEGER DEFAULT 0; DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1; START TRANSACTION; /* UPDATE sys_sno SET sValue=sValue WHERE sCode=tsCode; */SELECT sValue INTO tsValue FROM sys_sno WHERE sCode=tsCode for UPDATE; SELECT sQz INTO tsQZ FROM sys_sno WHERE sCode=tsCode ; -- 因子表中沒有記錄,插入初始值 IF tsValue IS NULL THEN SELECT CONCAT(DATE_FORMAT(NOW(),'%y%m'),'0001') INTO tsValue; UPDATE sys_sno SET sValue=tsValue WHERE sCode=tsCode ; SELECT CONCAT(tsQZ,tsValue) INTO result; ELSE SELECT SUBSTRING(tsValue,1,4) INTO tdToday; SELECT CONVERT(DATE_FORMAT(NOW(),'%y%m'),SIGNED) INTO nowdate;-- 判斷年月是否需要更新IF tdToday = nowdate THEN SET tsValue=CONVERT(tsValue,SIGNED) + 1; ELSE SELECT CONCAT(DATE_FORMAT(NOW(),'%y%m') ,'0001') INTO tsValue ; END IF; UPDATE sys_sno SET sValue =tsValue WHERE sCode=tsCode; SELECT CONCAT(tsQZ,tsValue) INTO result; END IF; IF t_error =1 THEN ROLLBACK; SET result = 'Error'; ELSE COMMIT; END IF; SELECT result ; END;

出處:https://www.jianshu.com/p/d7570564f104

Eclipse Maven Dependencies下引入本地工程的 jar 包卻變成源碼文件夾

How to tell Maven to include the jar dependency, not the subproject source directory in Eclipse?
開始找到這個方法 https://blog.csdn.net/Harbourside1/article/details/111871122 是不對的,后來找到這個 https://blog.csdn.net/oh_maxy/article/details/48347897,正確!就是沒有選 Utility Module 這個導致的:

Spring MVC Java 代替 xml

web.xml

原來 web.xml:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"><!-- 啟用 Spring --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- // --><!-- 對 Request、Response 的擴展 --><filter><filter-name>InitMvcRequest</filter-name><filter-class>com.ajaxjs.util.spring.InitMvcRequest</filter-class></filter><filter-mapping><filter-name>InitMvcRequest</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 全部允許跨域 --><filter><filter-name>Cors</filter-name><filter-class>com.ajaxjs.util.spring.CorsFilter</filter-class></filter><filter-mapping><filter-name>Cors</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- // --> </web-app> <!-- // -->

采用 Java:

public abstract void initWeb(ServletContext servletContext);@Override public void onStartup(ServletContext servletCxt) {LOGGER.info("WEB 程序啟動中……");servletCxt.setInitParameter("contextConfigLocation", "classpath:applicationContext.xml");servletCxt.addListener(new ContextLoaderListener()); // 監聽器FilterRegistration.Dynamic filterReg = servletCxt.addFilter("InitMvcRequest", new InitMvcRequest());filterReg.addMappingForUrlPatterns(null, true, "/*");initWeb(servletCxt);…… }

數據庫連接池失效

注意 Connection 一定要關閉!

<!-- 配置數據源 https://blog.csdn.net/syslbjjly/article/details/97108560 --> <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver" /><!--內網數據庫 --><property name="url" value="jdbc:mysql://10.201.xxx.xxx:3306/bdp?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false"></property> <property name="username" value="root"></property> <property name="password" value="xxx"></property> <!-- 驗證連接是否有效,(String) SQL 查詢,用來驗證從連接池取出的連接,在將連接返回給調用者之前。 如果指定,則查詢必須是一個 SQL SELECT 并且必須返回至少一行記錄查詢不必返回記錄,但這樣將不能拋出 SQL 異常 --><property name="validationQuery" value="SELECT 1" /><!-- (long) 避免過度驗證,保證驗證不超過這個頻率——以毫秒為單位。如果一個連接應該被驗證, 但上次驗證未達到指定間隔,將不再次驗證。 30000(30秒) --><property name="validationInterval" value="18800" /><!-- 驗證失敗時,是否將連接從池中丟棄 --><property name="testWhileIdle" value="true" /><property name="testOnBorrow" value="true" /><property name="testOnReturn" value="true" /> </bean>

MVC 框架中靜態資源的劃分

一般都是 MVC 框架接收所有的請求然后分別處理,去控制器的,還是靜態資源的,

import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.util.regex.Pattern;import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest;/*** 自定義請求對象,對請求對象和響應對象有很多需要擴展的地方* * @author Frank Cheung<sp42@qq.com>**/ //@Component public class InitMvcRequest implements Filter {/*** 字符串判斷是否靜態文件*/private static final Pattern IS_STATIC = Pattern.compile("\\.jpg|\\.png|\\.gif|\\.js|\\.css|\\.less|\\.ico|\\.jpeg|\\.htm|\\.swf|\\.txt|\\.mp4|\\.flv");@Overridepublic void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {HttpServletRequest _req = (HttpServletRequest) req;try {// 為防止中文亂碼,統一設置 UTF-8,設置請求編碼方式_req.setCharacterEncoding(StandardCharsets.UTF_8.toString());} catch (UnsupportedEncodingException e) {}if (!IS_STATIC.matcher(_req.getRequestURI()).find())chain.doFilter(req, resp);elsechain.doFilter(req, resp);}@Overridepublic void init(FilterConfig arg0) {}@Overridepublic void destroy() {} }

關于 Tomcat 的一些冷知識

Tomcat 自帶許多有用的組件,直接可用。

  • Tomcat 也有自己的數據庫連接池 jdbc-pool
  • 自帶管理監控工具 Manager,若不滿可以參考 PSI Probe
  • 想要一個模板系統?用 Tomcat 自帶的 EL表達式解析器 吧,可惜的是我找不到相關的教程……只能用 Spring 的
  • 可插拔以及 SCI 的實現原理,以及 Wrapper
  • 對于特定資源的保護,Tomcat 提供了安全域的功能實現
  • Tomcat 也可以做 SSO……
  • 一堆過濾器

我們知道 Spring 本身自帶一堆過濾器,Tomcat 也有呀。

  • SetCharacterEncodingFilter 解決亂碼問題
  • CorsFilter 跨域問題
  • CsrfPreventionFilter 防止跨站請求偽造(CSRF)
  • RemoteIpFilter/RemoteIpValve 獲取客戶端真實 ip
  • RemoteHostFilter、RemoteAddrFilter 獲取客戶端Host、Ip

觀察代碼執行時間

分析效率用,用 Spring 的 StopWatch。

StopWatch sw = new StopWatch();sw.start("起床"); Thread.sleep(1000); sw.stop();sw.start("洗漱"); Thread.sleep(2000); sw.stop();System.out.println(sw.prettyPrint()); System.out.println(sw.getTotalTimeMillis()); System.out.println(sw.getLastTaskName()); System.out.println(sw.getLastTaskInfo()); System.out.println(sw.getTaskCount());

如何對Spring MVC中的 Controller 進行單元測試

例子如下,參見。

@ContextConfiguration(locations = { "classpath*:applicationContext.xml" }) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class TestAlipay {MockMvc mockMvc;@AutowiredWebApplicationContext wac;@Beforepublic void init() {mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();}// @Testpublic void testCommonUpload() throws Exception {File file = new File("C:\\Users\\frank\\Desktop\\abldj75zav.png");byte[] bytes = FileHelper.openAsByte(file);String filenmae = "abldj75zav.png";MockMultipartFile mockMultipartFile = new MockMultipartFile("file", filenmae, MediaType.MULTIPART_FORM_DATA_VALUE, bytes);ResultActions andDo = mockMvc.perform(multipart("/upload").file(mockMultipartFile)).andExpect(status().isOk()).andExpect(content().string(filenmae)).andDo(print());System.out.println(andDo);assertNotNull(andDo);}@Testpublic void testNso() {assertTrue(true);}}

MySQL 于 Tomcat 的沖突

出現異常:checkStateForResourceLoading Illegal access,Eclipse 提示異常,生產環境應該不會。寫一個 Listener 解決。出處:1、2。

import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Enumeration;import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener;import org.springframework.stereotype.Component;import com.ajaxjs.Version; import com.mysql.cj.jdbc.AbandonedConnectionCleanupThread;/*** Eclipse 提示異常,生產環境應該不會* * @author Frank Cheung<sp42@qq.com>**/ @WebListener @Component public class ContainerContextClosedHandler implements ServletContextListener {@SuppressWarnings("deprecation")@Overridepublic void contextDestroyed(ServletContextEvent arg0) {if (Version.isDebug) {Enumeration<Driver> drivers = DriverManager.getDrivers();Driver driver = null;// clear driverswhile (drivers.hasMoreElements()) {try {driver = drivers.nextElement();DriverManager.deregisterDriver(driver);} catch (SQLException ex) {// deregistration failed, might want to do something, log at the very least}}// MySQL driver leaves around a thread. This static method cleans it up.try {AbandonedConnectionCleanupThread.shutdown();} catch (Exception e) {// again failure, not much you can do}}}@Overridepublic void contextInitialized(ServletContextEvent arg0) {System.out.println("------------------------------------------");}}

錯誤信息 HTML

急用一個錯誤提示頁面:

<title>操作錯誤</title> <meta charset="utf-8" /> <div style="height: 100%%; display: flex; justify-content: center; align-items: center;"><table><tr><td align="center"> <svg width="150px" viewBox="0 0 1000 1000"><g><path fill="#ea8010" d="M500,10c-46.7,0-84.5,38-84.5,84.9v573.7c0,46.9,37.8,84.9,84.5,84.9c46.7,0,84.5-38,84.5-84.9V94.9C584.5,48,546.7,10,500,10z M500,821c-46.7,0-84.5,37.8-84.5,84.5c0,46.7,37.8,84.5,84.5,84.5c46.7,0,84.5-37.8,84.5-84.5C584.4,858.9,546.6,821,500,821z" /></g></svg></td></tr><tr><td align="center"><br />錯誤XXXX<br /><a href="javascript:history.go(-1);">返回</a></td></tr></table> </div>

效果如圖

Eclipse 重啟 Tomcat 提示 May be locked by another process

每次重啟都會,很煩。原因是有打開文件的文件未關閉,Files.lines() 打開返回的 Stream<String> 也要關閉!

StringBuilder sb = new StringBuilder();try (Stream<String> lines = Files.lines(path, encode);) {lines.forEach(str -> sb.append(str));return sb.toString(); } catch (IOException e) {LOGGER.warning(e); }

典型的 Spring MVC 控制器單測

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext;import com.ajaxjs.data_service.api.ApiController;@ContextConfiguration(locations = { "classpath*:applicationContext.xml" }) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class TestSsoAccessTokenInterceptor {MockMvc mockMvc;@AutowiredWebApplicationContext wac;@AutowiredApiController apiController;@Beforepublic void init() {apiController.initCache();mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();}@Testpublic void test() throws Exception {MockHttpServletRequestBuilder req = get("/user_api").param("redirect_uri", "https://www.qq.com").param("client_id", "dss23s");mockMvc.perform(req).andExpect(status().is2xxSuccessful()).andDo(print()).andReturn().getResponse();} }

規避使用 PowerDesigner

老版本 12 沒有上傳數據的功能,可以規避版權檢測。通用破解方法:修改安裝目錄下的 pdflm12.dll 文件,使用二進制編輯器打開此文件,查找:83 C4 14 8B 85 E4 FE FF FF將此字符串改為 83 C4 14 33 C0 90 90 90 90

推薦 Hex Editor: wxMEdit。 Frhed 也小巧,但找不到搜索 Hex 的方法,郁悶。

使用正則提取網頁中a標簽的鏈接和標題

import java.util.regex.Matcher; import java.util.regex.Pattern;public class Test1 {public static void main(String[] args) {String str1 = "<a href=\"https://www.zifangsky.cn/2015/10/hello-world/\" title=\"\" data-original-title=\"Hello World\">Hello World</a>";String str2 = "<a href=\"http://banzhuanboy.com/363.html\" class=\"post-feature\" \">123</a>";String str3 = " <a class=\"article-title\" href=\"/2015/12/17/Webstorm-Hotkeys-For-Mac/\">c</a>";String str4 = " <a rel=\"bookmark\" title=\"Permanent Link to 黑客組織‘SkidNP’涂改了Phantom Squad的網站首頁\" href='12/hack-30127.htm'>黑</a>";String str5 = "<a href=\"http://www.imorlin.com/2015/12/24/1-3/\" title=\"\" data-original-title=\"2015圣誕節雪花代碼[天貓+C店]\"> 2015圣誕節雪花代碼[天貓+C店] <span class=\"label label-new entry-tag\">New</span> </a>";Pattern pattern = Pattern.compile("<a.*?href=[\"']?((https?://)?/?[^\"']+)[\"']?.*?>(.+)</a>"); Matcher matcher = pattern.matcher(str1);if(matcher.find()){String link = matcher.group(1).trim();String title = matcher.group(3).trim();if(!link.startsWith("http")){if(link.startsWith("/"))link = "https://www.zifangsky.cn" + link;else link = "https://www.zifangsky.cn" + link; }System.out.println("link: " + link);System.out.println("title: " + title);}} }

解釋:

1 選取了幾個有代表性的 a 標簽樣式進行測試

2 關于正則匹配模式”<a.*?href=[\”‘]?((https?://)?/?[^\”‘]+)[\”‘]?.*?>(.+)</a>“的說明:

i)<a.*?href= <a 開頭,中間緊跟著有0個或者多個字符,然后再跟著 href=

ii)[\”‘]?((https?://)?/? 一個或者0個的” 或者 ‘ ,然后再跟著0個或者一個的http://或者https:// ,再跟著0個或者1個的 /

iii)[^\”‘]+ 表示1個以上的不包括’或者” 的任意字符

iv)[\”‘]?表示鏈接后面的’或者” 當然也可能沒有

后面的可以根據前面的自己推理,就不解釋了

3 matcher.group(1)表示取出鏈接,也就是第二個()的內容(PS:第一個()表示的是整個正則表達式,默認省略了),在正則中是這一段規則:((https?://)?/?[^\”‘]+)

4 matcher.group(3) 同理可知,對應的是這一段規則:(.+)

5 對于代碼中的 https://www.zifangsky.cn ,這是由于部分鏈接使用了相對路徑,比如說:href=’12/hack-30127.htm’ 。這時我們就需要加上它的域名,當然需要根據實際情況來加。這里我就隨便亂加了

構建可重復讀取 inputStream 的 request

我們知道,request 的 inputStream 只能被讀取一次,多次讀取將報錯,那么如何才能重復讀取呢?答案之一是:增加緩沖,記錄已讀取的內容。

import org.springframework.mock.web.DelegatingServletInputStream;import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import java.io.*;/*** request wrapper: 可重復讀取request.getInputStream*/ public class RepeatedlyReadRequestWrapper extends HttpServletRequestWrapper {private static final int BUFFER_START_POSITION = 0;private static final int CHAR_BUFFER_LENGTH = 1024;/*** input stream 的buffer*/private final String body;/*** @param request {@link javax.servlet.http.HttpServletRequest} object.*/public RepeatedlyReadRequestWrapper(HttpServletRequest request) {super(request);StringBuilder stringBuilder = new StringBuilder();InputStream inputStream = null;try {inputStream = request.getInputStream();} catch (IOException e) {log.error("Error reading the request body…", e);}if (inputStream != null) {try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {char[] charBuffer = new char[CHAR_BUFFER_LENGTH];int bytesRead;while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {stringBuilder.append(charBuffer, BUFFER_START_POSITION, bytesRead);}} catch (IOException e) {log.error("Fail to read input stream",e);}} else {stringBuilder.append("");}body = stringBuilder.toString();}@Overridepublic ServletInputStream getInputStream() throws IOException {final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());return new DelegatingServletInputStream(byteArrayInputStream);} }

接下來,需要一個對應的 Filter

import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import java.io.IOException;public class RepeatlyReadFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {if (request instanceof HttpServletRequest) {request = new RepeatedlyReadRequestWrapper((HttpServletRequest) request);}chain.doFilter(request, response);}@Overridepublic void destroy() { } }

出處。

使用 Javassist 在 tomcat 容器中實現動態 Mock

在某些復雜場景下,我們需要對運行在 tomcat 容器中部分功能進行 mock(替換其實現),但該部分功能散落在各處,我們希望不修改源代碼以非侵入的方式來實現 Mock,在這種情況下,我們可以應用 Javassist 來實現。

使用Javassist在tomcat容器中動態替換源碼來實現動態 Mock
我們可以定義一個 ContextListener 的實例,在 tomcat 啟動時通過 Javassis t對源代碼進行動態替換,來實現 mock 的功能。

import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import java.util.HashMap; import java.util.Map; import org.lightfw.utilx.dynamic.JavassistUtil;public class MockListener implements ServletContextListener {public void contextInitialized(ServletContextEvent contextEvent) {log.info("Context Listener start................");//將TestController類的test方法的實現替換為"return 1;"JavassitUtil.replaceMethodBody("com.xx.web.controller.TestController", "test", "return 1");log.info("Context Listener started");}public void contextDestroyed(ServletContextEvent sc) {log.info("Context Listener stopped");} }

Javassist 相關代碼,需要使用的工具類。

private static ClassPool classPool;static {classPool = ClassPool.getDefault();classPool.insertClassPath(new ClassClassPath(JavassitUtil.class)); //主要用于web環境}/*** 替換方法體** @param className 類名,如:foo.Student* @param methodName 方法名* @param newMethodBody 新的方法體,如:"System.out.println(\"this method is changed dynamically!\");"*/public static void replaceMethodBody(String className, String methodName, String newMethodBody) {try {CtClass clazz =classPool.get(className);CtMethod method = clazz.getDeclaredMethod(methodName);method.setBody(newMethodBody);clazz.toClass();} catch (NotFoundException | CannotCompileException e) {throw new RuntimeException(e);}}

出處。

Spring 單元測試干掉 xml

Spring 全注解化了,但單測還是有個 xml 的小尾巴。

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 掃描的包 --><context:component-scanbase-package="com.ajaxjs.data_service, com.ajaxjs.entity, com.ajaxjs.rpc" /> </beans>

二貨 Eclipse 整天校驗這個 xml,還卡住。——其實可以創建一個 Java 類來代替 XML 文件:

import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;@Configuration @ComponentScan({ "com.ajaxjs.data_service", "com.ajaxjs.entity", "com.ajaxjs.rpc" }) public class TestConfig {}

單測:

import static org.junit.Assert.assertNotNull;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration;import com.ajaxjs.entity.datadict.DataDictService;@ContextConfiguration(classes = TestConfig.class) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class TestDataDict {@AutowiredDataDictService dataDictService;@Testpublic void test() {assertNotNull(dataDictService);} }

Java 枚舉技巧

枚舉除了名稱還有常量值,可以用 int 保存。

public static enum Lock {ADD_LOCK(0), UNLOCK(1);private int value;Lock(int value) {this.value = value;}public int getValue() {return value;} }

如果常量值是 0、1、2 順序的,可以不設置 int,直接:

public static enum Lock {ADD_LOCK, UNLOCK; }

lock.ordinal() 即可返回順序的 int。

Ubuntu sudo 不用每次都輸入密碼的解決辦法

雖然 sudo -i 可以避免輸入密碼,但 sftp 不行啊,怎么辦!?

網上說的辦法都不行,直接修改/etc/sudoers文件的最后一行:

%sudo ALL=(ALL:ALL) ALL 修改為 %sudo ALL=(ALL:ALL) NOPASSWD:ALL

我還改崩了,無法使用 sudo,解決辦法參見《Ubuntu改壞sudoers后無法使用sudo的解決辦法》

實際上 如果你對那文件有用戶權限,是不用輸入密碼的,使用 chown -R 用戶名 修改就行。

前端:左菜單,右主區域,左絕對值,右自動填滿的布局

用 Flex 布局,如下:

.container {display: flex;height: 100%;.left {height: 100%;flex: 0 0 300px;border-right: 1px solid lightgray;}.right {height: 100%;flex: 1;/*div占據所有剩余寬度 */} }

JS 快速壓縮 CSS 代碼

很簡單的:

/* 壓縮 css 并保存 */ function compress(code) { code = code.replace(/\n/ig, ''); // 去掉換行 code = code.replace(/(\s){2,}/ig, '$1'); // 多空間(兩個以上) 變 一個空格 code = code.replace(/\t/ig, ''); // 去掉tab code = code.replace(/\n\}/ig, '\}'); // 換行+} 變 不換行 code = code.replace(/\n\{\s*/ig, '\{'); // {+換行 變 不換行 code = code.replace(/(\S)\s*\}/ig, '$1\}'); // 去掉 內容 與 } 之間的空格 code = code.replace(/(\S)\s*\{/ig, '$1\{'); // 去掉 內容 與 { 之間的空格 code = code.replace(/\{\s*(\S)/ig, '\{$1'); // 去掉 { 與 內容之間空格 return code; }

MySQL 調整時區

SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP);

如果是中國標準時間, 會輸出08:00

修改時區

set global time_zone = '+8:00'; ##修改mysql全局時區為北京時間,即我們所在的東8區 set time_zone = '+8:00'; ##修改當前會話時區 flush privileges; #立即生效

IDEA 社區版新建 SpringBoot 項目

社區版下,Spring BootHelper 居然收費。怎么破?利用官方的 Spring Initializr 生成項目,導入即可。

Mybatis insert 的入參為map時,insert 語句中獲取key和value的寫法

https://blog.csdn.net/qq_40580023/article/details/84992429
MyBatis更新數據(輸入參數類型為Map)
https://blog.csdn.net/Dr_Guo/article/details/79057153

Servlet 3 原生文件上傳

package com.ajaxjs.image;import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Collection;import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part;import com.ajaxjs.util.io.StreamHelper;/*** Servlet implementation class Api*/ @WebServlet("/img_api/*") @MultipartConfig public class Api extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse* response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {new Target(request);String filename = "c:\\temp\\11.jpg";try (InputStream bin = new BufferedInputStream(new FileInputStream(filename));) {StreamHelper.write(bin, response.getOutputStream(), true);}}String savePath = "c:\\temp\\";/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse* response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {Collection<Part> parts = request.getParts();// 獲取上傳的文件集合if (parts.size() == 1) {// 上傳單個文件// Servlet3.0 將 multipart/form-data 的 POST 請求封裝成 Part,通過 Part 對上傳的文件進行操作。// Part part = parts[0];//從上傳的文件集合中獲取 Part 對象Part part = request.getPart("file");// 通過表單 file 控件(<input type="file" name="file">)的名字直接獲取 Part 對象uplaod(part, savePath);} else {for (Part part : parts)// 一次性上傳多個文件uplaod(part, savePath);}// response.getWriter().append("Served at: ").append(request.getContextPath());try (PrintWriter out = response.getWriter();) {out.println("上傳成功");}}public static void uplaod(Part part, String savePath) {String header = part.getHeader("content-disposition");// 獲取請求頭,請求頭的格式:form-data; name="file"; filename="snmp4j--api.zip"try {part.write(savePath + File.separator + getFileName(header));// 把文件寫到指定路徑} catch (IOException e) {e.printStackTrace();}}/*** Servlet3 沒有提供直接獲取文件名的方法,需要從請求頭中解析出來 根據請求頭解析出文件名* 請求頭的格式:火狐和google瀏覽器下:form-data; name="file"; filename="snmp4j--api.zip"* IE瀏覽器下:form-data; name="file"; filename="E:\snmp4j--api.zip"* * @param header 請求頭* @return 文件名*/public static String getFileName(String header) {/** String[] tempArr1 = header.split(";");代碼執行完之后,在不同的瀏覽器下,tempArr1數組里面的內容稍有區別* 火狐或者google瀏覽器下:tempArr1={form-data,name="file",filename="snmp4j--api.zip"}* IE瀏覽器下:tempArr1={form-data,name="file",filename="E:\snmp4j--api.zip"}*/String[] tempArr1 = header.split(";");/** 火狐或者google瀏覽器下:tempArr2={filename,"snmp4j--api.zip"}* IE瀏覽器下:tempArr2={filename,"E:\snmp4j--api.zip"}*/String[] tempArr2 = tempArr1[2].split("=");// 獲取文件名,兼容各種瀏覽器的寫法String fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\\") + 1).replaceAll("\"", "");return fileName;}}

這樣理解 java 中的 volatile 更簡單些

程序運行時,有2大塊內存,主內存和本地內存,當某線程讀或寫一個變量時,先操作本地內存,再選擇合適的時機同步到主內存中。

并發三個重要的概念:原子性,可見性,有序性。

關于原子性:

1,synchronized{}修飾的代碼塊,可保證原子性

2,對于volatile int i = 0;

i = 2;是原子操作

i++;不是原子操作,因為要先讀取i的當前值,再進行自增,再進行賦值操作

i = i;不是原子操作,因為要先讀取i的當前值,再進行賦值操作

int j = i;不是原子操作,因為要先讀取i的當前值,再進行賦值操作

  • volatile 只具備可見性和有序性。
  • volatile 可見性,當線程給該變量賦值時,該新值會先修改到本地內存,再直接同步到主內存。
  • volatile 有序性,當純種讀取該變量值時,必須先從主內存讀取最新的值,再同步到本地內存中,再從本地內存中讀取最新值。

Linux 啟動 JAR 包 Shell 腳本

實用呀,可以 kill 掉已有進程然后啟動

port=8083 pid=$(netstat -nlp | grep :$port | awk '{print $7}' | awk -F"/" '{ print $1 }') kill -9 ${pid} echo "killed ${pid}"nohup java -jar auth.jar > /dev/null & tail -f /data/logs/uam/auth/log_debug.log

最后一行 tail -f /data/logs/uam/auth/log_debug.log 觀察日志的可以不執行或者修改 文檔地址。

如何測試高并發的線程安全

結合 CountDownLatch類和Semaphore類測試(出處),例子如下。

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore;/*** @author binghe* @version 1.0.0* @description 測試SimpleDateFormat的線程不安全問題*/ public class SimpleDateFormatTest01 {//執行總次數private static final int EXECUTE_COUNT = 1000;//同時運行的線程數量private static final int THREAD_COUNT = 20;//SimpleDateFormat對象private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");public static void main(String[] args) throws InterruptedException {final Semaphore semaphore = new Semaphore(THREAD_COUNT);final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT);ExecutorService executorService = Executors.newCachedThreadPool();for (int i = 0; i < EXECUTE_COUNT; i++){executorService.execute(() -> {try {semaphore.acquire();try {simpleDateFormat.parse("2020-01-01");} catch (ParseException e) {System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗");e.printStackTrace();System.exit(1);}catch (NumberFormatException e){System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗");e.printStackTrace();System.exit(1);}semaphore.release();} catch (InterruptedException e) {System.out.println("信號量發生錯誤");e.printStackTrace();System.exit(1);}countDownLatch.countDown();});}countDownLatch.await();executorService.shutdown();System.out.println("所有線程格式化日期成功");} }

maven中 Failed to read schema document 錯誤

https://blog.csdn.net/qq_39741730/article/details/104663761

自動 kill 進程再啟動,并輸出日志文件

# 獲取進程名 process_name=new-fleet-market-business-1.0-SNAPSHOT.jar# 查找進程 ID pid=$(jps -l | grep $process_name | awk '{print $1}')# 打印進程 ID echo "進程 ID 為:$pid"# 判斷進程 ID 是否為空 if [ -n "$pid" ]; then# 終止進程kill -9 $pidecho "停止進程 $pid" elseecho "沒有找到進程 $process_name" fiecho "啟動程序" nohup java -Xms512m -Xmx512m -jar ./$process_name >message.log 2>&1 &

總結

以上是生活随笔為你收集整理的每天一剂开发良药的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。