目录
3.1 面向接口编程,创建BaseAdminDao接口
完整项目在这里实时更新<( ̄︶ ̄)↗[GO!]:https://github.com/GoodbyeFirefly/ExpressManagementSystem
1,导入后台操作模板
在web目录下创建admin文件夹,将素材模板粘贴进去
2,创建数据库及管理员信息表
以下代码为navicat工具自动生成
/*
Navicat Premium Data Transfer
Source Server : localhost_3306
Source Server Type : MySQL
Source Server Version : 50731
Source Host : localhost:3306
Source Schema : ems
Target Server Type : MySQL
Target Server Version : 50731
File Encoding : 65001
Date: 05/09/2021 10:46:27
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for eadmin
-- ----------------------------
DROP TABLE IF EXISTS `eadmin`;
CREATE TABLE `eadmin` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`password` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`loginip` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`logintime` datetime(0) NULL DEFAULT NULL,
`createtime` datetime(0) NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `username`(`username`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of eadmin
-- ----------------------------
INSERT INTO `eadmin` VALUES (1, 'admin', '123', NULL, NULL, '2020-06-06 09:45:09');
SET FOREIGN_KEY_CHECKS = 1;
3,编写管理员数据与数据库的交互逻辑
3.1 面向接口编程,创建BaseAdminDao接口
package com.xxy.dao;
import java.util.Date;
public interface BaseAdminDao {
/**
* 根据用户名更新登陆时间和登录ip
* @param username
* @param date
* @param ip
*/
void updateLoginTime(String username, Date date, String ip);
/**
* 判断登录是否成功
* @param username
* @param password
* @return
*/
boolean login(String username, String password);
}
3.2 编写BaseAdminDao接口实现类
实现BaseAdminDao接口
package com.xxy.dao.impl;
import com.xxy.dao.BaseAdminDao;
import com.xxy.util.DruidUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
public class AdminDaoMysql implements BaseAdminDao {
private static final String SQL_UPDATE_LOGIN_TIME = "UPDATE EADMIN SET LOGINTIME=?,LOGINIP=? WHERE USERNAME=?";
private static final String SQL_LOGIN = "SELECT ID FROM EADMIN WHERE USERNAME=? AND PASSWORD=?";
/**
* 根据用户名更新登陆时间和登录ip
*
* @param username
* @param date
* @param ip
*/
@Override
public void updateLoginTime(String username, Date date, String ip) {
Connection conn = DruidUtil.getConnection();
PreparedStatement state = null;
try {
state = conn.prepareStatement(SQL_UPDATE_LOGIN_TIME);
// 这里的date是util包中的类,作为参数传递方便处理。传入时需要转换为sql包中的Date类
state.setDate(1, new java.sql.Date(date.getTime()));
state.setString(2, ip);
state.setString(3, username);
state.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DruidUtil.close(conn, state, null);// 这里不需要返回结果,所以传入null
}
}
/**
* 判断登录是否成功
*
* @param username
* @param password
* @return
*/
@Override
public boolean login(String username, String password) {
Connection conn = DruidUtil.getConnection();
PreparedStatement state = null;
ResultSet rs = null;
try {
state = conn.prepareStatement(SQL_LOGIN);
// 这里的date是util包中的类,作为参数传递方便处理。传入时需要转换为sql包中的Date类
state.setString(1, username);
state.setString(2, password);
rs = state.executeQuery();
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DruidUtil.close(conn, state, rs);
}
return false;
}
}
3.3 编写service类
为避免controller层和dao层紧耦合(这两层比较重要,若发生改动对程序影响较大),在两者中间加入service层。
package com.xxy.service;
import com.xxy.dao.BaseAdminDao;
import com.xxy.dao.impl.AdminDaoMysql;
import java.util.Date;
public class AdminService {
private static BaseAdminDao dao = new AdminDaoMysql();
public static void updateLoginTimeAndIP(String username, Date date, String ip) {
dao.updateLoginTime(username, date, ip);
}
public static boolean login(String username, String password) {
return dao.login(username, password);
}
}
标签:username,BaseAdminDao,07,训练,param,state,管理员,import,String From: https://blog.51cto.com/u_15849465/5803802章节汇总在这里(づ ̄3 ̄)づ╭❤~@&再见萤火虫&【07-项目训练】
对学习Java感兴趣的同学欢迎加入QQ学习交流群:1126298731
有问题欢迎提问,大家一起在学习Java的路上打怪升级!(o゜▽゜)o☆[BINGO!]