常用的一些工具类
Ajax提示消息的封装result
public class Result extends HashMap<String,Object> {
/**
* 成功
* @return
*/
public static Result success() {
Result result = new Result();
result.put(Constant.RESULT_CODE,Constant.RESULT_SUCCESS);
return result;
}
/**
* 失败
* @param message
* @return
*/
public static Result fail(String message) {
// 单例模式
Result result = new Result();
result.put(Constant.RESULT_CODE,Constant.RESULT_FAIL);
result.put(Constant.RESULT_MESSAGE,message);
return result;
}
}
uuid的生成
public class UUIDUtils {
public static void main(String[] args) {
String str=UUID.randomUUID().toString();
str=str.replaceAll("-","");
System.out.println(str);
}
public static String getUUID(){
return UUID.randomUUID().toString().replaceAll("-","");
}
}
md5加密
public class MD5Util {
public static String getMD5(String password) {
try {
// 得到一个信息摘要器
MessageDigest digest = MessageDigest.getInstance("md5");
byte[] result = digest.digest(password.getBytes());
StringBuffer buffer = new StringBuffer();
// 把每一个byte 做一个与运算 0xff;
for (byte b : result) {
// 与运算
int number = b & 0xff;// 加盐
String str = Integer.toHexString(number);
if (str.length() == 1) {
buffer.append("0");
}
buffer.append(str);
}
// 标准的md5加密后的结果
return buffer.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return "";
}
}
}
pegeBean分页
public class PageBean<T> {
private Integer pageNo; //第几页
private Integer pageSize; //每页条数
private List<T> dataList; //数据条
private Integer totalPage; //总共几页
private Integer total; //总的条数
public PageBean(Integer pageNo, Integer pageSize) {
if(pageNo<=0){
this.pageNo = 1;
}else{
this.pageNo = pageNo;
}
if(pageSize<=0){
this.pageSize = 10;
}else{
this.pageSize = pageSize;
}
this.pageNo = pageNo;
this.pageSize = pageSize;
}
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
this.pageNo = pageNo;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public List<T> getDataList() {
return dataList;
}
public void setDataList(List<T> dataList) {
this.dataList = dataList;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.totalPage = (total%pageSize==0)?(total/pageSize):(total/pageSize+1);
this.total = total;
}
/**
* 开始索引
*/
public Integer getStartIndex(){
return (this.pageNo-1)*this.pageSize;
}
@Override
public String toString() {
return "PageBean{" +
"pageno=" + pageNo +
", pagesize=" + pageSize +
", dataList=" + dataList +
", totalPage=" + totalPage +
", total=" + total +
'}';
}
}
这里需要提供模糊查询总数的sql和模糊查询
在控制器里传入起始页和每页大小
分页controller
@RequestMapping("/workbench/activity/queryActivityListForPageByCondition.do")
public @ResponseBody
Object queryActivityListForPageByCondition(@RequestParam(value = "pageNo", defaultValue = "1", required = true) Integer pageNo,
@RequestParam(value = "pageSize", defaultValue = "5", required = true) Integer pageSize,
@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "ownerName", required = false) String ownerName,
@RequestParam(value = "startDate", required = false) String startDate,
@RequestParam(value = "endDate", required = false) String endDate) {
//准备参数
Map<String, Object> param = new HashMap<>();
param.put("pageNo", pageNo);
param.put("pageSize", pageSize);
param.put("name", name);
param.put("ownerName", ownerName);
param.put("startDate", startDate);
param.put("endDate", endDate);
//调用业务接口层的方法
//多条件分页查询市场活动列表数据 -> 返回‘分页模型对象’:每页展示数据集合,总记录数
//按照面向对象编辑思想,将分页查询的数据封装到一个分页模型对象中
PageBean<Activity> pageBean = activityService.queryActivityListForPageByCondition(param);
return pageBean;
}
分页serviceImpl
public PageBean<Activity> queryActivityListForPageByCondition(Map<String, Object> param) {
PageBean<Activity> pageBean = new PageBean<>((Integer) param.get("pageNo"), (Integer) param.get("pageSize"));
Integer offset = pageBean.getStartIndex();
param.put("offset", offset);//起始行
//1.获取每页显示的数据集合
List activityList = activityMapper.selectActivityListForPageByCondition(param);
pageBean.setDataList(activityList);//当前行的数据
//2.获取总记录数
Integer total = activityMapper.selectTotal(param);
pageBean.setTotal(total);//总行数和总页数
return pageBean;
}
pageHelper
如果使用pageHelper则不需要提供计算总记录数的sql,也不需要上面的,直接用即可,非常方便。但是具体属性名称得按照pagehelper里面的属性,然后提供给前端界面进行展示即可。
发送邮件
mail工具类
mail
public class Mail {
private MailSender mailSender;
public MailSender getMailSender() {
return mailSender;
}
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public Date sendMail() throws MessagingException {
Session session=Session.getInstance(mailSender.getProperties());
MimeMessage message=null;
Transport transport=null;
try{
message=new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(mailSender.getSenderName()));
// Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(mailSender.getRecipientAddress()));
// Set Subject: 头部头字段
message.setSubject(mailSender.getSubject());
// 设置消息体
// message.setText("来自赵老六的测试邮件");
message.setContent(mailSender.getContent(),
"text/html;charset=gb2312");
message.saveChanges(); // implicit with send()
message.setSentDate(new Date());
session.setDebug(true);
// Transport transport = session.getTransport("smtp");
transport = session.getTransport();
transport.connect(mailSender.getMailServerHost(),mailSender.getMailServerPort(),mailSender.getSenderName(),mailSender.getSenderPassword());
//发送
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
transport.sendMessage(message,message.getAllRecipients());
transport.close();
// 发送消息
//Transport.send(message);
}catch (MessagingException mex) {
mex.printStackTrace();
}
return message.getSentDate();
}
}
mailSender
public class MailSender {
private String senderName;//发件人姓名
private String senderPassword;//发件人密码
private String recipientAddress;//收件人地址
private int mailServerPort=25;//邮件服务端口
private String mailServerHost;//邮件服务主机
private Properties properties=System.getProperties();
private String subject;//邮件头部
private String text;//邮件文本
private String content;//邮件内容
public String getSenderName() {
return senderName;
}
public void setSenderName(String senderName) {
this.senderName = senderName;
}
public String getSenderPassword() {
return senderPassword;
}
public void setSenderPassword(String senderPassword) {
this.senderPassword = senderPassword;
}
public String getRecipientAddress() {
return recipientAddress;
}
public void setRecipientAddress(String recipientAddress) {
this.recipientAddress = recipientAddress;
}
public int getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(int mailServerPort) {
this.mailServerPort = mailServerPort;
}
public String getMailServerHost() {
return mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public Properties getProperties() {
properties.setProperty("mail.smtp.auth", "true");
//设置认证协议
properties.setProperty("mail.transport.protocol","smtp");
return properties;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
controller
/**
* 发送验证码
*
* @param request
* @return
*/
@RequestMapping("/sendCode")
@ResponseBody
public Object sendCode(HttpServletRequest request, HttpSession session) throws Exception {
if (true) {
MailSender mailSender = new MailSender();
mailSender.setSenderName("******@qq.com");
mailSender.setSenderPassword("********");
String email = request.getParameter("email");
if (email != null ) {
mailSender.setRecipientAddress(email);
}
mailSender.setMailServerHost("smtp.qq.com");
mailSender.setSubject("修改密码验证!");
String result = "";
for (int i = 0; i < 6; i++) {
int intVal = (int) (Math.random() * 26 + 97);
result = result + (char) intVal;
}
String content = "您的验证码为:" + result;
mailSender.setContent(content);
Mail mail = new Mail();
mail.setMailSender(mailSender);
//返回邮件发送时间
Date mailSentDate = mail.sendMail();
session.setAttribute("code", result);
ajaxResult.ajaxTrue("验证码发送成功,请去修改密码");
} else {
ajaxResult.ajaxFalse("验证码发送失败");
}
return ajaxResult;
}