- MVC模式和三层架构">MVC模式和三层架构
- 案例练习
- 显示品牌名称、公司名称resultMap
MVC模式和三层架构
MVC模式
MVC分层开发模式
MVC是一种分层开发的模式,其中: :::info M:Model,业务模型,处理业务
V:View,视图,界面展示
C:Controller,控制器,处理请求,调用模型和视图 :::MVC好处
职责单一,互不影响
- 有利于分工协作
-
三层架构
三层架构(图文)
MVC模式和三层架构图
案例练习
准备环境
创建新的模块 brand_demo,引入坐标
- 创建三层架构的包结构
- 数据库表 tb_brand
- 实体类 Brand
MyBatis 基础环境 :::info Mybatis - config.xml
BrandMapper.xml
BrandMapper接口 :::建立新模块项目
pom引入需要坐标
```java
org.mybatis mybatis 3.5.5 mysql mysql-connector-java 5.1.36
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- jstl-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<a name="p0EUH"></a>
### pom引入tomcat插件
```java
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
创建三层架构包结构
:::info
com.taotao.web
com.taotao.service
com.taotao.mapper
com.taotao.pojo
com.taotao.util
:::
使用数据库
:::info
仍然使用mybatis数据库中的tb_brand表
:::
Brand实体类
package com.taotao.pojo;
/**
* create by 刘鸿涛
* 2022/3/29 16:19
*/
public class Brand {
private Integer id;
private String brand_name;
private String company_name;
private Integer ordered;
private String description;
private Integer status;
public Brand(){
}
public Brand(Integer id, String brand_name, String company_name, Integer ordered, String description, Integer status) {
this.id = id;
this.brand_name = brand_name;
this.company_name = company_name;
this.ordered = ordered;
this.description = description;
this.status = status;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBrand_name() {
return brand_name;
}
public void setBrand_name(String brand_name) {
this.brand_name = brand_name;
}
public String getCompany_name() {
return company_name;
}
public void setCompany_name(String company_name) {
this.company_name = company_name;
}
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 void setStatus(Integer status) {
this.status = status;
}
@Override
public String toString() {
return "Brand{" +
"id=" + id +
", brand_name='" + brand_name + '\'' +
", company_name='" + company_name + '\'' +
", ordered=" + ordered +
", description='" + description + '\'' +
", status=" + status +
'}';
}
}
xml配置文件
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 起别名-->
<typeAliases>
<package name="com.taotao.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis?useSSL=false&useServerPrepStmts=true"/>
<property name="username" value="root"/>
<property name="password" value="12345"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 扫描mapper-->
<package name="com/taotao/mapper"/>
</mappers>
</configuration>
创建映射文件夹
:::info resources 下的 com.taotao.mapper :::
创建映射文件
:::info BrandMapper.xml :::
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.taotao.mapper.BrandMapper">
<select id="selectAll" resultType="com.taotao.pojo.Brand">
select * from tb_brand
</select>
</mapper>
创建BrandMapper接口
:::info com.taotao.mapper.BrandMapper.java :::
package com.taotao.mapper;
/**
* create by 刘鸿涛
* 2022/3/29 16:12
*/
public interface BrandMapper {
}
完成“查询所有”功能
流程分析
package com.taotao.mapper;
import com.taotao.pojo.Brand;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* create by 刘鸿涛
* 2022/3/29 16:12
*/
public interface BrandMapper {
@Select("select * from tb_brand")
List<Brand> selectAll();
}
编写service
:::info 新建BrandService.java :::
package com.taotao.service;
import com.taotao.mapper.BrandMapper;
import com.taotao.pojo.Brand;
import com.taotao.util.SqlSessionfactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
/**
* create by 刘鸿涛
* 2022/3/29 16:59
*/
@SuppressWarnings({"all"})
public class BrandService {
/**
* 查询所有
* @return
*/
public List<Brand> selectAll(){
//调用BrandMapper.selectAll()
SqlSessionFactory factory = SqlSessionfactoryUtils.getSqlSessionFactory();
//2.获取sqlSession
SqlSession sqlSession = factory.openSession();
//3.获取BrandMapper
BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//4.调用方法
List<Brand> brands = mapper.selectAll();
//5.关闭资源
sqlSession.close();
return brands;
}
}
编写index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="/brand_demo/selectAllServlet">查询所有</a>
</body>
</html>
编写Servlet
package com.taotao.web;
import com.taotao.pojo.Brand;
import com.taotao.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;
/**
* create by 刘鸿涛
* 2022/3/29 17:31
*/
@WebServlet("/selectAllServlet")
public class selectAllServlet extends HttpServlet {
private BrandService service = new BrandService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.调用BrandService完成查询
List<Brand> brands = service.selectAll();
//2.存入request域中
req.setAttribute("brands",brands);
//3.转发到brand.jsp
req.getRequestDispatcher("/brand.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
编写JSP
<%--
Created by IntelliJ IDEA.
User: guigui
Date: 2022/3/29
Time: 10:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<input type="button" value="新增"><br>
<hr>
<table border="1" cellspacing="0" width="800">
<tr>
<th>序号</th>
<th>品牌名称</th>
<th>公司名称</th>
<th>价格</th>
<th>描述</th>
<th>状态</th>
</tr>
<c:forEach items="${brands}" var="brand" varStatus="status">
<tr align="center">
<td>${brand.id}</td>
<td>${brand.brand_name}</td>
<td>${brand.company_name}</td>
<td>${brand.ordered}</td>
<td>${brand.description}</td>
<c:if test="${brand.status == 1}">
<td>启用</td>
</c:if>
<c:if test="${brand.status != 1}">
<td>禁用</td>
</c:if>
</tr>
</c:forEach>
</table>
</body>
</html>
运行测试
:::info 因为数据库字段名与 实体类 字段名不同,所以没有显示brandName和companyName :::
显示品牌名称、公司名称resultMap
mybatis开发sql
编写BrandMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.taotao.mapper.BrandMapper">
<select id="selectAll" resultMap="brandResultMap">
select * from tb_brand
</select>
<resultMap id="brandResultMap" type="brand">
<result column="brandName" property="brand_name"></result>
<result column="companyName" property="company_name"></result>
</resultMap>
</mapper>
编写BrandMapper.java
:::info 为BrandMapper.java加上@ResultMap注解 :::
package com.taotao.mapper;
import com.taotao.pojo.Brand;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* create by 刘鸿涛
* 2022/3/29 16:12
*/
public interface BrandMapper {
// @Select("select * from tb_brand")
// @ResultMap("brandResultMap")
List<Brand> selectAll();
}
测试运行
注解开发SQL
编写BrandMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.taotao.mapper.BrandMapper">
<!-- <select id="selectAll" resultMap="brandResultMap">-->
<!-- select * from tb_brand-->
<!-- </select>-->
<resultMap id="brandResultMap" type="brand">
<result column="brandName" property="brand_name"></result>
<result column="companyName" property="company_name"></result>
</resultMap>
</mapper>
编写BrandMapper.java
package com.taotao.mapper;
import com.taotao.pojo.Brand;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* create by 刘鸿涛
* 2022/3/29 16:12
*/
public interface BrandMapper {
@Select("select * from tb_brand")
@ResultMap("brandResultMap")
List<Brand> selectAll();
}