intellij-idea java IDEA实现商品增删改查和保存 - //添加
打开一个新的工作空间,不能有其他项目。以图3为标准,创建xml和实体类用于存储数据,并初始化添加两条数据创建图1界面创建图2界面图1中,当点击添加时,将数据添加到xml中,并自己创建id属性,完成之后跳转到图3界面显示图2中,解析xml中,展示所有信息图2中,当点击修改按钮时,跳转到图3进行回显图3中,商品id不能被操作,当点击修改按钮时,去xml中修改数据,并跳转到图2界面图2中,当点击删除按钮时,删除数据图2中,当点击保存按钮时,将数据保存到D盘下的商品.txt文件中
创建一个Project新项目 选中Maven 1.create from archetype打勾,选中其中的maven-archetype-webapp
2.然后next,创建项目名称,路径,groupid也可以改,然后next
3.Maven home directory 选中apache-maven-3.6.3(Maven文件) user settings file 选中Maven文件中conf目录下settings的路径 local repository 选中Maven文件mvn目录下的repository路径
点击finish。 (1)添加依赖 :
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.hsd
Project_Test1
1.0-SNAPSHOT
war
Project_Test1 Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
jstl
jstl
1.2
org.mybatis
mybatis
3.5.5
taglibs
standard
1.1.2
mysql
mysql-connector-java
5.1.34
javax.servlet.jsp
jsp-api
2.2
javax.servlet
javax.servlet-api
3.1.0
com.fasterxml.jackson
jackson-datatype-json-org
1.8.0
junit
junit
4.11
test
Project_Test1
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
添加依赖 后,点击右侧Maven->Lifecycle->install自动下载所需的东西
(2)给main函数加两个新文件:
找到project Structure,位置在IDEA右上角倒数第三个图标(快捷键:ctrl+shift+alt+s) 到modules 在scr 目录下的main函数新加(new folder)java 和resources文件(java要点蓝色文件夹Sources, resources要点击Resources) 然后apply-->ok。
(3)将位于IDEA右上角运行符号旁的输入框add配置成Tomcat9.0.68: application server 选择Tomcat9.0.68 Deployment页面右下角点击fit 第二行 显示出新建的项目名 在最后一行application context 中把项目名后缀去掉 然后apply-->ok。
至此,成功创建项目 。
一、实现登录
1.创建Login类
package com.hsd.entity;
public class Login {
private String username;
private String password;
public Login() {
}
public Login(String username, String password) {
this.username = username;
this.password = password;
}
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;
}
@Override
public String toString() {
return "Login{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
2.创建login.jsp
<%--
Created by IntelliJ IDEA.
User: 30868
Date: 2022/10/26
Time: 14:54
To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
登录页面
欢迎登录
${login_msg}
${msg}
3.创建MyBatis-config.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
4.创建SqlSessionFactoryUtils类
package com.hsd.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class SqlSessionFactoryUtils {
//定义一个SqlSessionFactory类来存储数据
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//定义MyBatis-config.xml配置文件,加载MyBatis-config.xml配置文件
String resources="MyBatis-config.xml";
//通过输入流拿到MyBatis-config.xml配置文件
InputStream is= Resources.getResourceAsStream(resources);
//MyBatis通过读取配置文件信息,构造出sqlSessionFactory对象
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
}
5.创建LoginService类
package com.hsd.service;
import com.hsd.entity.Login;
import com.hsd.mapper.LoginMapper;
import com.hsd.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
public class LoginService {
//使用工具类获取SqlSessionFactoryUtils
SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
//登陆方法
public Login login(String username,String password)
{
//获取sqlSession
SqlSession sqlSession = factory.openSession();
//获取mapper
LoginMapper mapper = sqlSession.getMapper(LoginMapper.class);
Login login=mapper.select(username, password);
sqlSession.close();
return login;
}
}
6.创建LoginMapper类
package com.hsd.mapper;
import com.hsd.entity.Login;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
public interface LoginMapper {
@Select("select * from user2 where username=#{username} and password=#{password}")
Login select(@Param("username") String username,@Param("password") String password);
}
7.创建LoginServlet类
package com.hsd.controller;
import com.hsd.entity.Login;
import com.hsd.service.LoginService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//调用LoginService方法
LoginService loginService = new LoginService();
//获取页面的name值
String username=req.getParameter("username");
String password=req.getParameter("password");
//调用loginService登陆方法
Login login = loginService.login(username, password);
System.out.println(login);
//判断是否登录成功
if(login!=null)
{
//登陆成功
//将登陆的信息存到Session中
HttpSession session = req.getSession();
session.setAttribute("login",login);
resp.sendRedirect("SelectAllServlet");
}else{
//登陆失败
req.setAttribute("login_msg","用户名或密码有误");
req.getRequestDispatcher("/login.jsp").forward(req,resp);
}
}
}
至此,实现登录。
二、实现商品展示
1.创建Produce类 拼多多帮助力入口
package com.hsd.entity;
public class Produce {
private int id;
private String name;
private String price;
private String address;
private String sctime;
public Produce() {
}
public Produce(int id, String name, String price, String address, String sctime) {
this.id = id;
this.name = name;
this.price = price;
this.address = address;
this.sctime = sctime;
}
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 getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSctime() {
return sctime;
}
public void setSctime(String sctime) {
this.sctime = sctime;
}
@Override
public String toString() {
return "Produce{" +
"id=" + id +
", name='" + name + '\'' +
", price='" + price + '\'' +
", address='" + address + '\'' +
", sctime='" + sctime + '\'' +
'}';
}
}
2.数据库建表produce
CREATE TABLE produce
(
id INT PRIMARY KEY,
NAME VARCHAR(20),
price VARCHAR(20),
address VARCHAR(50),
sctime DATE
);
INSERT INTO produce(id,NAME,price,address,sctime)VALUES
(1,"哇哈哈","20元","北京","2018-02-02"),
(2,"哇哈哈","20元","北京","2018-02-02");
3.创建produce.jsp
<%--
Created by IntelliJ IDEA.
User: 30868
Date: 2022/10/26
Time: 16:19
To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
商品展示页面
<%--通过forEach循环获取域,items接受域对象,var:循环的变量--%>
商品id
商品名
单价
产地
生产日期
操作
${l.id}
${l.name}
${l.price}
${l.address}
${l.sctime}
1.创建SelectAllServlet类
package com.hsd.controller;
import com.hsd.entity.Produce;
import com.hsd.service.ProduceService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/SelectAllServlet")
public class SelectAllServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session=req.getSession();
//调用produceService对象
ProduceService produceService=new ProduceService();
//调用produceService完成查询
Listlist=produceService.selectAll();
//将拿到的数据存在域中
req.setAttribute("list",list);
//转发到produce.jsp页面
req.getRequestDispatcher("/produce.jsp").forward(req,resp);
}
}
2.创建ProduceService类
package com.hsd.service;
import com.hsd.entity.Produce;
import com.hsd.mapper.ProduceMapper;
import com.hsd.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class ProduceService {
//获取utils工具类
SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
public ListselectAll(){
//获取SqlSession,通过SqlSessionFactory创建SqlSession,然后Mybatis是通过SqlSession来操作数据库的
SqlSession sqlSession=factory.openSession();
//获取BrandMapper接口
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
//获取接口中查询的所有方法
Listproduces=mapper.selectAll();
//释放资源
sqlSession.close();
return produces;
}
//添加
public void add(Produce produce) {
SqlSession sqlSession=factory.openSession();
//获取BrandMapper接口
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
//获取接口中添加的所有方法
mapper.add(produce);
//提交事务
sqlSession.commit();
//释放
sqlSession.close();
}
//根据id修改
public void update(Produce produce) {
SqlSession sqlSession=factory.openSession();
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
mapper.update(produce);
sqlSession.commit();
sqlSession.close();
}
//根据id查询
public Produce selectById(int id) {
SqlSession sqlSession=factory.openSession();
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
Produce produce=mapper.selectById(id);
sqlSession.commit();
sqlSession.close();
return produce;
}
//根据id删除
public void deleteById(int id) {
SqlSession sqlSession=factory.openSession();
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
mapper.deleteById(id);
sqlSession.commit();
sqlSession.close();
}
}
3.创建ProduceMapper类
package com.hsd.mapper;
import com.hsd.entity.Produce;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface ProduceMapper {
//查询所有
@Select("select * from produce")
ListselectAll();
//添加
@Insert("insert into produce value(#{id},#{name},#{price},#{address},#{sctime})")
void add(Produce produce);
//根据id修改
@Update("update produce set id=#{id},name=#{name},price=#{price},address=#{address},sctime=#{sctime} where id=#{id}")
void update(Produce produce);
//根据id查询
@Select("select * from produce where id=#{id}")
Produce selectById(int id);
//删除
@Delete("delete from produce where id=#{id}")
void deleteById(int id);
}
至此,实现商品展示
三、实现商品添加
1.创建add.jsp
<%--
Created by IntelliJ IDEA.
User: 30868
Date: 2022/10/26
Time: 16:52
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
添加页面
2.创建AddServlet类
package com.hsd.controller;
import com.hsd.entity.Produce;
import com.hsd.service.ProduceService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取produceService对象
ProduceService produceService = new ProduceService();
//接收表单提交的数据
String id=req.getParameter("id");
String name=req.getParameter("name");
String price=req.getParameter("price");
String address=req.getParameter("address");
String sctime=req.getParameter("sctime");
//创建produce的对象
Produce produce = new Produce();
produce.setId(Integer.parseInt(id));
produce.setName(name);
produce.setPrice(price);
produce.setAddress(address);
produce.setSctime(sctime);
//调用produceService中的add方法
produceService.add(produce);
//转发到主页面
req.getRequestDispatcher("/SelectAllServlet").forward(req,resp);
}
}
3.ProduceService中添加add()方法
package com.hsd.service;
import com.hsd.entity.Produce;
import com.hsd.mapper.ProduceMapper;
import com.hsd.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class ProduceService {
//获取utils工具类
SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
public ListselectAll(){
//获取SqlSession,通过SqlSessionFactory创建SqlSession,然后Mybatis是通过SqlSession来操作数据库的
SqlSession sqlSession=factory.openSession();
//获取BrandMapper接口
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
//获取接口中查询的所有方法
Listproduces=mapper.selectAll();
//释放资源
sqlSession.close();
return produces;
}
//添加
public void add(Produce produce) {
SqlSession sqlSession=factory.openSession();
//获取BrandMapper接口
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
//获取接口中添加的所有方法
mapper.add(produce);
//提交事务
sqlSession.commit();
//释放
sqlSession.close();
}
//根据id修改
public void update(Produce produce) {
SqlSession sqlSession=factory.openSession();
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
mapper.update(produce);
sqlSession.commit();
sqlSession.close();
}
//根据id查询
public Produce selectById(int id) {
SqlSession sqlSession=factory.openSession();
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
Produce produce=mapper.selectById(id);
sqlSession.commit();
sqlSession.close();
return produce;
}
//根据id删除
public void deleteById(int id) {
SqlSession sqlSession=factory.openSession();
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
mapper.deleteById(id);
sqlSession.commit();
sqlSession.close();
}
}
4.ProduceMapper中添加add()方法
package com.hsd.mapper;
import com.hsd.entity.Produce;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface ProduceMapper {
//查询所有
@Select("select * from produce")
ListselectAll();
//添加
@Insert("insert into produce value(#{id},#{name},#{price},#{address},#{sctime})")
void add(Produce produce);
//根据id修改
@Update("update produce set id=#{id},name=#{name},price=#{price},address=#{address},sctime=#{sctime} where id=#{id}")
void update(Produce produce);
//根据id查询
@Select("select * from produce where id=#{id}")
Produce selectById(int id);
//删除
@Delete("delete from produce where id=#{id}")
void deleteById(int id);
}
至此,实现商品添加
四、实现商品修改(根据id修改)
1.创建update.jsp
<%--
Created by IntelliJ IDEA.
User: 30868
Date: 2022/10/26
Time: 17:11
To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
修改页面
<%----%>
2.创建updateServlet类
package com.hsd.controller;
import com.hsd.entity.Produce;
import com.hsd.service.ProduceService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/updateServlet")
public class updateServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取produceService对象
ProduceService produceService = new ProduceService();
//接收表单提交的数据
String id=req.getParameter("id");
String name=req.getParameter("name");
String price=req.getParameter("price");
String address=req.getParameter("address");
String sctime=req.getParameter("sctime");
//创建Brand的对象
Produce produce = new Produce();
produce.setId(Integer.parseInt(id));
produce.setName(name);
produce.setPrice(price);
produce.setAddress(address);
produce.setSctime(sctime);
//调用produceService的update方法
produceService.update(produce);
//转发到主页面
req.getRequestDispatcher("/SelectAllServlet").forward(req,resp);
}
}
3.ProduceService中添加update()方法
package com.hsd.service;
import com.hsd.entity.Produce;
import com.hsd.mapper.ProduceMapper;
import com.hsd.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class ProduceService {
//获取utils工具类
SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
public ListselectAll(){
//获取SqlSession,通过SqlSessionFactory创建SqlSession,然后Mybatis是通过SqlSession来操作数据库的
SqlSession sqlSession=factory.openSession();
//获取BrandMapper接口
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
//获取接口中查询的所有方法
Listproduces=mapper.selectAll();
//释放资源
sqlSession.close();
return produces;
}
//添加
public void add(Produce produce) {
SqlSession sqlSession=factory.openSession();
//获取BrandMapper接口
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
//获取接口中添加的所有方法
mapper.add(produce);
//提交事务
sqlSession.commit();
//释放
sqlSession.close();
}
//根据id修改
public void update(Produce produce) {
SqlSession sqlSession=factory.openSession();
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
mapper.update(produce);
sqlSession.commit();
sqlSession.close();
}
//根据id查询
public Produce selectById(int id) {
SqlSession sqlSession=factory.openSession();
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
Produce produce=mapper.selectById(id);
sqlSession.commit();
sqlSession.close();
return produce;
}
//根据id删除
public void deleteById(int id) {
SqlSession sqlSession=factory.openSession();
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
mapper.deleteById(id);
sqlSession.commit();
sqlSession.close();
}
}
4.ProduceMapper中添加update()方法
package com.hsd.mapper;
import com.hsd.entity.Produce;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface ProduceMapper {
//查询所有
@Select("select * from produce")
ListselectAll();
//添加
@Insert("insert into produce value(#{id},#{name},#{price},#{address},#{sctime})")
void add(Produce produce);
//根据id修改
@Update("update produce set id=#{id},name=#{name},price=#{price},address=#{address},sctime=#{sctime} where id=#{id}")
void update(Produce produce);
//根据id查询
@Select("select * from produce where id=#{id}")
Produce selectById(int id);
//删除
@Delete("delete from produce where id=#{id}")
void deleteById(int id);
}
5.创建selectByIdServlet类
package com.hsd.controller;
import com.hsd.entity.Produce;
import com.hsd.service.ProduceService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/selectByIdServlet")
public class selectByIdServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建produceService对象
ProduceService produceService = new ProduceService();
//接收id
String id=req.getParameter("id");
//调用produceService的selectById方法
Produce produce=produceService.selectById(Integer.parseInt(id));
//存到域中
req.setAttribute("p",produce);
//转发到update.jsp页面
req.getRequestDispatcher("/update.jsp").forward(req,resp);
}
}
6.ProduceService中添加selectById()方法
package com.hsd.service;
import com.hsd.entity.Produce;
import com.hsd.mapper.ProduceMapper;
import com.hsd.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class ProduceService {
//获取utils工具类
SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
public ListselectAll(){
//获取SqlSession,通过SqlSessionFactory创建SqlSession,然后Mybatis是通过SqlSession来操作数据库的
SqlSession sqlSession=factory.openSession();
//获取BrandMapper接口
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
//获取接口中查询的所有方法
Listproduces=mapper.selectAll();
//释放资源
sqlSession.close();
return produces;
}
//添加
public void add(Produce produce) {
SqlSession sqlSession=factory.openSession();
//获取BrandMapper接口
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
//获取接口中添加的所有方法
mapper.add(produce);
//提交事务
sqlSession.commit();
//释放
sqlSession.close();
}
//根据id修改
public void update(Produce produce) {
SqlSession sqlSession=factory.openSession();
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
mapper.update(produce);
sqlSession.commit();
sqlSession.close();
}
//根据id查询
public Produce selectById(int id) {
SqlSession sqlSession=factory.openSession();
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
Produce produce=mapper.selectById(id);
sqlSession.commit();
sqlSession.close();
return produce;
}
//根据id删除
public void deleteById(int id) {
SqlSession sqlSession=factory.openSession();
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
mapper.deleteById(id);
sqlSession.commit();
sqlSession.close();
}
}
7.ProduceMapper中添加selectById()方法
package com.hsd.mapper;
import com.hsd.entity.Produce;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface ProduceMapper {
//查询所有
@Select("select * from produce")
ListselectAll();
//添加
@Insert("insert into produce value(#{id},#{name},#{price},#{address},#{sctime})")
void add(Produce produce);
//根据id修改
@Update("update produce set id=#{id},name=#{name},price=#{price},address=#{address},sctime=#{sctime} where id=#{id}")
void update(Produce produce);
//根据id查询
@Select("select * from produce where id=#{id}")
Produce selectById(int id);
//删除
@Delete("delete from produce where id=#{id}")
void deleteById(int id);
}
至此,实现商品修改(根据id修改)
五、实现商品删除(根据id删除)
1.创建deleteByIdServlet类
package com.hsd.controller;
import com.hsd.service.ProduceService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/deleteByIdServlet")
public class deleteByIdServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建produceService对象
ProduceService produceService = new ProduceService();
//1. 接收id
String id=req.getParameter("id");
//2. 调用service查询
produceService.deleteById(Integer.parseInt(id));
//4. 转发到update.jsp
req.getRequestDispatcher("/SelectAllServlet").forward(req,resp);
}
}
2.ProduceService中添加deleteById()方法
package com.hsd.service;
import com.hsd.entity.Produce;
import com.hsd.mapper.ProduceMapper;
import com.hsd.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class ProduceService {
//获取utils工具类
SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
public ListselectAll(){
//获取SqlSession,通过SqlSessionFactory创建SqlSession,然后Mybatis是通过SqlSession来操作数据库的
SqlSession sqlSession=factory.openSession();
//获取BrandMapper接口
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
//获取接口中查询的所有方法
Listproduces=mapper.selectAll();
//释放资源
sqlSession.close();
return produces;
}
//添加
public void add(Produce produce) {
SqlSession sqlSession=factory.openSession();
//获取BrandMapper接口
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
//获取接口中添加的所有方法
mapper.add(produce);
//提交事务
sqlSession.commit();
//释放
sqlSession.close();
}
//根据id修改
public void update(Produce produce) {
SqlSession sqlSession=factory.openSession();
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
mapper.update(produce);
sqlSession.commit();
sqlSession.close();
}
//根据id查询
public Produce selectById(int id) {
SqlSession sqlSession=factory.openSession();
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
Produce produce=mapper.selectById(id);
sqlSession.commit();
sqlSession.close();
return produce;
}
//根据id删除
public void deleteById(int id) {
SqlSession sqlSession=factory.openSession();
ProduceMapper mapper=sqlSession.getMapper(ProduceMapper.class);
mapper.deleteById(id);
sqlSession.commit();
sqlSession.close();
}
}
3.ProduceMapper中添加deleteById()方法
package com.hsd.mapper;
import com.hsd.entity.Produce;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface ProduceMapper {
//查询所有
@Select("select * from produce")
ListselectAll();
//添加
@Insert("insert into produce value(#{id},#{name},#{price},#{address},#{sctime})")
void add(Produce produce);
//根据id修改
@Update("update produce set id=#{id},name=#{name},price=#{price},address=#{address},sctime=#{sctime} where id=#{id}")
void update(Produce produce);
//根据id查询
@Select("select * from produce where id=#{id}")
Produce selectById(int id);
//删除
@Delete("delete from produce where id=#{id}")
void deleteById(int id);
}
至此,实现商品删除(根据id删除)
六、实现将信息保存在D盘的a.txt中
创建Test01类
package com.hsd.service;
import com.hsd.entity.Produce;
import org.codehaus.jackson.map.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
@WebServlet("/Test01")
public class Test01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ProduceService produceService = new ProduceService();
String id=req.getParameter("id");
String name=req.getParameter("name");
String price=req.getParameter("price");
String address=req.getParameter("address");
String sctime=req.getParameter("sctime");
//将java对象转换为json字符串
//1.创建produce1对象
Produce produce1 = new Produce();
produce1.setId(Integer.parseInt(id));
produce1.setName(name);
produce1.setPrice(price);
produce1.setAddress(address);
produce1.setSctime(sctime);
Produce produce=produceService.selectById(Integer.parseInt(id));
req.setAttribute("p",produce);
req.getRequestDispatcher("/SelectAllServlet").forward(req,resp);
//2.创建jackson核心对象ObjectMapper
/*ObjectMapper常用的方法:
writeValueAsString:将对象转换为字符串
writeValue:将对象转换为字符串并写入到D:\\a.txt文件中
*/
//3.调用ObjectMapper的相关方法进行转换
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.writeValue(new File("D:\\a.txt"),produce);
}
}
七、实现未登录不可查看,添加及修改功能
package com.hsd.filter;
import com.hsd.entity.Login;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter(value = {"/SelectAllServlet","/add.jsp","/update.jsp","/produce.jsp"})
public class RightConfig implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//项目开始执行一次
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//向下转型
HttpServletRequest request=(HttpServletRequest) servletRequest;
HttpServletResponse response =(HttpServletResponse) servletResponse;
//获取session
HttpSession session = request.getSession();
//通过getAttribute获取user2
Login login=(Login) session.getAttribute("login");
if(login!=null)
{//释放拦截器
filterChain.doFilter(request,response);
}else{
request.setAttribute("msg","你还没有登陆");
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}
@Override
public void destroy() {
//项目运行执行一次
}
}
效果:
参考阅读