MVC模式图解:

三层架构图解:

表现层、业务逻辑层、数据访问层互不干扰,也就是说比如我们表现层更改代码不会干涉到其他两层

(com.itheima.web 这代表的是包名 也就是说最好把表现层或者业务逻辑层放到此对应的包名下)

以后会用三大框架封装表现层、业务逻辑层、数据访问层 (对应框架:SpringMVC、Spring、Mybatis)

MVC模式和三层架构图解:

也就是说我们学习的MVC模式对应的是三层架构的表现层 (相当于学三层架构的表现层)

运用以上三层架构思想完成案例:

准备环境:

1、创建模块brand-demo 引入用到的坐标 (brand-demo创建的是web模块 骨架模式):

坐标:

4.0.0org.examplebrand-demo1.0-SNAPSHOTwarorg.mybatismybatis3.5.5mysqlmysql-connector-java5.1.34javax.servletjavax.servlet-api3.1.0providedjavax.servlet.jspjsp-api2.2providedjstljstl1.2<!--jstl中的标准标签库 ( 就是标签库等. )-->taglibsstandard1.1.2org.apache.tomcat.maventomcat7-maven-plugin2.2<!--8084 换端口用 -->

注意别忘记把brand-demo新项目拉到服务器中:

2、创建三层架构的包结构:

3、数据库表准备tb_brand:

create table tb_brand(-- id 主键idint primary key auto_increment,-- 品牌名称brand_name varchar(20),-- 企业名称company_name varchar(20),-- 排序字段ordered int,-- 描述信息description varchar(100),-- 状态: 0:禁用 1:启动statusint); insert into tb_brand (brand_name, company_name, ordered, description, status)values('三只松鼠','三只松鼠股份有限公司',5,'好吃不上火',0),('华为','华为技术有限公司',100,'华为致于把数字世界带入每个人、每个家庭、每个组织、构建万物互联的智能世界',1),('小米','小米科技有限公司',50,'are you ok',1);

4、实体类Brand:复制mybatis增删改查笔记即可

5、mybatis基础环境配置: 看mybatis笔记 去官方复制即可 mybatis-config.xml

重要细节:注意搞完之后检查以下配置文件的路径是否都正确、是否都能对应住

接下来开始运用三层架构的思想完成增删改查操作:

以下是以查询所有商品为例, 其他的增删改的逻辑和查询所有的逻辑是一样的:

index.jsp改成brand.jsp

一、查询所有:

(以下的过程是在index.html开始的,所以看笔记的时候最好也在index.html开始)

Dao/Mapper层:(数据访问层)

