demo1.0
思路
完成一个用户信息查询功能
1、数据库创建USER表
int id;
String name;
String gender;
int age;
String address;
String qq;
String email;
2、思路:
用户通过index.jsp点击查询所有用户信息,在list.jsp上面显示
index.jsp往servlet里面查询,servlet调用服务层的方法进行查询,然后返回信息到list.jsp
服务层:存在一个接口UserService和实现类UserServiceImpl,服务层调用dao层的方法进行发现用户数据
dao层:存在一个接口UserDao和实现类UserDaoImpl,dao层使用JDBC操作数据库,调用了util工具类中的jdbcutils类
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>zhongdiandemo1</artifactId>
<version>1.0-SNAPSHOT</version>
<name>zhongdiandemo1</name>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<junit.version>5.7.1</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
</project>
url=jdbc:mysql://ip:3306/lww?useUnicode=true&characterEncoding=utf8
username=root
password=xxxxxxx
driver=com.mysql.cj.jdbc.Driver
jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>Bootstrap helloworld</title>
<!-- Bootstrap
在zd项目中,如果不加上虚拟目录zd,查看网页会发现,引用的是http://localhost:8080/css/bootstrap.min.css
是没有办法访问到的,因为项目在tomcat中部署,在Tomcat的zd项目里面
-->
<link rel="stylesheet" href="/zd/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<div align="center">
<a href="${pageContext.request.contextPath}/userListServlet" style="...">
查询所有用户信息
</a>
</div>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<!--<script src="/js/jquery-3.6.0.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>-->
<script src="/zd/js/jquery-3.6.0.min.js" crossorigin="anonymous"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="/zd/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>Bootstrap helloworld</title>
<!-- Bootstrap
在zd项目中,如果不加上虚拟目录zd,查看网页会发现,引用的是http://localhost:8080/css/bootstrap.min.css
是没有办法访问到的,因为项目在tomcat中部署,在Tomcat的zd项目里面
-->
<link rel="stylesheet" href="/zd/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<style type="text/css">
h3,td,th{
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h3 style="...">用户信息列表</h3>
<table border="1" class="table table-bordered table-hover">
<tr class="success">
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>籍贯</th>
<th>QQ</th>
<th>邮箱</th>
<th>操作</th>
</tr>
<c:forEach items="${users}" var="user" varStatus="s">
<tr>
<td>${s.count}</td>
<td>${user.name}</td>
<td>${user.gender}</td>
<td>${user.age}</td>
<td>${user.address}</td>
<td>${user.qq}</td>
<td>${user.email}</td>
<td><a class="btn btn-default btn-sm" href="update.html">修改</a> <a class="btn btn-default btn-sm" href="">删除</a></td>
</tr>
</c:forEach>
<tr>
<td colspan="8" align="center"><a class="btn btn-default" href="add.html">添加联系人</a></td>
</tr>
</table>
</div>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
<!--<script src="/js/jquery-3.6.0.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>-->
<script src="/zd/js/jquery-3.6.0.min.js" crossorigin="anonymous"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="/zd/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
</body>
</html>
servlet
package com.example.zhongdiandemo1.web.servlet;
import com.example.zhongdiandemo1.domain.User;
import com.example.zhongdiandemo1.service.UserService;
import com.example.zhongdiandemo1.service.impl.UserServiceImpl;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
@WebServlet("/userListServlet")
public class UserListServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//调用UserService完成查询
UserService service = new UserServiceImpl();
List<User> users = service.findAll();
//将list存入request域
request.setAttribute("users",users);
//转发到list.jsp
request.getRequestDispatcher("list.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
util
package com.example.zhongdiandemo1.util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
public class jdbcutils {
private static DataSource ds;
static {
try {
//加载配置文件
Properties pro = new Properties();
InputStream is = jdbcutils.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//初始化连接池对象
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接池对象
*/
public static DataSource getDataSource(){
return ds;
}
/**
* 获取Connection对象
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
服务层
package com.example.zhongdiandemo1.service;
import com.example.zhongdiandemo1.domain.User;
import java.util.List;
/**
* 用户管理的业务接口
*/
public interface UserService {
/**
* 查询所有用户信息
* @return
*/
public List<User> findAll();
}
package com.example.zhongdiandemo1.service.impl;
import com.example.zhongdiandemo1.dao.UserDao;
import com.example.zhongdiandemo1.dao.impl.UserDaoImpl;
import com.example.zhongdiandemo1.domain.User;
import com.example.zhongdiandemo1.service.UserService;
import java.util.List;
public class UserServiceImpl implements UserService {
//实例化一个dao的使用对象,使用多态的形式
private UserDao dao = new UserDaoImpl();
@Override
public List<User> findAll() {
//调用Dao完成查询
return dao.findAll();
}
}
domain-javabean存放get/set方法的类
package com.example.zhongdiandemo1.domain;
public class User {
private int id;
private String name;
private String gender;
private int age;
private String address;
private String qq;
private String email;
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", address='" + address + '\'' +
", qq='" + qq + '\'' +
", email='" + email + '\'' +
'}';
}
}
dao层
package com.example.zhongdiandemo1.dao;
import com.example.zhongdiandemo1.domain.User;
import java.util.List;
/**
* 用户操作的Dao
*/
public interface UserDao {
public List<User> findAll();
}
package com.example.zhongdiandemo1.dao.impl;
import com.example.zhongdiandemo1.dao.UserDao;
import com.example.zhongdiandemo1.domain.User;
import com.example.zhongdiandemo1.util.jdbcutils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class UserDaoImpl implements UserDao {
private JdbcTemplate template = new JdbcTemplate(jdbcutils.getDataSource());
@Override
public List<User> findAll() {
//使用JDBC操作数据库
String sql = "select * from USER";
//使用BeanPropertyRowMapper时,是给query()方法传递一个BeanPropertyRowMapper对象,
// 让JdbcTemplate帮我们把查询结果集users的每一行结果都使用BeanPropertyRowMapper.mapRow()方法,转化成我们想要的Java类对象
List<User> users = template.query(sql, new BeanPropertyRowMapper<User>(User.class));
return users;
}
}