代码的业务逻辑图
基本的CURD、分页查询、条件查询、批量删除
前端:Vue+Ajax+Elememt-ui
后端:Web层(Servlet)+Service层+Dao层(持久层)+Mybatis
/*
Navicat Premium Data Transfer
Source Server : Mybatis
Source Server Type : MySQL
Source Server Version : 50528
Source Host : localhost:3306
Source Schema : test
Target Server Type : MySQL
Target Server Version : 50528
File Encoding : 65001
Date: 16/10/2022 09:32:18
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for brand
-- ----------------------------
DROP TABLE IF EXISTS `brand`;
CREATE TABLE `brand` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
`brand_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '品牌名',
`company_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '公司名',
`ordered` int(11) NOT NULL COMMENT '排序字段',
`description` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '描述信息',
`status` int(11) NOT NULL COMMENT '状态',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 74 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of brand
-- ----------------------------
INSERT INTO `brand` VALUES (1, '小潮Team', '上海潮晟文化传播有限公司', 2, '带给别人快乐是一件很酷的事情', 1);
INSERT INTO `brand` VALUES (2, '宠物星球', '凌飞科技有限公司', 1, '为理想打造梦幻家园', 1);
INSERT INTO `brand` VALUES (3, '小潮Team', '上海潮晟文化传播有限公司', 3, '带给别人快乐是一件很酷的事情', 1);
INSERT INTO `brand` VALUES (4, '小潮Team', '上海潮晟文化传播有限公司', 5, '带给别人快乐是一件很酷的事情', 1);
INSERT INTO `brand` VALUES (5, '小潮Team', '上海潮晟文化传播有限公司', 10, '带给别人快乐是一件很酷的事情', 1);
INSERT INTO `brand` VALUES (6, '宠物星球', '凌飞科技有限公司', 15, '为理想打造梦幻家园', 1);
INSERT INTO `brand` VALUES (7, '宠物星球', '凌飞科技有限公司', 18, '为理想打造梦幻家园', 1);
INSERT INTO `brand` VALUES (8, '小米', '小米科技有限公司', 102, '为发烧而生', 0);
INSERT INTO `brand` VALUES (9, '华为', '华为科技有限公司', 20, '就是割韭菜', 0);
INSERT INTO `brand` VALUES (10, '三只松鼠', '三只松鼠股份有限公司', 50, '吃吃吃', 1);
INSERT INTO `brand` VALUES (11, '京东', '北京京东世纪贸易有限公司', 34, '嗖嗖嗖', 1);
INSERT INTO `brand` VALUES (12, '腾讯', '腾讯计算机系统有限公司', 45, '就是圈钱', 1);
INSERT INTO `brand` VALUES (13, '百度', '百度在线网络技术公司', 10, '搜的就是快', 1);
INSERT INTO `brand` VALUES (14, 'aa', 'aaa', 23, 'aaa', 0);
INSERT INTO `brand` VALUES (15, 'bbb', 'bbb', 24, 'bbb', 1);
INSERT INTO `brand` VALUES (16, 'ccc', 'ccc', 25, 'ccc', 0);
INSERT INTO `brand` VALUES (17, 'ddd', 'ddd', 26, 'ddd', 1);
INSERT INTO `brand` VALUES (18, 'eee', 'eee', 27, 'eee', 1);
INSERT INTO `brand` VALUES (19, 'fff', 'fff', 28, 'fff', 1);
INSERT INTO `brand` VALUES (20, 'ggg', 'ggg', 29, 'ggg', 1);
INSERT INTO `brand` VALUES (21, 'hhh', 'hhh', 30, 'hhh', 0);
INSERT INTO `brand` VALUES (22, 'iii', 'iii', 31, 'iii', 1);
INSERT INTO `brand` VALUES (23, 'jjj', 'jjj', 32, 'jjj', 0);
INSERT INTO `brand` VALUES (24, 'kkk', 'kkk', 33, 'kkk', 1);
INSERT INTO `brand` VALUES (25, 'lll', 'lll', 34, 'lll', 1);
INSERT INTO `brand` VALUES (26, 'mmm', 'mmm', 35, 'mmm', 1);
INSERT INTO `brand` VALUES (27, 'nnn', 'nnn', 36, 'nnn', 1);
INSERT INTO `brand` VALUES (28, '111', '111', 37, '111', 0);
INSERT INTO `brand` VALUES (29, '222', '222', 38, '222', 0);
INSERT INTO `brand` VALUES (30, '333', '333', 39, '333', 0);
INSERT INTO `brand` VALUES (31, '444', '444', 40, '444', 0);
INSERT INTO `brand` VALUES (32, '555', '555', 41, '555', 0);
INSERT INTO `brand` VALUES (33, '666', '666', 42, '666', 1);
INSERT INTO `brand` VALUES (34, '777', '777', 43, '777', 0);
INSERT INTO `brand` VALUES (35, '888', '888', 44, '888', 1);
SET FOREIGN_KEY_CHECKS = 1;
链接:https://pan.baidu.com/s/1vGP-cTM5H2r90xssVB1EYA?pwd=0323 提取码:0323
首先,我们要先进行分析,要实现案例对品牌信息的操作,需要三层架构的相互调用和操作来实现。
Web层(表现层):表现层我们要实现通过对页面相应的操作,获取页面上的请求或者数据发送到Service层,然后通过Service层的一些方法将数据返回到Web层,进行显示
Service层(业务逻辑层):这里主要是进行方法的编写、数据的调度,将从前端拿到的数据或者请求传入到相应的方法体内,调用相应的方法实现相应的操作。
Dao层(数据访问层):主要创建pojo实体类,通过mapper映射的方式,获取数据库的数据。
这里用到的Element前端框架组件,可以参考:Element - 网站快速成型工具
package com.wfy.pojo;
public class Brand {
//id 主键
private Integer id;
//brandName 品牌名称
private String brandName;
//企业名称
private String companyName;
//排序字段
private Integer ordered;
//描述信息
private String description;
//状态: 0:禁用 1:启动
private Integer status;
private String statusStr;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Integer getOrdered() {
return ordered;
}
public void setOrdered(Integer ordered) {
this.ordered = ordered;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getStatus() {
return status;
}
//逻辑视图
public String getStatusStr(){
if(status==1){
return "启用";
}
return status==0 ?"禁用":"启用";
}
public void setStatus(Integer status) {
this.status = status;
}
@Override
public String toString() {
return "Brand{" +
"id=" + id +
", brandName='" + brandName + '\'' +
", companyName='" + companyName + '\'' +
", ordered=" + ordered +
", description='" + description + '\'' +
", status=" + status +
'}';
}
}
package com.wfy.pojo;
import java.util.List;
//分页查询的JavaBean
public class PageBean
//总记录数
private int totalCount;
//当前业数据
private List
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public List
return rows;
}
public void setRows(List
this.rows = rows;
}
}
package com.wfy.mapper;
import com.wfy.pojo.Brand;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface BrandMapper {
//查询所有
@Select("select * from brand")
@ResultMap("brandResultMap")
List
//新增品牌
@Insert("insert into brand values (null,#{brandName},#{companyName},#{ordered},#{description},#{status})")
void AddBrand(Brand brand);
//根据id来获取指定品牌信息,实现数据的回显操作
@Select("select * from brand where id = #{id}")
@ResultMap("brandResultMap")
Brand SelectById(int id);
//更改品牌信息
@Update("update brand set brand_name=#{brandName},company_name=#{companyName},ordered=#{ordered},description=#{description} where id=#{id} ")
void UpdateBrand(Brand brand);
//删除指定品牌数据
@Delete("delete from brand where id =#{id}")
void deleteById(int id);
//删除多条数据
void DeleteByIds( @Param("ids") int[] ids);
//分页查询 begin--开始的索引 size--获取信息的条目数
@Select("select * from brand limit #{begin},#{size} ")
@ResultMap("brandResultMap")
List
//查询总信息条数
@Select("select count(*) from brand")
int SelectTotalCount();
//分页条件查询 begin--开始的索引 size--获取信息的条目数
List
//条件查询总信息条数
int SelectTotalCountByCondition(Brand brand);
}
注意这里的映射文件需要在resource目录下创建分层目录,通过‘\’进行分割
及文件的目录名为:“ com\wfy\mapper”
因为我们需要不断的调用SqlSession的方法,通过mapper映像的方式,为了降低程序的耦合度,同时为了简化代码,通过接口的方式来实现对方法的调用
package com.wfy.service;
import com.wfy.pojo.Brand;
import com.wfy.pojo.PageBean;
import java.util.List;
public interface BrandService {
//查询所有数据
public List
//新增数据
void AddBrand(Brand brand);
//更改数据信息
//获取查询到的数据进行表格显现
Brand SelectById(int id);
void UpdateBrand(Brand brand);
//删除指定数据
void DeleteById(int id);
//删除多条数据
void DeleteByIds(int[] ids);
//分页查询 currentPage--当前页码 pageSize--每页展示条数
PageBean
//分页条件查询
PageBean
}
因为需要不断的调用SqlSessionFactoy工厂,为了简化代码,创建Util包,编写一个SqlSessionFactoryUtils来简化代码逻辑
package com.wfy.util;
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 {
private static SqlSessionFactory sqlSessionFactory;
static {
//静态代码块会随着类的加载而自动执行,且只执行一次
try {
String resource = "mybatis-config.xml";
InputStream inputStream = null;
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static SqlSessionFactory getSqlSessionFactory(){
return sqlSessionFactory;
}
}
package com.wfy.service.impl;
import com.wfy.mapper.BrandMapper;
import com.wfy.pojo.Brand;
import com.wfy.pojo.PageBean;
import com.wfy.service.BrandService;
import com.wfy.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class BrandServiceImpl implements BrandService {
//1.创建sqlSessionFactory 工厂对象
SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
public List
//2.获取SqlSession对象
SqlSession sqlSession = factory.openSession();
//3.获取BrandMapper
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//4.调用方法
List
//5.释放资源
sqlSession.close();
return brands;
}
public void AddBrand(Brand brand) {
SqlSession sqlSession = factory.openSession();
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
mapper.AddBrand(brand);
sqlSession.commit();
sqlSession.close();
}
public Brand SelectById(int id) {
//2.获取SqlSession对象
SqlSession sqlSession = factory.openSession();
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
Brand brand = mapper.SelectById(id);
//关闭资源
sqlSession.close();
return brand;
}
public void UpdateBrand(Brand brand) {
SqlSession sqlSession = factory.openSession();
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
mapper.UpdateBrand(brand);
//提交事务、关闭资源
sqlSession.commit();
sqlSession.close();
}
public void DeleteById(int id) {
SqlSession sqlSession = factory.openSession();
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
mapper.deleteById(id);
sqlSession.commit();
sqlSession.close();
}
public void DeleteByIds(int[] ids) {
//2.获取SqlSession对象
SqlSession sqlSession = factory.openSession();
//3.获取BrandMapper
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//4.调用方法
mapper.DeleteByIds(ids);
sqlSession.commit();
sqlSession.close();
}
public PageBean
//2.获取SqlSession对象
SqlSession sqlSession = factory.openSession();
//3.获取BrandMapper
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//4.计算开始索引=(当前页码数-1)*展示的信息条数
int begin=(currentPage-1) * pageSize;
//计算条目数
int size=pageSize;
//5.查询当前页数据
List
//6.查询总记录数
int totalCount = mapper.SelectTotalCount();
//7.封装PageBean对象
PageBean
pageBean.setRows(rows);
pageBean.setTotalCount(totalCount);
//8.释放资源
sqlSession.close();
return pageBean;
}
public PageBean
//2.获取SqlSession对象
SqlSession sqlSession = factory.openSession();
//3.获取BrandMapper
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//4.计算开始索引=(当前页码数-1)*展示的信息条数
int begin=(currentPage-1) * pageSize;
//计算条目数
int size=pageSize;
//处理brand条件,设置模糊表达式
String brandName = brand.getBrandName();
if (brandName !=null&&brandName.length()>0){
brand.setBrandName("%"+brandName+"%");
}
String companyName = brand.getCompanyName();
if (companyName !=null&&companyName.length()>0){
brand.setCompanyName("%"+companyName+"%");
}
//5.查询当前页数据
List
//6.查询总记录数
int totalCount = mapper.SelectTotalCountByCondition(brand);
//7.封装PageBean对象
PageBean
pageBean.setRows(rows);
pageBean.setTotalCount(totalCount);
//8.释放资源
sqlSession.close();
return pageBean;
}
}
如果为了实现不同的方法,我们需要不断地调用HttpServlet,因此通过编写一个BaseServlet来代替HttpServlet
package com.wfy.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//替换HttpServlet ,根据请求的最后一段路径来进行方法分发
public class BaseServlet extends HttpServlet {
//根据请求的最后一段路径来进行方法的分发
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取请求路径
String uri = req.getRequestURI(); //BrandTest/brand/SelectAllServlet
// System.out.println(uri);
//2.获取最后一段路径----方法名
int index=uri.lastIndexOf('/');
String methodName = uri.substring(index+1);
//System.out.println(methodName);
//3.执行方法
//3-1获取BrandServlet的字节码对象 class
// this:指代关系 谁调用我(this 所在的方法),我(this)代表谁
// System.out.println(this);//com.wfy.servlet.BrandServlet@61f761f5
Class extends BaseServlet> cls = this.getClass();
//3-2.获取方法的Method对象
try {
Method method = cls.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
//3-3执行方法
method.invoke(this,req,resp);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
package com.wfy.servlet;
import com.alibaba.fastjson.JSON;
import com.wfy.pojo.Brand;
import com.wfy.pojo.PageBean;
import com.wfy.service.BrandService;
import com.wfy.service.impl.BrandServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet{
private BrandService service = new BrandServiceImpl();
//获取全部信息
public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.调用service查询
List
//2.将数据转换为JSON
String toJSONString = JSON.toJSONString(brands);
//3.写数据
response.setContentType("text/json;charset=utf-8");
response.getWriter().write(toJSONString);
}
//新增信息
public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//1.接收品牌的数据
BufferedReader reader = request.getReader();
String params = reader.readLine();//json字符串
Brand brand = JSON.parseObject(params, Brand.class);
//2.调用service
service.AddBrand(brand);
//3.响应成功标识
response.getWriter().write("success");
}
//更改指定数据信息
//1-根据id查找指定数据,进行数据的回显操作
public void selectById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.接收从web层传输过来的id
BufferedReader reader = request.getReader();
String params = reader.readLine();
Integer id = JSON.parseObject(params, int.class);
Brand brand = service.SelectById(new Integer(id).intValue());
//2.将数据转化为JSON
String toJSONString = JSON.toJSONString(brand);
//3.写入数据
response.setContentType("text/json;charset=utf-8");
response.getWriter().write(toJSONString);
}
//2-更改品牌的信息
public void updateBrand(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.接收web层传输过来的数据
BufferedReader reader = request.getReader();
String params = reader.readLine();
Brand brand = JSON.parseObject(params, Brand.class);
//将数据进行回显操作
// System.out.println(brand);
service.UpdateBrand(brand);
//3.响应成功标识
response.getWriter().write("success");
}
//删除指定数据
public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//1.接收用户传递的id
BufferedReader reader = request.getReader();
String params = reader.readLine();
//将JSON数据转换为java数据
Integer id = JSON.parseObject(params, int.class);
service.DeleteById(new Integer(id).intValue());
//响应成功标识
response.getWriter().write("success");
}
//删除多条数据
public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//1.接收需要删除品牌数据的ids
BufferedReader reader = request.getReader();
String params = reader.readLine();
// System.out.println(params);
//1-2将接收到的信息转换为int[]
int[] ids = JSON.parseObject(params, int[].class);
// for(int id:ids){
// System.out.println(id);
// }
//2.调用service
service.DeleteByIds(ids);
//3.响应成功标识
response.getWriter().write("success");
}
//分页查询
public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.接收参数 当前页码 和 每页展示的条数 在url后进行拼接 url?currentPage=1&pageSize=5
String _currentPage = request.getParameter("currentPage");
String _pageSize = request.getParameter("pageSize");
int currentPage = Integer.parseInt(_currentPage);
int pageSize = Integer.parseInt(_pageSize);
//2.调用service进行查询
PageBean
//3.转换成JSON对象
String jsonString = JSON.toJSONString(pageBean);
//4.将数据写入
response.setContentType("text/json;charset=utf-8");
response.getWriter().write(jsonString);
}
//分页条件查询
public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.接收参数 当前页码 和 每页展示的条数 在url后进行拼接 url?currentPage=1&pageSize=5
String _currentPage = request.getParameter("currentPage");
String _pageSize = request.getParameter("pageSize");
int currentPage = Integer.parseInt(_currentPage);
int pageSize = Integer.parseInt(_pageSize);
//获取条件查询的对象
BufferedReader reader = request.getReader();
String params = reader.readLine();
Brand brand = JSON.parseObject(params, Brand.class);
//2.调用service进行查询
PageBean
//3.转换成JSON对象
String jsonString = JSON.toJSONString(pageBean);
//4.将数据写入
response.setContentType("text/json;charset=utf-8");
response.getWriter().write(jsonString);
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章