2024.12.27

package com.men.employee;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.men.common.model.Employee;

import java.util.List;

@Before(EmployeeInterceptor.class)
public class EmployeeController extends Controller {

@Inject
EmployeeService service = new EmployeeService();

/**
* 主界面
*/
public void index() {
if (getSession().getAttribute("loginUser") == null) {
redirect("/");
} else {
render("student.html");
}
}

/**
* 员工列表
*/
public void list() {
setAttr("list", service.paginate(getParaToInt(0, 1), 5));
render("list.html");
}

/**
* 添加员工
*/
public void add() {
render("add.html");
}

public void save() {
getModel(Employee.class).save();
redirect("/student/list");
}

/**
* 更新员工信息
*/
public void edit() {
setAttr("employee", service.findById(getParaToInt()));
render("update.html");
}

public void update() {
getModel(Employee.class).update();
redirect("/student/list");
}

/**
* 删除员工信息
*/
public void delete() {
service.deleteById(getParaToInt());
redirect("/student/list");
}

/**
* 模糊查询
*/
public void search() {
String param = getPara("param");

if (param != null && !param.isEmpty()) {
List<Record> employee = service.searchEmployee(param);

setAttr("employee", employee);
setAttr("param", param);
} else {
List<Employee> allEmployee = service.findAll();
setAttr("employee", allEmployee);
}

render("/student/search.html");
}
}
package com.men.employee;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;

public class EmployeeInterceptor implements Interceptor {

@Override
public void intercept(Invocation inv) {
System.out.println("Before invoking " + inv.getActionKey());
inv.invoke();
System.out.println("After invoking " + inv.getActionKey());
}
}
package com.men.employee;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.men.common.model.Employee;

import java.util.List;

/**
* 员工业务逻辑层
*/
public class EmployeeService {

private Employee dao = new Employee().dao();

/**
* 分页获取员工列表
*/
public Page<Employee> paginate(int pageNumber, int pageSize) {
return dao.paginate(pageNumber, pageSize, "select *", "from employee order by EmployeeID asc");
}

/**
* 根据 ID 查找员工
*/
public Employee findById(int employeeID) {
return dao.findById(employeeID);
}

/**
* 删除员工信息
*/
public void deleteById(int employeeID) {
dao.deleteById(employeeID);
}

/**
* 模糊查询员工
*/
public List<Record> searchEmployee(String keyword) {
String sql = "select * from employee where employeeID like ? or name like ? or position like ? or processID like ?";
keyword = "%" + keyword.trim() + "%";
return Db.find(sql, keyword, keyword, keyword, keyword);
}


/**
* 查找所有员工
*/
public List<Employee> findAll() {
return dao.find("select * from employee");
}

public Employee findUserByNameAndPassword(String account) {
return dao.findFirst("select * from admin where account = ?", account);
}

}
package com.men.index;

import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.men.common.model.Employee;
import com.men.employee.EmployeeService;

/**
* @author mendianyu
*/
public class IndexController extends Controller
{

EmployeeService service = new EmployeeService();

//前台渲染登录界面
public void index()
{
render("index.html");
}

/**
* 用户登录验证
*/
@ActionKey("/login")
public void login()
{
//获取用户输入的账号和密码
String account = getPara("account");
String password = getPara("password");

//从数据库中获取数据
Employee userMassage = service.findUserByNameAndPassword(account);

//验证用户名和密码是否正确
if (userMassage != null)
{
if (password.equals(userMassage.getStr("password")))
{
setSessionAttr("loginUser", userMassage);
redirect("/student");
} else
{
setAttr("errorMsg", "用户名或密码错误,请重新输入。");
render("index.html");
return;
}
} else
{
setAttr("errorMsg", "该用户名不存在,请重新输入。");
render("index.html");
return;
}
}

/**
* 用户退出
*/
@ActionKey("logout")
public void logout()
{
getSession().removeAttribute("loginUser");
render("index.html");
}
}



posted @   我也不想的  阅读(4)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示