商品管理系统:
功能:
登录,记住账号密码,注销登录,商品基本增删改查,分页,高级查询
使用技术:
javase,mysql(8.0),mybatis持久层框架,mvc思想,web基础,bootstrap前端框架
开发:
1.建表
id 主键;唯一的区分一条数据
主键自增长:没有业务含义,不关心
表字段的约束:一般不加外键约束,影响性能
2.创建项目:
根(webapp)/WEB-INF(不能通过浏览器直接访问)/classes(存放项目编译之后的文件)
/lib(第三方的jar包)
/web.xml(对当前项目做配置)
3.部署项目
把项目交给Tomcat服务器管理
直接在Tomcat/conf/server.xml文件中配置
4.创建实体类
注意属性和表中字段的映射
5.完成持久层的开发
jdbc(在java代码中编写sql / 查询的结果集需要程序员自己封装)
MyBatis: 配置(主配置文件 / 映射文件----编写sql)
SqlSessionFactory
SqlSession
#{} 和 ${} 的区别和应用场景
6.完成业务层的开发
高级查询+分页查询的流程
QueryObject的作用
PageResult的作用
7.完成表现层的开发
请求分发
封装参数
共享数据
页面跳转
取值显示
实现用户登录:
提交表单,(username,password),数据库查询数据验证,验证成功跳转至商品列表界面,失败返回登录界面
LoginServlet.java
@WebServlet(name = "login", urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private IUserService service = new UserServiceImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
//获取页面数据
String username = req.getParameter("username");
String password = req.getParameter("password");
String remenber = req.getParameter("remenberMe");
//封装成User对象
User user = new User(username, password);
//根据获取的数据去数据库中查询对应的User信息
User u = service.selectByUsernameAndPassword(user);
//查询失败共享一个错误信息
if (u == null) {
req.setAttribute("errormsg", "用户名或者密码错误!");
req.getRequestDispatcher("login.jsp").forward(req, res);
//成功跳转至product
} else {
//判断是否需要记住用户名,密码
if (remenber != null) {
//将用户名和密码存入cookie
Cookie usernameCookie = new Cookie("username", username);
Cookie passwordCookie = new Cookie("password", password);
//存储时间为一周,不设置默认为会话cookie,浏览器关闭销毁
usernameCookie.setMaxAge(60 * 60 * 24 * 7);
passwordCookie.setMaxAge(60 * 60 * 24 * 7);
res.addCookie(usernameCookie);
res.addCookie(passwordCookie);
}
//将用户信息共享,以便于在其他界面获取用户信息
req.getSession().setAttribute("USER_IN_SESSION", u);
//跳转
res.sendRedirect("/product");
}
}
}
@WebServlet(name = "product", urlPatterns = "/product")
public class ProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private IProductService service = new ProductServiceImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
//请求分发
String cmd = req.getParameter("cmd");
if ("delete".equals(cmd)) {
delete(req, res);
} else if ("saveORupdate".equals(cmd)) {
saveORupdate(req, res);
} else if ("input".equals(cmd)) {
input(req, res);
} else {
list(req, res);
}
}
//商品列表
private void list(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//实现高级查询,分页查询需要的数据
String currentPage = req.getParameter("currentPage");
String pageSize = req.getParameter("pageSize");
String keyWord = req.getParameter("keyWord");
String minSalePrice = req.getParameter("minSalePrice");
String maxSalePrice = req.getParameter("maxSalePrice");
ProductQueryObject qo = new ProductQueryObject();
if (hasLength(currentPage)) {
qo.setCurrentPage(Integer.valueOf(currentPage));
}
if (hasLength(pageSize)) {
qo.setPageSize(Integer.valueOf(pageSize));
}
if (hasLength(keyWord)) {
qo.setKeyWord(keyWord);
}
if (hasLength(minSalePrice)) {
qo.setMinSalePrice(new BigDecimal(minSalePrice));
}
if (hasLength(maxSalePrice)) {
qo.setMaxSalePrice(new BigDecimal(maxSalePrice));
}
req.setAttribute("qo", qo);
PageResult result = service.queryForList(qo);
req.setAttribute("result", result);
req.getRequestDispatcher("/WEB-INF/views/list.jsp").forward(req, res);
}
//输入页面
private void input(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String id = req.getParameter("id");
//判断是更改还是插入
if (hasLength(id)) {
Product product = service.get(Long.valueOf(id));
req.setAttribute("p", product);
req.getRequestDispatcher("/WEB-INF/views/input.jsp").forward(req, res);
} else {
req.getRequestDispatcher("/WEB-INF/views/input.jsp").forward(req, res);
}
}
//更改或者插入操作
private void saveORupdate(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String id = req.getParameter("id");
System.out.println("当前id:"+id);
String productName = req.getParameter("productName");
String dirId = req.getParameter("dirId");
String salePrice = req.getParameter("salePrice");
String supplier = req.getParameter("supplier");
String brand = req.getParameter("brand");
String cutoff = req.getParameter("cutoff");
String costPrice = req.getParameter("costPrice");
Product p = new Product();
p.setProductName(productName);
p.setBrand(brand);
p.setSupplier(supplier);
if (hasLength(dirId)) {
p.setDirId(Long.valueOf(dirId));
}
if (hasLength(salePrice)) {
p.setSalePrice(new BigDecimal(salePrice));
}
if (hasLength(cutoff)) {
p.setCutoff(new BigDecimal(cutoff));
}
if (hasLength(costPrice)) {
p.setCostPrice(new BigDecimal(costPrice));
}
//根据id执行不同操作
if (hasLength(id)) {
p.setId(Long.valueOf(id));
System.out.println("执行更新操作");
System.out.println(p);
service.update(p);
} else {
System.out.println("执行插入操作");
System.out.println(p);
service.save(p);
}
res.sendRedirect("/product");
}
//删除
private void delete(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
Long id = Long.valueOf(req.getParameter("id"));
service.delete(id);
res.sendRedirect("/product");
}
private boolean hasLength(String str) {
return str != null && !"".equals(str.trim());
}
}
LogoutServlet.java
@WebServlet(name = "logout", urlPatterns = "/logout")
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//销毁session
req.getSession().invalidate();
//重定向到登陆界面
res.sendRedirect("login.jsp");
}
}
源码