image.png

demo1.0

思路

image.png

  1. 完成一个用户信息查询功能
  2. 1、数据库创建USER
  3. int id;
  4. String name;
  5. String gender;
  6. int age;
  7. String address;
  8. String qq;
  9. String email;
  10. 2、思路:
  11. 用户通过index.jsp点击查询所有用户信息,在list.jsp上面显示
  12. index.jspservlet里面查询,servlet调用服务层的方法进行查询,然后返回信息到list.jsp
  13. 服务层:存在一个接口UserService和实现类UserServiceImpl,服务层调用dao层的方法进行发现用户数据
  14. dao层:存在一个接口UserDao和实现类UserDaoImpldao层使用JDBC操作数据库,调用了util工具类中的jdbcutils

配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.example</groupId>
  7. <artifactId>zhongdiandemo1</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <name>zhongdiandemo1</name>
  10. <packaging>war</packaging>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <maven.compiler.target>1.8</maven.compiler.target>
  14. <maven.compiler.source>1.8</maven.compiler.source>
  15. <junit.version>5.7.1</junit.version>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>javax.servlet</groupId>
  20. <artifactId>javax.servlet-api</artifactId>
  21. <version>4.0.1</version>
  22. <scope>provided</scope>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.junit.jupiter</groupId>
  26. <artifactId>junit-jupiter-api</artifactId>
  27. <version>${junit.version}</version>
  28. <scope>test</scope>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.junit.jupiter</groupId>
  32. <artifactId>junit-jupiter-engine</artifactId>
  33. <version>${junit.version}</version>
  34. <scope>test</scope>
  35. </dependency>
  36. <dependency>
  37. <groupId>com.alibaba</groupId>
  38. <artifactId>druid</artifactId>
  39. <version>1.2.6</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>mysql</groupId>
  43. <artifactId>mysql-connector-java</artifactId>
  44. <version>8.0.22</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework</groupId>
  48. <artifactId>spring-jdbc</artifactId>
  49. <version>5.3.9</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>commons-beanutils</groupId>
  53. <artifactId>commons-beanutils</artifactId>
  54. <version>1.9.4</version>
  55. </dependency>
  56. <dependency>
  57. <groupId>javax.servlet</groupId>
  58. <artifactId>servlet-api</artifactId>
  59. <version>2.5</version>
  60. <scope>provided</scope>
  61. </dependency>
  62. <dependency>
  63. <groupId>javax.servlet</groupId>
  64. <artifactId>jsp-api</artifactId>
  65. <version>2.0</version>
  66. <scope>provided</scope>
  67. </dependency>
  68. <dependency>
  69. <groupId>jstl</groupId>
  70. <artifactId>jstl</artifactId>
  71. <version>1.2</version>
  72. </dependency>
  73. <dependency>
  74. <groupId>taglibs</groupId>
  75. <artifactId>standard</artifactId>
  76. <version>1.1.2</version>
  77. </dependency>
  78. </dependencies>
  79. <build>
  80. <plugins>
  81. <plugin>
  82. <groupId>org.apache.maven.plugins</groupId>
  83. <artifactId>maven-war-plugin</artifactId>
  84. <version>3.3.1</version>
  85. </plugin>
  86. </plugins>
  87. </build>
  88. </project>
  1. url=jdbc:mysql://ip:3306/lww?useUnicode=true&characterEncoding=utf8
  2. username=root
  3. password=xxxxxxx
  4. driver=com.mysql.cj.jdbc.Driver

