请假
CREATE TABLE leave_requests (
request_id INT AUTO_INCREMENT PRIMARY KEY, -- 自动生成的请假申请ID
student_id CHAR(8) NOT NULL, -- 学号(八位数字)
name VARCHAR(50) NOT NULL, -- 姓名
gender CHAR(1) NOT NULL, -- 性别(假设使用 'M' 或 'F')
grade VARCHAR(10) NOT NULL, -- 年级
college VARCHAR(100) NOT NULL, -- 学院
major VARCHAR(100) NOT NULL, -- 专业
class VARCHAR(50) NOT NULL, -- 班级
leave_reason TEXT NOT NULL, -- 请假事由
leave_date DATE NOT NULL, -- 请假日期
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -- 创建时间
);
<?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.yanshi.mapper.ClassesMapper">
</mapper>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--起别名-->
<typeAliases>
<package name="com.yanshi.pojo"/><!--你自己的pojo-->
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///db1?useSSL=false&useServerPrepStmts=true"/>
<property name="username" value="root"/> <!--你自己的数据库名-->
<property name="password" value="2333"/> <!--你自己的数据库密码-->
</dataSource>
</environment>
</environments>
<mappers>
<!--扫描mapper-->
<package name="com.yanshi.mapper"/><!--你自己的mapper-->
</mappers>
</configuration>
import com.yanshi.pojo.Classes;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
public interface ClassesMapper {
@Select("select * from tb_class where name = #{name} ")
Classes selectByname(@Param("name") String name);//课程名查找
@Select("select * from tb_class where id = #{id} ")
Classes selectByid(@Param("id") String id);//id查找
@Insert("insert into tb_class values(null,#{id},#{name},#{num},#{teacher},#{pos})")
void add(Classes classes);//添加新数据
}
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class SqlSessionFactoryUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
}
import com.yanshi.mapper.ClassesMapper;
import com.yanshi.pojo.Classes;
import com.yanshi.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/FindByNameServlet")
public class FindByNameServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 接收课程名
String name=req.getParameter("name");
//2. 调用MyBatis完成查询
//2.1 获取SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory= SqlSessionFactoryUtils.getSqlSessionFactory();
//2.2 获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//2.3 获取Mapper
ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);
//2.4 调用方法
Classes classes = classesMapper.selectByname(name);
//2.5 释放资源
sqlSession.close();
//获取字符输出流,并设置content type
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
//3. 判断user释放为null
if(classes != null){
// 写出对应课程
writer.write(classes.toString());
}else {
// 判断没有课程
writer.write("没有找到该课程!");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--下面这是虚拟路径,ClassAdd是项目名,后面那串是FindByNameServlet里那个WebServlet声明的虚拟名,自己的要改!!!!!!-->
<form action="/ClassAdd/FindByNameServlet" method="post" id="form">
<p>课程名:<input id="name" name="name" type="text"></p>
<input type="submit" class="button" value="查找">
</form>
</body>
</html>
package com.example.mapper;
import com.example.model.LeaveRequest;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface Qingmapper {
// 根据学号、请假事由、请假时间的模糊查询
@Select("<script>" +
"SELECT name, college, major, class, leave_date, leave_reason " +
"FROM leave_requests " +
"WHERE 1=1 " +
"<if test='studentId != null and studentId != \"\"'>AND student_id LIKE CONCAT('%', #{studentId}, '%')</if> " +
"<if test='leaveReason != null and leaveReason != \"\"'>AND leave_reason LIKE CONCAT('%', #{leaveReason}, '%')</if> " +
"<if test='leaveDate != null'>AND leave_date = #{leaveDate}</if> " +
"</script>")
List<LeaveRequest> searchLeaveRequests(@Param("studentId") String studentId,
@Param("leaveReason") String leaveReason,
@Param("leaveDate") String leaveDate);
// 插入新的请假申请
@Insert("INSERT INTO leave_requests(student_id, name, gender, grade, college, major, class, leave_reason, leave_date) " +
"VALUES(#{studentId}, #{name}, #{gender}, #{grade}, #{college}, #{major}, #{class}, #{leaveReason}, #{leaveDate})")
void addLeaveRequest(LeaveRequest leaveRequest);
}