重点:戒骄戒躁!!!写任何功能的时候都要先梳理逻辑!!!不然很容易重写,浪费时间!!!
1.xxx cannot be resolved to a type
解决方法:Project ——> Clean——>选择要clean的项目 。
2.Java访问接口
// 请求体
Map<String, String> requestBody = new HashMap<String, String>();
requestBody.put("sentpair", rdssList.toString());
String result = "";
// POST请求
HttpPost requestMethod = new HttpPost(
url地址);
// 添加默认请求头
requestMethod.addHeader("Connection", "keep-alive");
requestMethod.addHeader("Pragma", "no-cache");
requestMethod
.addHeader(
"Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
requestMethod.addHeader("Accept-Encoding", "gzip, deflate");
requestMethod.addHeader("Accept-Language",
"en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7");
// 封装请求体
Iterator<Map.Entry<String, String>> iterator = requestBody.entrySet()
.iterator();
List<BasicNameValuePair> nameValuePairList = new ArrayList<BasicNameValuePair>();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
String name = entry.getKey();
String value = entry.getValue();
BasicNameValuePair nameValuePair = new BasicNameValuePair(name,
value);
nameValuePairList.add(nameValuePair);
}
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
nameValuePairList, "UTF-8");
requestMethod.setEntity(entity);
InputStream is = null;
InputStreamReader isr = null;
BufferedReader reader = null;
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(300000)
.setConnectionRequestTimeout(300000)
.setSocketTimeout(300000).build();
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(config).build();
CloseableHttpResponse response = null;
StringBuffer stringBuffer = new StringBuffer();
try {
try {
response = httpClient.execute(requestMethod);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
is = response.getEntity().getContent();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
isr = new InputStreamReader(is);
reader = new BufferedReader(isr);
String inputLine = StringUtils.EMPTY;
try {
while (null != (inputLine = reader.readLine())) {
stringBuffer.append(inputLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} finally {
IOUtils.closeQuietly(reader);
IOUtils.closeQuietly(httpClient);
IOUtils.closeQuietly(isr);
IOUtils.closeQuietly(response);
}
result = stringBuffer.toString();
resultJson.put("result", result);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
3.前后台分离,后端向前端传文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
public class DownloadYhsc extends HttpServlet {
/**
* @author admin
* @version 2.0
*/
private static final long serialVersionUID = 8529206527876956262L;
final protected static Logger logger = LoggerFactory
.getLogger(Download.class);
@Autowired
private ExportExcelUtil exportExcelUtil;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
@Override
@SuppressWarnings({ "unchecked" })
public void doGet(HttpServletRequest request, HttpServletResponse response) {
Map<String, Object> paramMap = request.getParameterMap();
if (null == paramMap || paramMap.isEmpty()) {
return;
}
String serverPath = new StringBuilder()
.append(ArteryUtil.getRequest().getSession()
.getServletContext().getRealPath(File.separator))
.append("pub\\yhsc").append(File.separator).toString();
String fileName;
try {
fileName = URLEncoder.encode("xxx.docx", "UTF-8");
String header = new StringBuilder().append("attachment;filename=")
.append(fileName).toString();
response.setContentType("application/octet-stream");
response.addHeader("Content-disposition", header);
response.addHeader("Transfer-Encoding", null);
response.addHeader("connection", "close");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
File file = new File(serverPath + "xxx.docx");
FileInputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(file);
out = response.getOutputStream();
IOUtils.copy(in, out);
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
logger.error("文件不存在" + e);
} catch (IOException e) {
e.printStackTrace();
logger.error("获取流异常" + e);
} finally {
IOUtils.closeQuietly(out);
IOUtils.closeQuietly(in);
}
}
/**
* 转换编码
*
* @param fileName
* @return
*/
private String getDecodedStr(String str) {
String decodedStr = null;
try {
if (null == str || str.isEmpty()) {
decodedStr = StringUtils.EMPTY;
} else {
decodedStr = URLDecoder.decode(str, "UTF-8");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
logger.error("不支持的编码" + e);
}
return decodedStr;
}
/**
* Destruction of the servlet. <br>
*/
@Override
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
@Override
public void init() throws ServletException {
// Put your code here
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
4.对map的value进行排序
// 对map的value进行排序
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(
XzcfMap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
// 升序排序
public int compare(Entry<String, String> o1,
Entry<String, String> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
5.在循环里产生相同的UUID问题
解决:查看保存UUID的值是不是全局变量!!!
6.TreeMap倒序
TreeMap<String, String> map = new TreeMap<String, String>(
new Comparator<String>() {
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
});
7.MyBatis批量增删改查
<!-- 批量增加 -->
<insert id="insertUsersBatch" parameterType="java.util.List">
insert into user(userName,password) values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.userName},#{item.password})
</foreach>
</insert>
<!-- 批量删除 -->
<delete id="deleteUsersBatch" parameterType="java.util.List">
delete from user where id in
<foreach collection="list" index="index" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</delete>
<!-- 批量查询 -->
<select id="selectUsersBatch" resultType="User">
select *
from user where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
<!-- 批量更新 -->
<update id="updateUsersBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";" >
update user
<set>
userName = #{item.userName}, password = #{item.password}
</set>
where id = #{item.id}
</foreach>
</update>
8.MyBatis写SQL的大于等于和小于等于
大于等于:<![CDATA[>=]]>
小于等于:<![CDATA[<=]]>
9.日期加一天的方法
Date time = new Date();
Calendar c = Calendar.getInstance();
c.setTime(time);
c.add(Calendar.DAY_OF_MONTH, 1)
10.PostgreSQL传时间类型
不能传String类型,会报错。
报错信息:timestamp without timeZone(时间戳没有时区)
11.jar包冲突解决方法
1.在maven的jar包中找到对应的jar包
2.在pom文件查看Dependency Hierarchy
3.看jar包版本信息是否对应
4.在项目的pom文件中找到依赖
5.右键Exclude Maven Artifact,保存
6.更新Maven依赖(项目右键update dependency)
12.前端开发:点击事件返回页面顶部
<!-- 定义一个名称为top的锚点链接 -->
<a name="top"></a>
<!-- 这里是网页主体内容,此处省略 -->
<!-- 返回页面顶部top锚点的链接 -->
<a href="#top">返回顶部</a>
13.前端开发:弹出框点击弹出框以外的地方,收起弹出框
:close-on-click-modal="true"
14.前端开发:VUE规定V-FOR循环次数
<p v-for="(lab,index) in Options" v-if='index<=1'></p>
15.JSON转换出现错误:net.sf.json.JSONException: Unterminated string at character
出现原因:字符串中存在换行符,导致json转换异常
解决方法:去除字符串中的换行符
代码:
"字符串".replaceAll("[\b\r\n\t]*", "")
16.where和having的区别
WHERE语句在GROUPBY语句之前;SQL会在分组之前计算WHERE语句。
HAVING语句在GROUPBY语句之后;SQL会在分组之后计算HAVING语句。
having是对group by后的结果进行筛选,可以写函数
17.Triple抽象类,实现这个抽象类的类能够存储三个对象
忘了就百度!!!