登录日志在一个系统中是很重要的,它可以记录用户的活跃度,还可以统计用户的在线时间,可以反映出这一个系统的使用情况
1.domain的设计
@Setter
@Getter
public class Iplog extends BaseDomain {
public static final int STATE_SUCCESS = 0;//登录成功
public static final int STATE_FAIL = 1;//登录失败
private String ip;//用户IP
private String username;//用户名
private int state = STATE_SUCCESS;//登录状态
private Date loginTime;//登录时间
public String getStateDisplay() {
//用来页面显示,如果直接显示0或者1,不是很直观
return state == STATE_SUCCESS ? "登录成功" : "登录失败";
}
}
2.mapper,这里设计的只需要有一个插入的功能和分页,按条件查询
@Repository
public interface IplogMapper {
int insert(Iplog record);
/**
* 查询总条数
* @param qo
* @return
*/
int queryForCount(IpLogQueryObject qo);
/**
* 查询结果集
* @param qo
* @return
*/
List<Iplog> query(IpLogQueryObject qo);
}
3.mapper,xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.fym.newp2p.base.mapper.IplogMapper" >
<resultMap id="BaseResultMap" type="com.fym.newp2p.base.domain.Iplog" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="ip" property="ip" jdbcType="VARCHAR" />
<result column="state" property="state" jdbcType="TINYINT" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="loginTime" property="loginTime" jdbcType="TIMESTAMP" />
</resultMap>
<insert id="insert" useGeneratedKeys="true" keyProperty="id" >
insert into iplog (ip, state, username, loginTime)
values (#{ip,jdbcType=VARCHAR}, #{state,jdbcType=TINYINT}, #{username,jdbcType=VARCHAR},#{loginTime,jdbcType=TIMESTAMP})
</insert>
<!--<select id="selectAll" resultMap="BaseResultMap" >
select id, ip, state, username, loginTime
from iplog
</select>-->
<sql id="base_where">
<where>
<if test="beginDate!=null">
AND loginTime >= #{beginDate}
</if>
<if test="endDate!=null">
AND loginTime <= #{endDate}
</if>
<if test="state!=-1">
and state=#{state}
</if>
<if test="username!=null">
and username=#{username}
</if>
</where>
</sql>
<select id="queryForCount" resultType="int">
SELECT count(id)
FROM iplog
<include refid="base_where"/>
</select>
<select id="query" resultMap="BaseResultMap">
select id, ip, state, username, loginTime
FROM iplog
<include refid="base_where"/>
limit #{beginIndex},#{pageSize}
</select>
</mapper>
4.分页和按条件查询
@Getter
@Setter
@ToString
public class PageResult {
private List<?> listData;//分页查询的结果集
private Integer prevPage;//上一页
private Integer nextPage;//下一页
private Integer currentPage;//当前页
private Integer pageSize;//页面大小
private Integer totalCount;//总条数
private Integer beginPage;//首页
private Integer totalPage;//总页数
public static PageResult EMPTY_RESULT = new PageResult(Collections.emptyList(),1,5,0);
public PageResult(List<?> listData, Integer currentPage, Integer pageSize, Integer totalCount) {
this.listData = listData;
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalCount = totalCount;
this.beginPage=1;
this.totalPage=this.totalCount%this.pageSize==0?(this.totalCount/this.pageSize):(this.totalCount/this.pageSize+1);
this.prevPage=this.currentPage==1?1:this.currentPage-1;
this.nextPage=this.currentPage==this.totalPage?this.totalPage:this.currentPage+1;
}
}
Getter@Setter
public class IpLogQueryObject extends QueryObject {
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date beginDate;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date endDate;
private String username;
private int state=-1;
public Date getEndDate(){
return endDate==null?null: DateUtil.getEndDate(this.endDate);
}
public Date getBeginDate(){
return beginDate==null?null: DateUtil.getBeginDate(this.beginDate);
}
}
5.时间处理的工具类
**
* 时间处理的工具类
*/
public class DateUtil {
// 2-10 15:07:33
// 2-10 00:00:00
/**
* 返回当天最早的事件 00:00:00
*
* @param date
* @return
*/
public static Date getBeginDate(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
return c.getTime();
}
/**
* 返回当前最晚的事件 23:59:59
*
* @param date
* @return
*/
public static Date getEndDate(Date date) {
if (date != null) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
return c.getTime();
}
return null;
}
/**
* 获取两个时间间隔秒数
*/
public static long getSecondsBetween(Date d1, Date d2) {
return Math.abs((d1.getTime() - d2.getTime()) / 1000);
}
}
6.service实现
@Service
public class IpLogServiceImpl implements IIpLogService {
@Autowired
private IplogMapper iplogMapper;
@Override
public PageResult query(IpLogQueryObject qo) {
int count = iplogMapper.queryForCount(qo);
if(count<=0){
return PageResult.EMPTY_RESULT;
}
return new PageResult(iplogMapper.query(qo),qo.getCurrentPage(),qo.getPageSize(),count);
}
}
7.controller
@Controller
public class IplogController {
@Autowired
private IIpLogService ipLogService;
@RequestMapping("ipLog")
public String iplog(@ModelAttribute("qo") IpLogQueryObject qo, Model model){
qo.setUsername(UserContext.getCurreentUser().getUsername());
PageResult result = ipLogService.query(qo);
model.addAttribute("result",result);
return "iplog_list";
}
}
8.前台页面(这里使用的freemarker,只截取有用的部分)
<script type="text/javascript">
$(function(){
//给这两个input点击显示日历
$(".beginDate,.endDate").click(function () {
WdatePicker();
});
//点击查询,提交表单
$("#query").click(function () {
//设置当前页为1
$("#currentPage").val(1);
$("#searchForm").submit();
});
//分页
$("#pagination").twbsPagination({
totalPages:${result.totalPage},
visiblePages:5,
startPage:${result.currentPage},
onPageClick:function (e,page) {
$("#currentPage").val(page);
$("#searchForm").submit();
}
})
});
<div class="col-sm-9">
<form action="/ipLog" name="searchForm" id="searchForm" class="form-inline" method="post">
<input type="hidden" id="currentPage" name="currentPage" value="" />
<div class="form-group">
<label>时间范围</label>
<input type="text" class="form-control beginDate" name="beginDate" value='${(qo.beginDate?string("yyyy-MM-dd"))!""}'/>
</div>
<div class="form-group">
<label></label>
<input type="text" class="form-control endDate" name="endDate" value='${(qo.endDate?string("yyyy-MM-dd"))!""}'/>
</div>
<div class="form-group">
<label>状态</label>
<select class="form-control" name="state" id="state">
<option value="-1">全部</option>
<option value="1">登录失败</option>
<option value="0">登录成功</option>
</select>
<script type="text/javascript">
$("#state option[value=${qo.state}]").attr("selected",true);
</script>
</div>
<div class="form-group">
<button type="button" id="query" class="btn btn-success"><i class="icon-search"></i> 查询</button>
</div>
</form>
<div class="panel panel-default" style="margin-top: 20px;">
<div class="panel-heading">
登录日志
</div>
<table class="table table-striped">
<thead>
<tr>
<th>用户</th>
<th>登录时间</th>
<th>登录ip</th>
<th>登录状态</th>
</tr>
</thead>
<tbody>
<#list result.listData as item>
<tr>
<td>${item.username}</td>
<td>${item.loginTime?string("yyyy-MM-dd HH:mm:ss")}</td>
<td>${item.ip}</td>
<td>${item.stateDisplay}</td>
</tr>
</#list>
</tbody>
</table>
<div style="text-align: center;">
<ul id="pagination" class="pagination"></ul>
</div>
8.在登录的时候就需要添加日志信息,登录操作
不管是登录成功或者失败,都是需要记录信息
public Logininfo login(String username, String password,String ip) {
Iplog iplog = new Iplog();
iplog.setLoginTime(new Date());
iplog.setUsername(username);
iplog.setIp(ip);
Logininfo logininfo = logininfoMapper.login(username, MD5.encode(password));
if(logininfo!=null){
iplog.setState(Iplog.STATE_SUCCESS);
}else{
iplog.setState(Iplog.STATE_FAIL);
}
iplogMapper.insert(iplog);
return logininfo;
}
9.效果如下: