学生选课系统--配置文件+工具类+实体类+登录选择页面+登录的账户密码检查
学生选课系统--配置文件+工具类+实体类+登录选择页面+登录的账户密码检查
这些代码差不多都不会再改变的了。
1.Stu.java(Stu实体类)
package com.xxxx.entity; public class Stu { private int id;//学号 private String name;//姓名 private String sex;//性别 private int age;//年龄 private String pro;//专业 private String cla;//班级 private int claid;//已选课程id public int getClaid() { return claid; } public void setClaid(int claid) { this.claid = claid; } public Stu(int id, String name, String sex, int age, String pro, String cla, int claid) { this.id = id; this.name = name; this.sex = sex; this.age = age; this.pro = pro; this.cla = cla; this.claid = claid; } public int getId() { return id; } public Stu(String name, String sex, int age, String pro, String cla) { this.name = name; this.sex = sex; this.age = age; this.pro = pro; this.cla = cla; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPro() { return pro; } public void setPro(String pro) { this.pro = pro; } public String getCla() { return cla; } public void setCla(String cla) { this.cla = cla; } public Stu() { } public Stu(int id, String name, String sex, int age, String pro, String cla) { this.id = id; this.name = name; this.sex = sex; this.age = age; this.pro = pro; this.cla = cla; } }
2.Tea.java(Tea实体类)
package com.xxxx.entity; public class Tea { private int id;//工号 private String name;//姓名 private String pro;//职位 private String yuan;//所属院 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPro() { return pro; } public void setPro(String pro) { this.pro = pro; } public String getYuan() { return yuan; } public void setYuan(String yuan) { this.yuan = yuan; } public Tea(int id, String name, String pro, String yuan) { this.id = id; this.name = name; this.pro = pro; this.yuan = yuan; } public Tea() { } }
3.Cla.java(Cla实体类)
package com.xxxx.entity; public class Cla { private int id;//课程编号 private String name;//课程名称 private String time;//开课时间星期几 private String ci;//上课时间1-2节 private String yuan;//开课院系 private String tea;//任课老师名字 private int teaid;//老师id private int people;//限制人数 private int xuan=0;//已选人数默认为0 public int getId() { return id; } public int getXuan() { return xuan; } public void setXuan(int xuan) { this.xuan = xuan; } public Cla(int id, String name, String time, String ci, String yuan, String tea, int teaid, int people, int xuan) { this.id = id; this.name = name; this.time = time; this.ci = ci; this.yuan = yuan; this.tea = tea; this.teaid = teaid; this.people = people; this.xuan = xuan; } public Cla(String name, String time, String ci, String yuan, String tea, int teaid, int people) { this.name = name; this.time = time; this.ci = ci; this.yuan = yuan; this.tea = tea; this.teaid = teaid; this.people = people; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getCi() { return ci; } public void setCi(String ci) { this.ci = ci; } public String getYuan() { return yuan; } public void setYuan(String yuan) { this.yuan = yuan; } public String getTea() { return tea; } public void setTea(String tea) { this.tea = tea; } public int getTeaid() { return teaid; } public void setTeaid(int teaid) { this.teaid = teaid; } public int getPeople() { return people; } public void setPeople(int people) { this.people = people; } public Cla() { } public Cla(int id, String name, String time, String ci, String yuan, String tea, int teaid, int people) { this.id = id; this.name = name; this.time = time; this.ci = ci; this.yuan = yuan; this.tea = tea; this.teaid = teaid; this.people = people; } }
4.Login.java(Login实体类)
package com.xxxx.entity; public class Login { private String uname; private String upwd; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } public Login() { } public Login(String uname, String upwd) { this.uname = uname; this.upwd = upwd; } }
5.GetSqlSession.java(工具类)
package com.xxxx.util; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class GetSqlSession { /** * 获取SqlSession对象 */ public static SqlSession CreateSqlSession() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //获取SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(true);//关闭事物,否则要手动提交事务 return sqlSession; } }
6.mybatis-config.xml(配置文件)
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- properties 标签中的配置可以供整个配置文件使用,在任何位置都可以引入其中配置的值。 properties 标签可以通过子标签 property 标签来配置一些子元素信息,也可以配置外部的动态文件。 --> <properties resource="mysql.properties"/> <!-- 也可以配置 url,但是 url和resource只能存在一个 --> <!-- 类型别名 --> <!-- <typeAliases>--> <!-- <typeAlias type="com.xxxx.entity.User" alias="User"/>--> <!-- </typeAliases>--> <!-- 对事物的管理和连接池的配置 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <!--连接池信息--> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!--mappers映射器--> <mappers> <!--映射文件方式1.一个一个的配置--> <!-- <mapper resource="com/xxxx/mapper/UserMapper.xml"/>--> <!-- <mapper class="com.xxxx.mapper.UserMapper"/>--> <!--映射文件方式2.自动扫描包内的Mapper接口和配置文件--> <package name="com.xxxx.mapper"/> </mappers> </configuration>
7.mysql.properties(配置文件)
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/course?serverTimezone=UTC&useSSL=false&characterEncoding=UTF-8 username=root password=123456
8.yemian.jsp(登录的页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <%-- 选择登录界面:学生、教师、管理员,并且输入账号密码 --%> <head> <title>选择登录</title> </head> <body> <form method="post" action="yemianServlet"> <h2>石家庄铁道大学学生选课管理系统</h2> 账号:<input name="uname" type="text" id="uname"><br> 密码:<input name="upwd" type="password" id="upwd"><br> 选择登录身份:<input type="radio" name="logintype" value="学生" checked>学生 <input type="radio" name="logintype" value="教师">教师 <input type="radio" name="logintype" value="管理员">管理员<br> <button>登录</button><span style="font-size: 15px;color: red" id="msg">${msg}</span> </form> </body> </html>
9.yemianServlet.java(登录检查,账号密码是否正确,获取身份)
package com.xxxx.servlet; import com.xxxx.entity.Login; import com.xxxx.mapper.LoginMapper; import com.xxxx.util.GetSqlSession; import org.apache.ibatis.session.SqlSession; 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; /** * 账户存在-->比较密码: * 正确-->判断登录身份-->进入对应身份的显示页面(重定向:session) * 错误-->回去刚刚的登录页面(跳转) */ @WebServlet("/yemianServlet") public class yemianServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); String uname=request.getParameter("uname");//账号 String upwd=request.getParameter("upwd");//密码 String logintype=request.getParameter("logintype");//登录身份 SqlSession sqlSession = GetSqlSession.CreateSqlSession(); LoginMapper loginMapper = sqlSession.getMapper(LoginMapper.class); Login login=loginMapper.selectByUname(uname); if(login!=null){ if(!upwd.equals(login.getUpwd())){ request.setAttribute("msg","密码错误"); request.getRequestDispatcher("yemian.jsp").forward(request,response); } else { request.getSession().setAttribute("uname",uname);//设置域对象,范围:**session** if(logintype.equals("学生")) response.sendRedirect("stu.jsp"); else if(logintype.equals("教师")) response.sendRedirect("tea.jsp"); else response.sendRedirect("admin.jsp"); } } else{ request.setAttribute("msg","用户不存在"); request.getRequestDispatcher("yemian.jsp").forward(request,response); } sqlSession.close();//关闭资源 } }