package com.itheima.mapper;import com.itheima.pojo.Brand;import org.apache.ibatis.annotations.ResultMap;import org.apache.ibatis.annotations.Select;import java.util.List;/** *代理开发接口 */public interface BrandMapper {/** *查询所有商品 */@Select("select * from tb_brand")@ResultMap("brandResultMap") // 数据库中的字段名和实体类的属性名有不一样的时候用这个注表 //明, 因为brandResultMap标签中处理过字段名和数据库名不一样的时候了List selectAll();}

service层:(业务逻辑层)

(这个业务逻辑层可以拿到BrandMapper接口查询出来的所有商品数据 【只不过以前我们拿到数据后都是输出了 这里我们返回到方法当中了 因为我们需要在web表现层 进行把数据转发给jsp页面展示给用户的 不是输出在我们控制台的】)

package com.itheima.service;import com.itheima.mapper.BrandMapper;import com.itheima.pojo.Brand;import com.itheima.util.SqlSessionFactoryUtils;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import java.util.List;/** *业务逻辑层 */public class BrandService {/** *查询所有功能 * @return */public List selectAll(){// 利用工厂调用BrandMapper接口中的selectAll查询全部的方法SqlSessionFactory Factory =SqlSessionFactoryUtils.getSqlSessionFactory();SqlSession sqlSession =Factory.openSession();BrandMapper brandMapper =sqlSession.getMapper(BrandMapper.class);List brands =brandMapper.selectAll(); //// System.out.println(brands); 输出全部的商品// 这里我们返回即可,当调用这个方法的时候再输出return brands;}}

web层:(表现层)

把查询出来的所有的数据转发到brand.jsp页面当中然后展示给用户

jsp的作用就是:jsp页面运用EL表达式可以把查询到的所有商品展示给客户端浏览器上

package com.itheima.web;import com.itheima.pojo.Brand;import com.itheima.service.BrandService;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;import java.util.List;@WebServlet("/selectAllServlet")public class SelectAllServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 1 调用BrandService完成查询BrandService brandService =new BrandService();List brands =brandService.selectAll();// 2 把查询到的数据存入request域当中request.setAttribute("brands",brands);// 3 把存入的数据转发到brand.jsp页面中供用户查看数据request.getRequestDispatcher("/brand.jsp").forward(request,response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}}

index.html: (点击查询所有跳转到selectAllServlet路径下

Title 查询所有 

转发到的brand.jsp: (把数据展示给用户)

Title

序号品牌名称企业名称排序品牌介绍状态操作
${status.count}${brand.brandName}${brand.companyName}${brand.ordered}${brand.description} 启用 禁止 修改 删除
document.getElementById("add").onclick = function (){location.href = "/brand-demo/addBrand.jsp";}

开启服务器后,通过index.html进行访问路径下的资源如下:

总体的增删改查就是和上面的逻辑是一样的:

增删改查总代码如下:

Mapper层:(BrandMapper) 访问数据层

package com.itheima.mapper;import com.itheima.pojo.Brand;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.ResultMap;import org.apache.ibatis.annotations.Select;import java.util.List;/** *代理开发接口 */public interface BrandMapper {/** *1、查询所有商品 */@Select("select * from tb_brand")@ResultMap("brandResultMap")List selectAll();/** *2、添加商品 */void add(Brand brand);/** * 3、根据id查询 */@Select("select * from tb_brand where id = #{id}")@ResultMap("brandResultMap")Brand selectById(int id);/** * 4、修改商品 */void update(Brand brand);/** * 5、删除商品 (通过id删除) */@Delete("delete from tb_brand where id =#{id}")void delete(int id);}

BrandMapper.xml文件:

insert into tb_brand(brand_name, company_name, ordered, description, status) values(#{brandName},#{companyName},#{ordered},#{description},#{status});update tb_brand set brand_name =#{brandName},company_name=#{companyName},ordered=#{ordered},description=#{description},status=#{status} where id =#{id};

Service层:业务逻辑层 (Brandservice)

package com.itheima.service;import com.itheima.mapper.BrandMapper;import com.itheima.pojo.Brand;import com.itheima.util.SqlSessionFactoryUtils;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import java.util.List;/** *业务逻辑层 */public class BrandService {/** *查询所有功能 * @return */public List selectAll(){// 利用工厂调用BrandMapper接口中的selectAll查询全部的方法SqlSessionFactory Factory =SqlSessionFactoryUtils.getSqlSessionFactory();SqlSession sqlSession =Factory.openSession();BrandMapper brandMapper =sqlSession.getMapper(BrandMapper.class);List brands =brandMapper.selectAll();// System.out.println(brands); 输出全部的商品// 这里我们返回即可,当调用这个方法的时候再输出// 释放资源sqlSession.close();return brands;// 释放资源}/** *添加商品的功能 *注意小细节:所有的增删改都涉及到事务,idea会默认帮我们回滚事务 因此我们需要手动提交事务 */public void add(Brand brand){// 利用工厂调用BrandMapper接口中的selectAll查询全部的方法SqlSessionFactory Factory =SqlSessionFactoryUtils.getSqlSessionFactory();SqlSession sqlSession =Factory.openSession();BrandMapper brandMapper =sqlSession.getMapper(BrandMapper.class);brandMapper.add(brand);// 提交事务sqlSession.commit();// 释放资源sqlSession.close();}/** * 根据id查询商品数据 */public Brand selectById(int id){// 利用工厂调用BrandMapper接口中的selectAll查询全部的方法SqlSessionFactory Factory =SqlSessionFactoryUtils.getSqlSessionFactory();SqlSession sqlSession =Factory.openSession();BrandMapper brandMapper =sqlSession.getMapper(BrandMapper.class);Brand brand =brandMapper.selectById(id);// 释放资源sqlSession.close();return brand;}/** * 修改商品 */public void update(Brand brand){SqlSessionFactory Factory =SqlSessionFactoryUtils.getSqlSessionFactory();SqlSession sqlSession =Factory.openSession();BrandMapper brandMapper =sqlSession.getMapper(BrandMapper.class);brandMapper.update(brand);// 提交事务sqlSession.commit();// 释放资源sqlSession.close();}/** *删除商品 *(先通过id查找出想要删除的整条SQL数据) */public void deleteById(int id){SqlSessionFactory Factory =SqlSessionFactoryUtils.getSqlSessionFactory();SqlSession sqlSession =Factory.openSession();BrandMapper brandMapper =sqlSession.getMapper(BrandMapper.class);brandMapper.delete(id);// 提交事务sqlSession.commit();// 释放资源sqlSession.close();}}

web层:表现层

AddServlet:

package com.itheima.web;import com.itheima.pojo.Brand;import com.itheima.service.BrandService;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 {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 解决Post请求乱码问题request.setCharacterEncoding("utf-8");// 1. 先获取客户端addBrand.jsp传输过来的请求数据 (数据以key values的形式存储在request域当中)String brandName =request.getParameter("brandName");String companyName =request.getParameter("companyName");String ordered =request.getParameter("ordered");String description =request.getParameter("description");String status =request.getParameter("status");// 2. 把获取的客户端的请求数据 封装到Brand对象属性中 进行往数据库当中添加数据Brand brand =new Brand();brand.setBrandName(brandName);brand.setCompanyName(companyName);brand.setOrdered(Integer.parseInt(ordered)); // 因为对象属性中该属性是Integer类型 客户的数据为String类型brand.setDescription(description);brand.setStatus(Integer.parseInt(status));// 3. 调用BrandService业务逻辑层中的增加商品的方法BrandService brandService =new BrandService();brandService.add(brand);// 4. 转发到查询所有商品的路径(把添加过后的全部商品再查询一下展示给客户端)request.getRequestDispatcher("/selectAllServlet").forward(request,response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}}

DeleteById:

package com.itheima.web;import com.itheima.service.BrandService;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("/deleteById")public class DeleteById extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 1. 接收传送过来的idString id =request.getParameter("id");// 2. 调用业务逻辑层 传递id参数BrandService brandService =new BrandService();brandService.deleteById(Integer.parseInt(id));// 3. 删除成功后 转发到查询所有商品的路径下 展示给用户所有商品request.getRequestDispatcher("/selectAllServlet").forward(request,response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}}

SelectAllServlet:

package com.itheima.web;import com.itheima.pojo.Brand;import com.itheima.service.BrandService;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;import java.util.List;@WebServlet("/selectAllServlet")public class SelectAllServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 1 调用BrandService完成查询BrandService brandService =new BrandService();List brands =brandService.selectAll();// 2 把查询到的数据存入request域当中request.setAttribute("brands",brands);// 3 把存入的数据转发到brand.jsp页面中供用户查看数据request.getRequestDispatcher("/brand.jsp").forward(request,response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}}

SelectByIdServlet:(这个是修改回显的操作,先是通过在brand.jsp页面上点击修改按钮(附带着id) 然后通过超链接进入selectByIdServlet路径资源下、把该id所查询出来的信息展示回显给用户 然后供用户进行修改的操作 最后再转发到查询所有的商品 把修改后的所有商品展示给用户)

package com.itheima.web;import com.itheima.pojo.Brand;import com.itheima.service.BrandService;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 {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 1. 接收传送过来的idString id =request.getParameter("id");// 2. 调用业务逻辑层进行通过id查询BrandService brandService =new BrandService();Brand brand =brandService.selectById(Integer.parseInt(id));// SQL查询的时候id是int型这里接收的是String 需要转换// 3. 通过id查询的数据储存到request域当中request.setAttribute("brand",brand);// 4. 把通过id查询出来的商品数据回显给客户端进行修改操作// (就是把查询出来的数据通过转发给jsp页面 然后通过做的页面让客户端直接在我们做的页面当中进行修改数据)request.getRequestDispatcher("/update.jsp").forward(request,response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}}

UpdateServlet:

package com.itheima.web;import com.itheima.pojo.Brand;import com.itheima.service.BrandService;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 {Brand brand =new Brand();BrandService brandService =new BrandService();@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 处理post乱码问题request.setCharacterEncoding("utf-8");// 1. 接收用户修改传到request当中的数据String id =request.getParameter("id");String brandName =request.getParameter("brandName");String companyName =request.getParameter("companyName");String ordered =request.getParameter("ordered");String description =request.getParameter("description");String status =request.getParameter("status");// 2. 把接收的数据封装到Brand对象属性当中brand.setId(Integer.parseInt(id));brand.setBrandName(brandName);brand.setCompanyName(companyName);brand.setOrdered(Integer.parseInt(ordered));brand.setDescription(description);brand.setStatus(Integer.parseInt(status));// 3. 调用业务逻辑层中的update方法 进行传送封装到Brand对象属性中的客户端数据 进行SQL语句操作brandService.update(brand);// 4. 修改成功后 转发到查询所有的selectAllServlet路径下把所有的商品再展示给用户request.getRequestDispatcher("/selectAllServlet").forward(request,response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}}

index.html:

Title 查询所有 

brand.jsp:

Title

序号品牌名称企业名称排序品牌介绍状态操作
${status.count}${brand.brandName}${brand.companyName}${brand.ordered}${brand.description} 启用 禁止 修改删除
document.getElementById("add").onclick = function (){location.href = "/brand-demo/addBrand.jsp";}

addBrand.jsp:

添加品牌

添加品牌

品牌名称:
企业名称:
排序:
描述信息:
状态:禁用启用

update.jsp:

修改品牌

修改品牌

品牌名称:
企业名称:
排序:
描述信息:
状态:禁用启用
禁用启用

解决如下这样的问题(jsp页面上不显示相对应的数据 而显示代码问题):

开启服务器:访问index.html页面 演示增删改查操作:

添加:

修改:

删除:

点击修改后即可 (通过id删除的)