jsp

  1. <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
  2. <!doctype html>
  3. <html lang="zh-CN">
  4. <head>
  5. <meta charset="utf-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1">
  8. <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
  9. <title>Bootstrap helloworld</title>
  10. <!-- Bootstrap
  11. 在zd项目中,如果不加上虚拟目录zd,查看网页会发现,引用的是http://localhost:8080/css/bootstrap.min.css
  12. 是没有办法访问到的,因为项目在tomcat中部署,在Tomcat的zd项目里面
  13. -->
  14. <link rel="stylesheet" href="/zd/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
  15. </head>
  16. <body>
  17. <div align="center">
  18. <a href="${pageContext.request.contextPath}/userListServlet" style="...">
  19. 查询所有用户信息
  20. </a>
  21. </div>
  22. <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
  23. <!--<script src="/js/jquery-3.6.0.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>-->
  24. <script src="/zd/js/jquery-3.6.0.min.js" crossorigin="anonymous"></script>
  25. <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
  26. <script src="/zd/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
  27. </body>
  28. </html>
  1. <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
  2. <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <!doctype html>
  4. <html lang="zh-CN">
  5. <head>
  6. <meta charset="utf-8">
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  8. <meta name="viewport" content="width=device-width, initial-scale=1">
  9. <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
  10. <title>Bootstrap helloworld</title>
  11. <!-- Bootstrap
  12. 在zd项目中,如果不加上虚拟目录zd,查看网页会发现,引用的是http://localhost:8080/css/bootstrap.min.css
  13. 是没有办法访问到的,因为项目在tomcat中部署,在Tomcat的zd项目里面
  14. -->
  15. <link rel="stylesheet" href="/zd/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
  16. <style type="text/css">
  17. h3,td,th{
  18. text-align: center;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="container">
  24. <h3 style="...">用户信息列表</h3>
  25. <table border="1" class="table table-bordered table-hover">
  26. <tr class="success">
  27. <th>编号</th>
  28. <th>姓名</th>
  29. <th>性别</th>
  30. <th>年龄</th>
  31. <th>籍贯</th>
  32. <th>QQ</th>
  33. <th>邮箱</th>
  34. <th>操作</th>
  35. </tr>
  36. <c:forEach items="${users}" var="user" varStatus="s">
  37. <tr>
  38. <td>${s.count}</td>
  39. <td>${user.name}</td>
  40. <td>${user.gender}</td>
  41. <td>${user.age}</td>
  42. <td>${user.address}</td>
  43. <td>${user.qq}</td>
  44. <td>${user.email}</td>
  45. <td><a class="btn btn-default btn-sm" href="update.html">修改</a>&nbsp;<a class="btn btn-default btn-sm" href="">删除</a></td>
  46. </tr>
  47. </c:forEach>
  48. <tr>
  49. <td colspan="8" align="center"><a class="btn btn-default" href="add.html">添加联系人</a></td>
  50. </tr>
  51. </table>
  52. </div>
  53. <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
  54. <!--<script src="/js/jquery-3.6.0.min.js" integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" crossorigin="anonymous"></script>-->
  55. <script src="/zd/js/jquery-3.6.0.min.js" crossorigin="anonymous"></script>
  56. <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
  57. <script src="/zd/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
  58. </body>
  59. </html>

servlet

  1. package com.example.zhongdiandemo1.web.servlet;
  2. import com.example.zhongdiandemo1.domain.User;
  3. import com.example.zhongdiandemo1.service.UserService;
  4. import com.example.zhongdiandemo1.service.impl.UserServiceImpl;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7. import javax.servlet.annotation.*;
  8. import java.io.IOException;
  9. import java.util.List;
  10. @WebServlet("/userListServlet")
  11. public class UserListServlet extends HttpServlet {
  12. @Override
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. //调用UserService完成查询
  15. UserService service = new UserServiceImpl();
  16. List<User> users = service.findAll();
  17. //将list存入request域
  18. request.setAttribute("users",users);
  19. //转发到list.jsp
  20. request.getRequestDispatcher("list.jsp").forward(request,response);
  21. }
  22. @Override
  23. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  24. this.doGet(request, response);
  25. }
  26. }

