记录日常开发中的小问题
------------懒得开新篇, 一些小问题全都记录在此篇, 会不时更新
-
案例一:
SpringDataJPA中使用@Query的自定义sql语句的时候 没有写上nativeQuery = true 启动会报错 ;
在@Query中加上 “nativeQuery = true” 成功启动
分析:
Spring Data Jpa 默认实现是hibernate,我们都知道hibernate使用HQL查询(Hibernate是JPA的实现之一),而不推荐使用sql查询,因为这样子就跟具体数据库耦合了,违背了初衷,但是没有union语句,所以也只能用原生了
附上一些@Query书写sql语句小例子:
//传入的是对象的时候 @Query(nativeQuery = true,value ="update tb_student s set a.name = :#{student.name}, a.age = :#{student.age}") int updateStudent(Student student); @Query(nativeQuery = true,value = "delete from tb_student s where s.id = :id") int deleteStudent(Long id); @Query(nativeQuery = true,value = "delete from tb_student s where s.id = ?1") int deleteStudentById(Long id); /**模糊查询的用法*/ @Query(nativeQuery = true,value ="select id,age,name from tb_student s where name like %:name% ") List<Student> findLikeStudents(@Param("name") String name); @Query(nativeQuery = true,value = "select * from tb_student s where s.id = ?1") List<Student> findStudentById(@Param("id") Long id);
-
案例二:
从Git上克隆项目太慢, 出现以下:
git config --global http.postBuffer 524288000
git config --global http.lowSpeedLimit 0 git config --global http.lowSpeedTime 999999
-
案例三:
AJAX发送PUT请求引发的问题:
原因:
分析上下文:
Tomcat:
- 会将请求体中的数据,封装成一个map
- request.getParameter(“xxx”)就会从这个map中取值
- SpringMVC封装POJO对象的时候,会先 request.getParameter(“xxx”) 在set进POJO中每个属性中
解决方案:在web.xml配置文件中加入这段过滤器配置即可
<!--使用rest风格的URi,将页面普通的post请求转换为指定的delete或者put请求--> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>HttpPutFormContentFilter</filter-name> <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class> </filter> <filter-mapping> <filter-name>HttpPutFormContentFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
-
案例四:
bootstrap出现$(…).modal is not a function
分析:
因为bootstrap4是基于jquery实现的,所以只有将jquery的引用放在bootstrap前才能正常使用。
解决方法:
将jquery的引用放在bootstrap之前
-
案例五:
运行 ‘xxxx’ 出错: 无法打开调试器端口 (127.0.0.1:62700): java.net.BindException “Address already in use: JVM_Bind”
解决:
打开tomcat配置在启动/连接中找到自己选择启动的所选项将原有62700端口号改成一个跨度比较大的端口后即可
今天运行搭建项目的时候发现一个很坑的报错 , 报错信息如下:
Caused by: java.lang.IllegalArgumentException: Result Maps collection already contains value for cbuc.tmall.mapper.OrderItemMapper.BaseResultMap
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:844)
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:816)
at org.apache.ibatis.session.Configuration.addResultMap(Configuration.java:598)
at org.apache.ibatis.builder.MapperBuilderAssistant.addResultMap(MapperBuilderAssistant.java:214)
at org.apache.ibatis.builder.ResultMapResolver.resolve(ResultMapResolver.java:47)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:285)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:252)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:244)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:116)
... 113 more
划重点 :
Result Maps collection already contains value for cbuc.tmall.mapper.OrderItemMapper.BaseResultMap
其实上面的报错信息说的很清楚了 , aleady contains value 这几个直译过来就是了, 但是脑子抽了 一直在mybatis配置文件中找错误, 找了半天愣是没找到, 然后才重会报错上寻找突破点, 然后就发现那错误所在:
我下面那部分棕色的内容和上面完全重了!!
删除后便正常运行 …
回忆场景 :
因为我再次使用逆向工程生成mapper接口和xml文件时,忘了删除原来的xml文件,新生成的与旧的同时出现旧重复了。
解决方案 : 把重复的部分删除,或者删除xml文件重新建立逆向工程
这个时候问题又来了, 什么是逆向工程 , 怎么建立逆向工程呢…
欢迎参考博文: Mybatis Generator 的使用
window.location.href is not a function
在chrome上运行项目的时候出现window.location.href is not a function,项目中的写法是这样的 :window.location.href(‘url’);
结论:
mybatis中通过@indert注解插入数据返回主键值
解决:
添加@Options(useGeneratedKeys=true, keyColumn=“id”)
通过ajax 访问 multipart request
通过表单提交的方式
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8" />
<!-- 指定所上传文件的总大小,单位字节。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="10240000" />
</bean>
问题:通过Ajax提交绕过表单的话会报“ Current request is not a multipart request”错误
解决:
红色部分是重点
-
processData设置为false。因为data值是FormData对象,不需要对数据做处理。
-
标签添加enctype="multipart/form-data"属性。
-
cache设置为false,上传文件不需要缓存。
-
contentType设置为false。因为是由表单构造的FormData对象,且已经声明了属性enctype=“multipart/form-data”,所以这里设置为false。
java中获取项目路径:
1)
String path =request.getSession().getServletContext().getRealPath("");//文件路径
String parentpath = new File(path).getParent();//获取项目的上一级目录
2)
//当前项目下路径 --- E:\Work\example
File file = new File("");
String filePath = file.getCanonicalPath();
System.out.println(filePath);
3)
//当前项目下xml文件夹 --- E:\Work\example\xml
File file1 = new File("");
String filePath1 = file1.getCanonicalPath()+File.separator+"xml\\";
System.out.println(filePath1);
4)
//获取类加载的根路径 --- E:\Work\example\out\production\classes
File file3 = new File(this.getClass().getResource("/").getPath());
System.out.println(file3);
5)
//获取当前类的所在工程路径 --- E:\Work\example\out\production\classes\com\demo
File file4 = new File(this.getClass().getResource("").getPath());
System.out.println(file4);
6)
//获取所有的类路径 包括jar包的路径
System.out.println(System.getProperty("java.class.path"));
今天在Idea上往自己的git仓库上push本地代码,发现报了一个错
Push failed: Could not read from remote repository.
解决:
打开设置,在搜索框输入git,SSH executable 后面的下拉框里选择Native即可
再次push代码,结果成功
除了查询操作,其他都应该通过重定向跳转页面,防止表单重复提交
子页面修改父页面的值
方法:
$("#msgNum",window.parent.document).html(result.data);
关键在于: window.parent.document
阿里轻量服务器上运行Tomcat成功,外部访问却失败
解决:
在终端输入:
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-port=8009/tcp --permanent
缘由:
在CentOS 7中引入了一个更强大的防火墙——Firewall。我们需要在Firewall中开启8080和8009端口(这里的两个端口是tomcat默认的访问tomcat服务器的端口,如果自己改变了端口,要按照自己修改的端口),也就是将8080和8009端口加入到zone(Firewall的新特性,简单讲它的作用就是定义了网络区域网络连接的可信等级)中。
接着在控制台配置防火墙:
这样子外部就能成功访问到tomcat