1,数据库创建存储专业信息的表
2,后端:
连接数据库工具类DBUtil.java:
package com.ffyc.webserver.util;import java.sql.*;public class DButils {static {try {Class.forName("com.mysql.cj.jdbc.Driver");} catch (ClassNotFoundException e) {e.printStackTrace();}}public static Connection getConnection() throws SQLException {String url = "jdbc:mysql://127.0.0.1:3306/userdb?serverTimezone=Asia/Shanghai";Connection connection = DriverManager.getConnection(url, "root", "123456");return connection;}public static void close(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {if (resultSet != null) {resultSet.close();}if (statement != null) {statement.close();}if (connection != null) {connection.close();}}
}
专业类信息:
package com.ffyc.webserver.Models;import java.util.Date;public class Major {int id;String name;int adminid;Date oper_time;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 int getAdminid() {return adminid;}public void setAdminid(int adminid) {this.adminid = adminid;}public Date getOper_time() {return oper_time;}public void setOper_time(Date oper_time) {this.oper_time = oper_time;}
}
后端对数据库查询:
package com.ffyc.webserver.dao;import com.ffyc.webserver.Models.Major;
import com.ffyc.webserver.Models.Student;
import com.ffyc.webserver.util.DButils;import javax.imageio.ImageReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;public class MajorDao {public List<Major>majors() throws SQLException {List<Major>list=new ArrayList<>();Connection connection= DButils.getConnection();String sql="SELECT m.id,m.name,a.account,m.oper_time\n" +"FROM major m LEFT JOIN USER a \n" +"ON a.id=m.adminid;";PreparedStatement preparedStatement= connection.prepareStatement(sql);ResultSet resultSet=preparedStatement.executeQuery();while(resultSet.next()){Major major=new Major();major.setId(resultSet.getInt("id"));major.setAccount(resultSet.getString("account"));major.setName(resultSet.getString("name"));major.setOper_time(resultSet.getDate("oper_time"));list.add(major);}DButils.close(resultSet,preparedStatement,connection);return list;}
}
创建专业servlet:MajorServlet.java
package com.ffyc.webserver.servlet;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;public class MajorServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doGet(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost(req, resp);}
}
重写代码:
package com.ffyc.webserver.servlet;import com.fasterxml.jackson.databind.ObjectMapper;
import com.ffyc.webserver.Models.CommonData;
import com.ffyc.webserver.Models.Major;
import com.ffyc.webserver.dao.MajorDao;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;public class MajorServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {PrintWriter printWriter= resp.getWriter();CommonData commonData=null;try{MajorDao majorDao=new MajorDao();List<Major>majorlist= majorDao.majors();commonData=new CommonData(200,majorlist,"查询成功!");} catch (SQLException throwables) {throwables.printStackTrace();commonData=new CommonData(401,"服务器忙...请稍后重试!");}ObjectMapper objectMapper=new ObjectMapper();String json=objectMapper.writeValueAsString(commonData);printWriter.print(json);}
}
配置web.xml
<servlet><servlet-name>Major</servlet-name><servlet-class>com.webserver.servlet.MajorServlet</servlet-class><!-- <load-on-startup>0</load-on-startup>--></servlet><servlet-mapping><servlet-name>Major</servlet-name><url-pattern>/admin/MajorServlet</url-pattern></servlet-mapping>
前端:
<template><div><el-button type="primary">添加专业信息</el-button><el-table :data="tableData" height="400" border style="width: 100%"><el-table-column prop="id" label="序号" width="100"></el-table-column><el-table-column prop="name" label="专业名" width="100"></el-table-column><el-table-column prop="account" label="修改人"></el-table-column><el-table-column prop="oper_time" label="修改时间" align="center"></el-table-column><el-table-column width="200"><template slot-scope="scope"><el-button type="primary" icon="el-icon-edit"></el-button><el-button type="primary" icon="el-icon-delete"></el-button></template></el-table-column></el-table></div>
</template>
<script>export default {data() {return {tableData: []}},mounted() {this.$http.get("admin/MajorServlet").then(resp => {this.tableData = resp.data.data;})}}
</script><style>
</style>
效果展示:
以上仅完成了对专业信息的查询。