util

  1. package com.example.zhongdiandemo1.util;
  2. import com.alibaba.druid.pool.DruidDataSourceFactory;
  3. import javax.sql.DataSource;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.sql.Connection;
  7. import java.sql.SQLException;
  8. import java.util.Properties;
  9. public class jdbcutils {
  10. private static DataSource ds;
  11. static {
  12. try {
  13. //加载配置文件
  14. Properties pro = new Properties();
  15. InputStream is = jdbcutils.class.getClassLoader().getResourceAsStream("druid.properties");
  16. pro.load(is);
  17. //初始化连接池对象
  18. ds = DruidDataSourceFactory.createDataSource(pro);
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. /**
  26. * 获取连接池对象
  27. */
  28. public static DataSource getDataSource(){
  29. return ds;
  30. }
  31. /**
  32. * 获取Connection对象
  33. */
  34. public static Connection getConnection() throws SQLException {
  35. return ds.getConnection();
  36. }
  37. }

服务层

  1. package com.example.zhongdiandemo1.service;
  2. import com.example.zhongdiandemo1.domain.User;
  3. import java.util.List;
  4. /**
  5. * 用户管理的业务接口
  6. */
  7. public interface UserService {
  8. /**
  9. * 查询所有用户信息
  10. * @return
  11. */
  12. public List<User> findAll();
  13. }
  1. package com.example.zhongdiandemo1.service.impl;
  2. import com.example.zhongdiandemo1.dao.UserDao;
  3. import com.example.zhongdiandemo1.dao.impl.UserDaoImpl;
  4. import com.example.zhongdiandemo1.domain.User;
  5. import com.example.zhongdiandemo1.service.UserService;
  6. import java.util.List;
  7. public class UserServiceImpl implements UserService {
  8. //实例化一个dao的使用对象,使用多态的形式
  9. private UserDao dao = new UserDaoImpl();
  10. @Override
  11. public List<User> findAll() {
  12. //调用Dao完成查询
  13. return dao.findAll();
  14. }
  15. }

domain-javabean存放get/set方法的类

  1. package com.example.zhongdiandemo1.domain;
  2. public class User {
  3. private int id;
  4. private String name;
  5. private String gender;
  6. private int age;
  7. private String address;
  8. private String qq;
  9. private String email;
  10. public int getId() {
  11. return id;
  12. }
  13. public void setId(int id) {
  14. this.id = id;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public String getGender() {
  23. return gender;
  24. }
  25. public void setGender(String gender) {
  26. this.gender = gender;
  27. }
  28. public int getAge() {
  29. return age;
  30. }
  31. public void setAge(int age) {
  32. this.age = age;
  33. }
  34. public String getAddress() {
  35. return address;
  36. }
  37. public void setAddress(String address) {
  38. this.address = address;
  39. }
  40. public String getQq() {
  41. return qq;
  42. }
  43. public void setQq(String qq) {
  44. this.qq = qq;
  45. }
  46. public String getEmail() {
  47. return email;
  48. }
  49. public void setEmail(String email) {
  50. this.email = email;
  51. }
  52. @Override
  53. public String toString() {
  54. return "User{" +
  55. "id=" + id +
  56. ", name='" + name + '\'' +
  57. ", gender='" + gender + '\'' +
  58. ", age=" + age +
  59. ", address='" + address + '\'' +
  60. ", qq='" + qq + '\'' +
  61. ", email='" + email + '\'' +
  62. '}';
  63. }
  64. }

dao层

  1. package com.example.zhongdiandemo1.dao;
  2. import com.example.zhongdiandemo1.domain.User;
  3. import java.util.List;
  4. /**
  5. * 用户操作的Dao
  6. */
  7. public interface UserDao {
  8. public List<User> findAll();
  9. }
  1. package com.example.zhongdiandemo1.dao.impl;
  2. import com.example.zhongdiandemo1.dao.UserDao;
  3. import com.example.zhongdiandemo1.domain.User;
  4. import com.example.zhongdiandemo1.util.jdbcutils;
  5. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  6. import org.springframework.jdbc.core.JdbcTemplate;
  7. import java.util.List;
  8. public class UserDaoImpl implements UserDao {
  9. private JdbcTemplate template = new JdbcTemplate(jdbcutils.getDataSource());
  10. @Override
  11. public List<User> findAll() {
  12. //使用JDBC操作数据库
  13. String sql = "select * from USER";
  14. //使用BeanPropertyRowMapper时,是给query()方法传递一个BeanPropertyRowMapper对象,
  15. // 让JdbcTemplate帮我们把查询结果集users的每一行结果都使用BeanPropertyRowMapper.mapRow()方法,转化成我们想要的Java类对象
  16. List<User> users = template.query(sql, new BeanPropertyRowMapper<User>(User.class));
  17. return users;
  18. }
  19. }