Java程序猿搬砖笔记(三)

Java程序猿搬砖笔记(三)

作为码农平时搜集一些小知识点个人认为是个不错的习惯,书上说

好记性不如烂笔头

我想即使是以前忽略或者新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、JavaScript delete 删除属性

//delete 只适用于删除对象属性
var a = {b:1}
delete a.b;

9、chrome调试模式控制台capture可以捕获整屏网页截图(手机模式)

10、JavaScript遍历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),采用回调方式重写
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值