Java实际场景应用案例
1. 把一个类对象赋给另一个所有属性相同的类对象
工具类:
BeanUtil.copyProperties(period,newPeriod);
谨记:BeanUtils.copyProperties只对bean属性进行复制,这里的复制属于浅复制
把javabean复制到另一个javabean 使用BeanUtils.copyProperties(a,b) 复制。该方法对于两种不同的jar包有两种不同的意义 ,a,b通常是两个结构相似的javabean,注意:a,b里的定义类型名称必须一致才能复制。引用的是org.springframework.beans,则是把a 的内容复制到b
if (period.getEndDate() < curDate) {
FinancePeriodIncomePeriod newPeriod = financePeriodIncomePeriodService.getFinancePeriodIncomePeriodByDate(curDate, period.getCorpId());
if(null==newPeriod) {
BeanUtil.copyProperties(period,newPeriod);
newPeriod.setId(null);
newPeriod.setCreateTime(null);
newPeriod.setStartDate(CalendarUtil.getDateWithInterval(period.getEndDate(), 1));
newPeriod.setEndDate(CalendarUtil.getDateWithInterval(period.getEndDate(), 12));
newPeriod.setType(FinanceInventoryContants.INVENTORY_ROYALTY_PERIOD_TYPE_CURRENT);// 当前区间
newPeriod.setId(iIdService.getId()); // need to check!!!
financePeriodIncomePeriodService.save(newPeriod);
}
}
2. 获取时间LocalDateTime类
1. 获取当前时间:LocalDateTime.now()
2. 如何将Date类的对象转化LocalDateTime类的对象
private LocalDateTime dateToLocalDateTime(Date date){
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
return localDateTime;
}
3. Mybatis-Plus 插入数据函数—save(entity)
曾以为还要写insert的sql语句,save函数可实现插入功能。
4. Sql 查询2个表,只返回一个表的所有属性
- 可读性高 :一个个属性列举出来
- 表的别名.*
案例如下:
<select id="getAllCorpYearPoolList" resultType="com.kinglex.executor.entity.FinanceCorpYearPool">
select corpYearPool.* from finance_corp_pool pool,finance_corp_year_pool corpYearPool
where corpYearPool.isDel= 0
and corpYearPool.company=#{arg2}
and pool.isDel=0
and pool.company=#{arg2}
and pool.id = corpYearPool.poolId
and corpYearPool.state = 1
and pool.corpId=#{arg0}
and corpYearPool.periodId=#{arg1}
order by corpYearPool.createTime
</select>
5. 关于Sql中IF如何使用?(三种实现方法if)
1 针对单个表可以用wrapper来实现,if判断属性是否为空时或者不为空时,各写一段wrapper。
2 如下案例
@Override
public List<FinanceTeamYearPool> getTeamYearPoolList(Long corpId, Long periodId, Integer isFixed, Long company) {
Map<String,Object> params = new HashMap<String,Object>();
StringBuffer where = new StringBuffer(" where teamYearPool.isDel="+UserConstant.DELETED_STATE_NO+" and teamYearPool.company=:company");
where.append(" and pool.isDel="+UserConstant.DELETED_STATE_NO+" and pool.company=:company");
where.append(" and pool.id = teamYearPool.poolId");
where.append(" and teamYearPool.state = 1");
where.append(" and pool.corpId=:corpId and teamYearPool.periodId=:periodId");
// where.append(" and pool.corpId="+corpId+" and teamYearPool.periodId=:periodId");
params.put("corpId", corpId);
params.put("periodId", periodId);
params.put("company", company);
if(null!=isFixed) {
where.append(" and pool.isFixed = :isFixed");
params.put("isFixed", isFixed);
}
String hql = "select teamYearPool from FinanceTeamPool pool,FinanceTeamYearPool teamYearPool "+where.toString()+" order by teamYearPool.createTime ";
return super.getListData(hql, params);
}
<select id="getTeamYearPoolList" resultType="com.kinglex.executor.entity.FinanceTeamYearPool">
select teamYearPool.* from finance_team_pool pool,finance_team_year_pool teamYearPool
where teamYearPool.isDel=0
and teamYearPool.company= #{arg3}
and pool.isDel= 0
and pool.company = #{arg3}
and pool.id = teamYearPool.poolId
and teamYearPool.state = 1
and pool.corpId=#{arg0}
and teamYearPool.periodId=#{arg1}
and IF (#{arg2} is null, 1 = 1, pool.isFixed = #{arg2})
</select>
3 面对这种情况,也可以先用String类 sql拼接好字符串,然后在xml中 ¥{sql} , 就可以实现同样的功能。
6. 日志类
6.1 日志级别
每个Logger都被定义了一个日志级别(log level),用来控制日志信息的输出。日志级别从高到低分为:
A:off 最高等级,用于关闭所有日志记录。
B:fatal 指出每个严重的错误事件将会导致应用程序的退出。
C:error 指出虽然发生错误事件,但仍然不影响系统的继续运行。
D:warm 表明会出现潜在的错误情形。
E:info 一般和在粗粒度级别上,强调应用程序的运行全程。
F:debug 一般用于细粒度级别上,对调试应用程序非常有帮助。
G:all 最低等级,用于打开所有日志记录。
上面这些级别是定义在org.apache.log4j.Level类中。Log4j只建议使用4个级别,优先级从高到低分别是 error,warn,info和debug。通过使用日志级别,可以控制应用程序中相应级别日志信息的输出。例如,如果使用b了info级别,则应用程 序中所有低于info级别的日志信息(如debug)将不会被打印出来
6.2 实例
// log4j 日志记录
public final Log log = LogFactory.getLog(getClass());
public synchronized void save(SearchConflictCasesource conflictCasesource) {
if(Boolean.valueOf(isSyncEs)==false) {
return;
}
String jsonString = "";
try {
JSONObject.fromObject(EntityUtils.toString(response.getEntity()));
if(result.has("error") || !result.has("result")) {
log.error("ES返回为:"+result.toString());
throw new RuntimeException();
} else {
// 变更为已同步至ES
// updateCasesourceIsSyncEs(conflictCasesource.getId(), 1); need to add!!!
}
} catch (Exception e) {
e.printStackTrace();
log.error("ES案源当事人保存失败["+jsonString+"]:",e);
throw new RuntimeException("ES案源当事人插入异常");
}
}
7. 分页查询
- 定时器
int pageSize = 500;
List<EmpInfo> empInfoList = null;
int page = 1;
do {
empInfoList = empInfoService.getEmpListAllWechatLinkByNotification(page, pageSize);
if (empInfoList != null) {
for (int i = 0; i < empInfoList.size(); i++) {
EmpInfo empInfo = (EmpInfo) empInfoList.get(i);
}
}
page++;
} while (!empInfoList.isEmpty());
- service层
@Override
public List<EmpInfo> getEmpListAllWechatLinkByNotification(Integer page, Integer pageSize) {
Page<EmpInfo> page1 = new Page<>();
page1.setCurrent(page);
page1.setSize(pageSize);
IPage<EmpInfo> empListAllWechatLinkByNotification = empInfoMapper.getEmpListAllWechatLinkByNotification(page1);
List<EmpInfo> empInfoList = empListAllWechatLinkByNotification.getRecords();
return empInfoList;
}
3.Mapper
8. 复杂查询—多表,数据库字段与非数据库字段联合查询案例
account.empName中empName是非数据库字段
<select id="getListAllInventoryLawyerAccountByCorpIdPeriod"
resultType="com.kinglex.executor.entity.InventoryLawyerAccount">
-- select account.*
-- from finance_set_lawyer lawyer,
-- inventory_lawyer_account account
-- where lawyer.isDel = 0
-- and IF(#{arg0} is null, 1 = 2, lawyer.cropId = #{arg0})
-- and IF(#{arg2} is null, 1 = 1, account.empName like "%"#{arg2}"%")
-- and lawyer.periodId = #{arg1}
-- and lawyer.isAllocation = 1
-- and account.isDel = 0
-- and lawyer.empId = account.empId
-- order by account.createTime desc
select account.*
from finance_set_lawyer lawyer,
inventory_lawyer_account account,
emp_info emp
where lawyer.isDel = 0
and emp.isDel = 0
and emp.empid = account.empId
and IF(#{arg0} is null, 1 = 2, lawyer.cropId = #{arg0})
and IF(#{arg2} is null, 1 = 1, emp.empName like "%"#{arg2}"%")
and lawyer.periodId = #{arg1}
and lawyer.isAllocation = 1
and account.isDel = 0
and lawyer.empId = account.empId
order by account.createTime desc
</select>