1、@Controller
package com.csdn.mymvc.annotation;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Controller {
}
2、@Service
package com.csdn.mymvc.annotation;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Service {
}
3、@Repository
package com.csdn.mymvc.annotation;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Repository {
}
4、@Autowire
package com.csdn.mymvc.annotation;
import java.lang.annotation.*;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Autowire {
}
1、FruitController
package com.csdn.fruit.servlet;
import com.csdn.fruit.dto.PageInfo;
import com.csdn.fruit.dto.PageQueryParam;
import com.csdn.fruit.dto.Result;
import com.csdn.fruit.pojo.Fruit;
import com.csdn.fruit.service.FruitService;
import com.csdn.fruit.util.RequestUtil;
import com.csdn.fruit.util.ResponseUtil;
import com.csdn.mymvc.annotation.*;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
@RequestMapping("/fruit")
public class FruitController {
@Autowire
private FruitService fruitService;
@GetMapping("/index")
protected void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Integer pageNo = 1;
String pageNoStr = req.getParameter("pageNo");
if (pageNoStr != null && !"".equals(pageNoStr)) {
pageNo = Integer.parseInt(pageNoStr);
}
String keyword = "";
String keywordStr = req.getParameter("keyword");
if (keywordStr != null) {
keyword = keywordStr;
}
PageQueryParam pageQueryParam = new PageQueryParam(pageNo, 5, keyword);
PageInfo<Fruit> pageInfo = fruitService.getFruitPageInfo(pageQueryParam);
Result result = Result.OK(pageInfo);
ResponseUtil.print(resp, result);
}
@PostMapping("/add")
protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Fruit fruit = (Fruit) RequestUtil.readObject(req, Fruit.class);
fruitService.addFruit(fruit);
ResponseUtil.print(resp, Result.OK());
}
@GetMapping("/del")
protected void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Integer fid = Integer.parseInt(req.getParameter("fid"));
fruitService.delFruit(fid);
ResponseUtil.print(resp, Result.OK());
}
@GetMapping("/edit")
protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Integer fid = Integer.parseInt(req.getParameter("fid"));
Fruit fruit = fruitService.getFruitById(fid);
ResponseUtil.print(resp, Result.OK(fruit));
}
@GetMapping("/getFname")
public void getFname(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String fname = req.getParameter("fname");
Fruit fruit = fruitService.getFruitByFname(fname);
ResponseUtil.print(resp, fruit == null ? Result.OK() : Result.Fail());
}
@PostMapping("/update")
protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Fruit fruit = (Fruit) RequestUtil.readObject(req, Fruit.class);
fruitService.updateFruit(fruit);
ResponseUtil.print(resp, Result.OK());
}
}
2、FruitServiceImpl
package com.csdn.fruit.service.impl;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.dto.PageInfo;
import com.csdn.fruit.dto.PageQueryParam;
import com.csdn.fruit.pojo.Fruit;
import com.csdn.fruit.service.FruitService;
import com.csdn.mymvc.annotation.Autowire;
import com.csdn.mymvc.annotation.Service;
import java.util.List;
@Service
public class FruitServiceImpl implements FruitService {
@Autowire
private FruitDao fruitDao ;
@Override
public PageInfo<Fruit> getFruitPageInfo(PageQueryParam pageQueryParam) {
Integer pageNo = pageQueryParam.getPageNo();
Integer pageSize = pageQueryParam.getPageSize();
String keyword = pageQueryParam.getKeyword();
List<Fruit> fruitList = fruitDao.getFruitList(pageNo, pageSize, keyword);
Integer recordCount = fruitDao.getRecordCount(pageQueryParam.getKeyword());
PageInfo<Fruit> fruitPageInfo = new PageInfo<>(fruitList, pageNo, recordCount);
return fruitPageInfo;
}
@Override
public void addFruit(Fruit fruit) {
if (fruitDao.getFruitByFname(fruit.getFname()) == null) {
fruitDao.addFruit(fruit);
}
}
@Override
public void updateFruit(Fruit fruit) {
fruitDao.updateFruit(fruit);
}
@Override
public void delFruit(Integer fid) {
fruitDao.delFruitByFid(fid);
}
@Override
public Fruit getFruitById(Integer fid) {
return fruitDao.getFruitByFid(fid);
}
@Override
public Fruit getFruitByFname(String fname) {
return fruitDao.getFruitByFname(fname);
}
}
3、FruitDaoImpl
package com.csdn.fruit.dao.impl;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.pojo.Fruit;
import com.csdn.mymvc.annotation.Repository;
import com.csdn.mymvc.dao.BaseDao;
import java.util.List;
@Repository
public class FruitDaoImpl extends BaseDao<Fruit> implements FruitDao {
@Override
public void addFruit(Fruit fruit) {
String sql = "insert into t_fruit values (0,?,?,?,?)";
super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark());
}
@Override
public void delFruit(String fname) {
String sql = "delete from t_fruit where fname=?";
super.executeUpdate(sql, fname);
}
//通过 fid 删除水果库存记录
@Override
public void delFruitByFid(Integer fid) {
super.executeUpdate("delete from t_fruit where fid = ? ", fid);
}
//通过 fid 可以修改所有的属性值
@Override
public void updateFruit(Fruit fruit) {
String sql = "update t_fruit set fname=?,price=?,fcount=?,remark=? where fid = ?";
super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark(), fruit.getFid());
}
@Override
public List<Fruit> getFruitList() {
return super.executeQuery("select * from t_fruit");
}
@Override
public List<Fruit> getFruitList(Integer pageNo, Integer pageSize) {
return super.executeQuery("select * from t_fruit limit ?,?", (pageNo - 1) * pageSize, pageSize);
}
@Override
public List<Fruit> getFruitList(Integer pageNo, Integer pageSize, String keyword) {
return super.executeQuery("select * from t_fruit where fname like ? or remark like? limit ?,?", "%" + keyword + "%", "%" + keyword + "%", (pageNo - 1) * pageSize, pageSize);
}
@Override
public Fruit getFruitByFname(String fname) {
return load("select * from t_fruit where fname = ?", fname);
}
@Override
public Fruit getFruitByFid(Integer fid) {
return load("select * from t_fruit where fid=?", fid);
}
@Override
public Integer getRecordCount() {
String sql = "select count(*) from t_fruit";
return ((Long) executeComplexQuery(sql).get(0)[0]).intValue();
}
@Override
public Integer getRecordCount(String keyword) {
String sql = "select count(*) from t_fruit where fname like ? or remark like ? ";
return ((Long) executeComplexQuery(sql, "%" + keyword + "%", "%" + keyword + "%").get(0)[0]).intValue();
}
}