一些细节,比如jar包放哪里,怎么建包,可以看之前的博客
本篇思路参考:[1]
完整代码:https://github.com/wangwyForNewTime/javaWeb-FindFromMySQL
数据库与上篇用的一个不赘述了
首先和前面登录不同的是,查需要读取数据库所有数据并且呈现出来,所以需要用到JSLT库
知识点可以参考这个,但是上面给的版本很低。然后我弄了半天一直服务器报500,后来查了查,从这个博主这里知道,tomcat10以上的用2.0.0版本以上,所以从这个博主这里下到了2.0.0
然后把jar包放进lib里面
<%--
Created by IntelliJ IDEA.
User: 大喵喵
Date: 2023/2/28
Time: 17:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta charset="UTF-8">
<title>管理员</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/index.css">
</head>
<body>
<div class="mainbac">
<h1>Maybe Something System </h1>
<div class="lii">欢迎你,管理员</div>
<div class="duzii">
<form class="form-inline" action="index">
<div class="form-group has-success has-feedback">
<label class="control-label">ID:</label>
<input class="form-control" style="margin-left: 20px;" name="test">
<button type="submit" class="btn btn-default">查 询</button>
<button type="button" class="btn btn-link">返回首页</button>
</div>
</form>
<table class="table table-striped" style="margin-top: 50px;">
<thead>
<tr >
<th>ID</th>
<th>密码</th>
<th>身份</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td>Wen</td>
<td>123456</td>
<td>管理员</td>
<td><a href="#">设置</a> | <a href="#">删除</a></td>
</tr>
<c:forEach var="e" items="${list}">
<tr>
<td>${e.username}</td>
<td>${e.password}</td>
<td>管理员</td>
<td><a href="#">设置</a> | <a href="#">删除</a></td>
</tr>
</c:forEach>
<%-- <tr>--%>
<%-- <td>Te</td>--%>
<%-- <td>12</td>--%>
<%-- <td>普通用户</td>--%>
<%-- <td><a href="#">设置</a> | <a href="#">删除</a></td>--%>
<%-- </tr>--%>
</tbody>
</table>
</div>
</div>
</body>
</html>
想要测试链接上了数据库,所有操作可以直接看上一篇内容的3
这里的作用就是让java代码能够操作数据库。
分层思想没什么神奇的,就是你要实现的功能,你要愿意全写一个java文件里也行,但是你把不同的类分开到独立文件里面,看起来清晰明了,这就是分层思想
就是建立user类
package entity;
public class User {
private Integer userId;
private String username;
private String password;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
就是给java能操作MySQL牵线搭桥,创建的接口
这次要获取所有的数据,所以需要的命令是select * from pa(数据库名)
UserMapper.java
package mapper;
import entity.User;
import java.util.List;
public interface UserMapper {
public User queryUserByName(String username);
public List<User> findAll();
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!-- 引入dtd -->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace就是接口的包名加类名 -->
<mapper namespace="mapper.UserMapper">
<select id="queryUserByName" resultType="entity.User" parameterType="String">
<!-- 写SQL语句 -->
select * from pa where username = #{username}
</select>
<select id="findAll" resultType="entity.User" parameterType="String">
<!-- 写SQL语句 -->
select * from pa
</select>
</mapper>
没有这个就别想链接数据库
mysql.properties,注意test1为数据库名字,按需更换,密码是MySQL的密码:
driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=UTF-8
username = root
password = 1234
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 resource="mysql.properties"/>
<!-- 默认使用的环境 ID(比如:default="development")
也就是说我们可以配置多套<environment>环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- JDBC 驱动-->
<property name="driver" value="${driver}"/>
<!-- url数据库的 JDBC URL地址。-->
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.xxxx.mapper"/>
</mappers>
</configuration>
/**
*
*/
package com.xxxx.util;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class GetSqlSession {
public static SqlSession createSqlSession(){
SqlSessionFactory sqlSessionFactory = null;
InputStream input = null;
SqlSession session = null;
try{
//获取mybatis的环境配置文件
String resource ="mybatis-config.xml";
//以流的方式获取resourse
input = Resources.getResourceAsStream(resource);
//创建会话工厂
sqlSessionFactory=new SqlSessionFactoryBuilder().build(input);
//通过工厂得到SqlSession
session=sqlSessionFactory.openSession();
return session;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args){
System.out.println(createSqlSession());
}
}
可以写个函数测试一些是否能联上数据库,具体看上一篇博客3.7
jsp想向后台传东西,后台想给jsp传数值,都需要servlet。如果实现的功能复杂,可以把实现功能的代码放到service层里面。但是这里代码简单,我就没分开,直接写在servlet层了
package servlet;
import entity.User;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import service.Userservice;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import util.GetSqlSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/index")//这个/很重要
public class Userservlet extends HttpServlet {
// private Userservice userService =new Userservice();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String uTestValue =request.getParameter("test");
System.out.println(uTestValue);
SqlSession session = GetSqlSession.createSqlSession();
UserMapper userMapper = session.getMapper(UserMapper.class);
List<User> list=userMapper.findAll();
System.out.println(list);
request.setAttribute("list", list);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
实在不想自己研究了,把这个博主的代码调通了直接拿来用了。
整个代码:https://github.com/wangwyForNewTime/JavaWeb-Add-delete-correct-and-check