与海笔记
获取当前登录用户
//获取当前登录的用户
LoginUser loginUser = SecurityUtils.getLoginUser();
String username = loginUser.getUsername();
SysUser user = loginUser.getUser();
身份证提取出生日期
// 出生日期
String birthday = "";
Long age = new Long(0);
String sfzh = compUser.getIdCard();
if (StringUtils.isNotEmpty(sfzh)) {
// 身份证号不为空
if (sfzh.length() == 15) {
birthday = "19" + sfzh.substring(6, 8) + "-" + sfzh.substring(8, 10) + "-" + sfzh.substring(10, 12);
} else if (sfzh.length() == 18) {
birthday = sfzh.substring(6, 10) + "-" + sfzh.substring(10, 12) + "-" + sfzh.substring(12, 14);
}
System.out.println("268:userId=" + userId + "\n 生日:" + birthday + "\n 身份证:" + sfzh);
if (StringUtils.isNotEmpty(birthday)) {
System.out.println("270:userId=" + userId + "\n 生日:" + birthday + "\n 身份证:" + sfzh);
age = (System.currentTimeMillis() - simpleDateFormat.parse(birthday).getTime()) / 31536000000L;
}
compAtteDay1.setAge(age);
System.out.println("286:" + sfzh + "\n userId:" + userId);
SQL语句
# 1.查看表结构
show create table ;
字符串操作
//字符串是否完全相同
str1.equals(str2);//返回值Boolean
JSON操作
//json串转集合
//方法1:
Map<String ,Object> resultJSON=(Map<String,Object>) JSON.parseObject(resultJSON);
//方法2:
import com.alibaba.fastjson.JSON;
//用BodyJson 封装bodyJSON格式
BodyJson ddJson = JSON.parseObject(BodyJson , DdJson.class);
//集合转json串
String JSONString= JSON.toJSONString(JSONmap);
SpringBoot整合定时任务
启动类
@SpringBootApplication//(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@MapperScan("com.example.demo.mapper")
public class MomddingApplication {
public static void main(String[] args) {
SpringApplication.run(MomddingApplication.class, args);
//支持秒级别定时任务 √
CronUtil.setMatchSecond(true);
//通过配置文件启动定时任务 √
CronUtil.start();
}
}
ApplicationContextUtil
解决springbean池的null问题 无需更改,照抄就行
package com.example.demo.utils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) {
ApplicationContextUtil.applicationContext = applicationContext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
}
QuartzCronUtil
创建要执行的定时任务
package com.example.demo.utils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.example.demo.pojo.CompConfig;
import com.example.demo.service.Impl.CompConfigServiceImpl;
/**
* 定时任务
* @author thdaoer
*
*/
@Component
public class QuartzCronUtil {
/**
* 通过注解@Autowired获取bean里面的compConfigService对象会报null异常
* 因为定时器的执行优先于spring创建bean
* 所以用ApplicationContextUtil工具栏获取
*/
// @Autowired
// private CompConfigService compConfigService;
CompConfigServiceImpl compConfigServiceImpl=(CompConfigServiceImpl)ApplicationContextUtil.getBean("compConfigServiceImpl");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 获取魔点AccessToken 2小时有效
*/
public void getOrgAccessToken() {
System.out.println("getOrgAccessToken定时任务");
String accessToken = new String();
HttpUrlConnectionUtils httpUrlConnectionUtils = new HttpUrlConnectionUtils();
String requestUrl = "https://toapi.moredian.com/org/getOrgAccessToken";
Map<String, String> params = new HashMap<>();
params.put("orgId", "1701977497052119040");
params.put("orgAuthKey", "ogFEq9THjo0qYgBoah0nHM1NGVStu4YJ");
String result = httpUrlConnectionUtils.get(requestUrl, params);
Map<String, Object> resultJSON = (Map<String, Object>) JSON.parseObject(result);
if (resultJSON.get("result").equals("0")) {
@SuppressWarnings("unchecked")
Map<String, String> value = (Map<String, String>) resultJSON.get("data");
accessToken = value.get("accessToken");
CompConfig compConfig1 = new CompConfig();
compConfig1.setVal(accessToken);
compConfig1.setConfigId(2);
compConfig1.setUpdateTime(simpleDateFormat.format(new Date()));
compConfigServiceImpl.updataAccessCompConfig(compConfig1);
}
System.out.println("getOrgAccessToken():" + accessToken);
}
public void getTest() {
System.out.println("getTest每2分钟执行一次");
}
}
配置文件
对于Maven项目,首先在src/main/resources/config下放入cron.setting文件(默认是这个路径的这个文件),然后在文件中放入定时规则,规则如下
设置:1,指定定时任务的java文件和执行时间
[com.example.demo.utils]
QuartzCronUtil.getOrgAccessToken=*/30 * * * * *
#QuartzCronUtil.getTest=*/20 * * * * *
时效检测
/**
* 时效检测
* true有效
* false无效
*
* @param compConfig
* @return
*/
private boolean Effective(CompConfig compConfig) {
String dateStr = compConfig.getUpdateTime();//创建时间
Date date = DateUtil.parse(dateStr);
String expiresStr = compConfig.getExpires();//有效时长:秒
int expires = Integer.parseInt(expiresStr);
if (DateUtil.offsetSecond(date, expires).getTime() < new Date().getTime())//失效时段
return false;
return true;
}
数据库操作
查询重复列
select 列名,count(列名)
from 表名
group by 列名
having count(列名)>1
order by count(列名) desc
查询不重复的列数据
select distinct 列名 from 表名
删除重复数据
delete from 表名01 where 列名01 IN (
select 列名01 from
(select 列名01 from 表名01 group by 列名01 having count(列名01)>1) as 自定义表名
);
例: 删除userId重复数据
delete from sys_users where user_id IN (
select user_id from
(select user_id from sys_users group by user_id having count(user_id)>1) as p
);
插入多条数据
INSERT INTO 表名 (列名) select语句 ;
Excel操作
方案1
缺点: Excel表格的实体类的列名要保持一致,无法解析中文
前段代码
样式下载
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>导入excel</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<script src="../excel/xlsx.full.min.js"></script>
<script src="../excel/jquery.js"></script>
<script src="../excel/xlsx.core.min.js"></script>
</head>
<body>
<div>
<input type="file" onchange="importf(this)"/>
</div>
<script>
var wb;//读取
var rABS = false;
//开始导入
function importf(obj) {
if (!obj.files) {
return;
}
var f = obj.files[0];
var reader = new FileReader();
reader.onload = function (e) {
var data = e.target.result;
if (rABS) {
wb = XLSX.read(btoa(fixdata(data)), {//手动转化
type: 'base64'
});
} else {
wb = XLSX.read(data, {
type: 'binary'
});
}
/**
* wb.SheetNames[0]是获取Sheets中第一个Sheet的名字
* wb.Sheets[Sheet名]获取第一个Sheet的数据
*/
var excelJson = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
$.ajax({
url: "/LoginController/InputFile",
type: "POST",
dataType: "json",
contentType: "application/json",
data: JSON.stringify(excelJson),
success: function (data) {
window.location.reload();//页面刷新
}
});
};
if (rABS) {
reader.readAsArrayBuffer(f);
} else {
reader.readAsBinaryString(f);
}
}
//文件流转BinaryString
function fixdata(data) {
var o = "",
l = 0,
w = 10240;
for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w +
w)));
o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
return o;
}
</script>
</body>
</html>
后端代码
实体类
Excel展示
package com.example.demo.pojo;
import com.example.demo.pojo.annotation.Excel;
public class userExcel {
//工号
//@Excel(name = "新工号")
private int userId;
//姓名
// @Excel(name = "姓名")
private String userName;
//职位
// @Excel(name = "是否为管理人员")
private String position;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
@Override
public String toString() {
return "userExcel{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", position='" + position + '\'' +
'}';
}
}
控制层代码
//批量导入 单选
@PostMapping("/oneinput")
@ResponseBody
public void oneinput(@RequestBody List<userExcel> xuanZePojos) {
System.out.println("11111");
}
断点展示