用户信息列表展示
1.需求:用户信息的增删改查操作
2.设计:
1.技术选型:Servlet+JSP+MySQL+JDBCTempleat+Duird+BeanUtilS+tomcat
2.数据库设计:
CREATE TABLE `user`(-- 创建表 id INT PRIMARY KEY auto_increment,-- 编号 name VARCHAR(20) NOT NULL,-- 姓名 gender VARCHAR(5),-- 性别 age int,-- 年龄 address VARCHAR(32),-- 籍贯 qq VARCHAR(20),-- qq email VARCHAR(50)-- 邮箱 )
insert into `user`(`id`,`name`,`gender`,`age`,`address`,`qq`,`email`) values (1,'张三','男',13,'陕西','12345','[email protected]'); insert into `user`(`id`,`name`,`gender`,`age`,`address`,`qq`,`email`) values (2,'李四','女',15,'北京','88888','[email protected]');
3.开发:
1.环境搭建
1.创建数据库环境
2.创建项目,导入需要的jar包
2.编码
4.测试
5.部署运维
列表查询分析
domain层User1实体类
package com.example.domain; /** * 实体类 */ public class User1 { private int id; private String name; private String gender; private int age; private String address; private String qq; private String email; @Override public String toString() { return "User1{" + "id=" + id + ", name='" + name + '\'' + ", gender='" + gender + '\'' + ", age=" + age + ", address='" + address + '\'' + ", qq='" + qq + '\'' + ", email='" + 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; } }
dao层的UserDao接口:
package com.example.dao; import com.example.domain.User1; import java.util.List; /** * 用户操作的dao */ public interface UserDao { public List<User1> findAll(); }
导入JDBCUtils工具类和druid.properties配置文件
dao层的小包implUserDaoImpl类:
package com.example.dao.impl; import com.example.dao.UserDao; import com.example.domain.User1; import com.example.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<User1> findAll() { //实现JDBC数据库操作 //定义sql String sql = "SELECT * FROM user1"; List<User1> users = template.query(sql, new BeanPropertyRowMapper<User1>(User1.class)); return users; } }
service层的UserService接口:
package com.example.service; import com.example.domain.User1; import java.util.List; public interface UserService { public List<User1> findAll(); }
service层下的小包impl的UserService接口:
package com.example.service.impl; import com.example.dao.UserDao; import com.example.dao.impl.UserDaoImpl; import com.example.domain.User1; import com.example.service.UserService; import java.util.List; public class UserServiceImpl implements UserService { public UserDao dao = new UserDaoImpl(); @Override public List<User1> findAll() { //调用dao完成查询 return dao.findAll(); } }
web层下的小包servlet的UserListServlet:
package com.example.web.servlet; import com.example.domain.User1; import com.example.service.UserService; import com.example.service.impl.UserServiceImpl; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.util.List; @WebServlet(name = "UserListServlet", value = "/UserListServlet") public class UserListServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.调用UserService完成查询 UserService service = new UserServiceImpl(); List<User1> users = service.findAll(); //2.将list存入request域 request.setAttribute("users", users); //3.转发到list.jsp request.getRequestDispatcher("/list.jsp").forward(request, response); } }
index.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"/> <title>首页</title> <!-- 1. 导入CSS的全局样式 --> <link href="css/bootstrap.min.css" rel="stylesheet"> <!-- 2. jQuery导入,建议使用1.9以上的版本 --> <script src="js/jquery-2.1.0.min.js"></script> <!-- 3. 导入bootstrap的js文件 --> <script src="js/bootstrap.min.js"></script> <script type="text/javascript"> </script> </head> <body> <div align="center"> <a href="${pageContext.request.contextPath}/UserListServlet" style="text-decoration:none;font-size:33px">查询所有用户信息 </a> </div> </body> </html>
list.jsp:
<%@ 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"> <!-- 使用Edge最新的浏览器的渲染方式 --> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。 width: 默认宽度与设备的宽度相同 initial-scale: 初始的缩放比,为1:1 --> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! --> <title>用户信息管理系统</title> <!-- 1. 导入CSS的全局样式 --> <link href="css/bootstrap.min.css" rel="stylesheet"> <!-- 2. jQuery导入,建议使用1.9以上的版本 --> <script src="js/jquery-2.1.0.min.js"></script> <!-- 3. 导入bootstrap的js文件 --> <script src="js/bootstrap.min.js"></script> <style type="text/css"> td, th { text-align: center; } </style> </head> <body> <div class="container"> <h3 style="text-align: center">用户信息列表</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-primary" href="add.html">添加联系人</a></td> </tr> </table> </div> </body> </html>
标签:qq,String,展示,用户,列表,example,import,com,public From: https://www.cnblogs.com/qihaokuan/p/17003556.html