title: 搬磚筆記(三)
date: 2020-04-04 01:20:05
updated: 2020-04-04 01:20:05
categories:
- 搬磚筆記
- java
tags:
- java
- jquery
- mybatis
- spring
- mysql
搬磚筆記(三)
作爲碼農平時蒐集一些小知識點個人認爲是個不錯的習慣,書上說
好記性不如爛筆頭
我想即使是以前忽略或者新get的很簡單的東西,自己動手記下來不管如何印象也會更深刻。
1、Servlet是服務器創建的,因此,不屬於IOC管理,所以不能用自動裝配,在Controller的對應方法用參數綁定;如果你只用@Autowire 或者 @Resource 註解,依賴都是在應用啓動時注入的,當你應用啓動的時候請求還沒來呢,哪兒來的 Request和Response對象啊。
所以當你需要Request 和Response對象時,需要將其放到controller的方法的參數中,這樣每次請求時,Spring MVC框架就會自動將HttpServeletRequest 或 HttpServeletResponse對象注入。
PS:Request對象表示一次請求,裏面包含了本次請求的所有信息,包括Http Header和 Body,
Response對象表示對請求的響應,可以設置響應的header和body
2、$(this).data();注意沒有參數,把當前jQuery對象的所有data屬性取出來,返回結果爲一個對象
例如:
var WarehouseDetail = [];
$trs.each(function(){
var $this = $(this);
var product = $this.data();
product.quantity = $this.find('.quantity').val();
product.remark = $this.find('.remark').text();
WarehouseDetail.push(product);
})
3、貨物處置訂單列表分頁出了問題,需要加個子查詢語句
select dol.*, det.order_detail_id, det.product_id, det.product_name, det.sku_code, det.quantity, det.unit_price, det.product_unit, det.order_price, det.class_id, pe.batch_entrust_name
from disposal_order_list dol
left join disposal_order_detail det using ( order_id )
left join purchase_entrust pe on pe.purchase_entrust_id=dol.purchase_entrust_id
where dol.order_id in(
select order_id
from disposal_order_list tmp
limit #{start},#{pageSize}) order by order_date desc
注意:dol.*是同事挖的坑,子查詢是我加的
然後mysql執行報錯This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME subquery’ 意思是這個版本的 MySQL不支持使用 LIMIT 子句的 IN/ALL/ANY/SOME子查詢,按照意思只需要在外面加一層就可以成功執行了
修改後代碼如下:
where dol.order_id in(select t.order_id from (
select order_id from disposal_order_list tmp
limit #{start},#{pageSize})t)
4、IE和Safari瀏覽器不支持’-‘格式的日期字符串,需要將其替換成’/’
例如:2019/11/13 21:53:05
5、IDEA Maven拉取jar包時一直報"Unable to import maven project: See logs for details"錯誤
java.lang.RuntimeException: com.google.inject.CreationException: Unable to create injector, see the following errors:
No implementation for org.apache.maven.model.path.PathTranslator was bound.
while locating org.apache.maven.model.path.PathTranslator
for field at org.apache.maven.model.interpolation.AbstractStringBasedModelInterpolator.pathTranslator(Unknown Source)
at org.codehaus.plexus.DefaultPlexusContainer$1.configure(DefaultPlexusContainer.java:350)
經過檢查以及百度發現我用的MAVEN版本比公司用的高,於是換成和公司一樣的成功解決問題
6、換項目後,jrebel熱部署不起作用了,IDEA報錯信息如下:
11:43 Invalid rebel.xml: Invalid ‘dir’ defined in class path of rebel.xml
(jar:file:/E:/supSCE_jskj/itonghui_web_cloud/target/MobileSchool-chat/WEB-INF/lib/itonghui-biz-marketing-rebate-0.0.2.ITHJS-SNAPSHOT.jar!/rebel.xml):
Directory ‘E:/supSCE_jskj/itonghui_web_cloud/src/main/webapp/static/wechat/bin’ does not exist
可能maven項目Jrebel默認路徑生成錯誤
修改rebel.xml中的標籤下的路徑爲實際classes存放路徑即可,如圖所示:
參考鏈接
7、查出數據庫表的所有字段並用逗號分隔
select group_concat(column_name)
from information_schema.columns
where table_schema ='數據庫名' and table_name = '表名'
8、js delete 刪除屬性
//delete 只適用於刪除對象屬性
var a = {b:1}
delete a.b;
9、chrome調試模式控制檯capture可以捕獲整屏網頁截圖(手機模式)
10、js遍歷map的一種方法
for (var key in map){
console.log(key,map[key]);
}
11、修改Mapper.xml不用重啓項目的方法
在maven中添加依賴
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>
修改applicationContext-mybatis.xml文件,添加下面的配置
<!-- mappper.xml自動熱部署 -->
<bean class="com.baomidou.mybatisplus.spring.MybatisMapperRefresh">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
<constructor-arg name="mapperLocations" value="classpath*:com/itonghui/biz/**/dao/mapper/*.xml"/>
<constructor-arg name="delaySeconds" value="5"/>
<constructor-arg name="sleepSeconds" value="10"/>
<constructor-arg name="enabled" value="true"/>
</bean>
其中delaySeconds是延遲加載時間,sleepSeconds是刷新時間間隔,enabled開啓熱加載(默認是false),mapperLocations的值與sqlSessionFactory配置的路徑一致
12、mysql排序字段爲空的排在最後面
如果是降序,爲空的數據會自動排到後面,如果是升序,需要加上is null,代碼如下:
select * from user u order by u.user_id is null, u.user_id
13、既驗證手機號碼也能驗證固定電話的正則表達式
//固定電話規則:前3(4)位爲0開頭,後面爲7或8位
^((0\d{2,3}-\d{7,8})|(1[357894]\d{9}))$
14、mysql設計數據庫字段時應當給個默認值,不要null(所有使用NULL值的情況,都可以通過一個有意義的值的表示,這樣有利於代碼的可讀性和可維護性,並能從約束上增強業務數據的規範性)
比如receive_num + #{receivedNum},如果receive_num爲null,運算結果永遠爲null,需要改爲ifnull(received_num,0) + #{item.receivedNum};
比如NOT IN、!= 等負向條件查詢在有 NULL 值的情況下返回永遠爲空結果,查詢容易出錯
15、mybatis-plus的or和and連用
例如:
java代碼:
EntityWrapper<AgreementManagement> entityWrapper = new EntityWrapper<>();
entityWrapper.eq("first_cust_id",agreementManagementDTO.getAddCustId()).or().eq("second_cust_id",agreementManagementDTO.getAddCustId()).or().eq("third_cust_id",agreementManagementDTO.getAddCustId());
ToolUtil.isNotEmpty(agreementManagementDTO.getEndDate(), ()->{//addNew()是爲sql語句加上()
entityWrapper.andNew().le("add_time", agreementManagementDTO.getEndDate());
});
輸出sql:
WHERE (first_cust_id = 104248 OR second_cust_id = 104248 OR third_cust_id = 104248) AND (add_time >= '2020-02-27 00:00:00.0') ORDER BY add_time DESC
16、獲取web項目的絕對路徑
String basePath = request.getServletContext().getRealPath("/");
返回值示例:E:\supSCE_jskj\itonghui_web_cloud\target\MobileSchool-chat
17、jquery的屬性選擇器
語法[attribute=value]
例如:
$("[id='5']")//篩選屬性id值爲5的元素
$("li[data-product-id='322']")//篩選li中屬性data-product-id值爲322的元素
$('.productGroup li[productId='322']')//篩選productGroup下的li中,屬性productId值爲322的元素
18、滾動條默認在最底部顯示(聊天窗口用到)
代碼如下:
("#contentSpan").scrollTop($("#contentSpan")[0].scrollHeight);
由於這個方法需要在頁面元素加載完畢才執行,所以可以設置定時執行,如下:
setTimeout(function () {
$("#contentSpan").scrollTop($("#contentSpan")[0].scrollHeight);
},200)
19、java集合排序的幾種方式
- 對於集合比較使用Collections.sort()
- 對於集合中的對象比較,需要指定比較邏輯,指定比較邏輯需要實現 Comparable接口並重寫compareTo方法自定義邏輯
- 匿名內部類方式。對於需要臨時改變比較規則,需要使用Collections.sort(List,Comparator),採用回調方式重寫