SpringMVC 和 mybatis 整合之查询(二)

一、需求

使用 springmvc 和 mybatis 完成商品列表查询。

项目结构:




二、整合 mybatis

2.1 sqlMapConfig.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="lxf.po"/>
</typeAliases>

<!-- 配置加载 mappers 
由于使用 spring 和 mybatis 整合使用 MapperScannerConfigurer 扫描;
所以这里就不需要配置 mappers 了。 不过扫描的mapper.xml 和 mapper.java 需同名且在同一目录下。
-->
</configuration>
2.1 db.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/testMy**
jdbc.username=**
jdbc.password=**

2.3 applicationContext-dao.xml:



po 类和 mapper 接口类 可以用逆向工程生成,也可以自己创建。不过逆向工程生成的文件只是适用于 单表增删改查。

笔者这里的是逆向工程,可以参考下。

2.4 Products.java:
package lxf.po;

import java.util.Date;

public class Products {
    private Integer id;

    private String name;

    private Float price;

    private String pic;

    private Date createtime;

    private String detail;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic == null ? null : pic.trim();
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail == null ? null : detail.trim();
    }
}

逆向工程还会生成 ProductsExample.java ,ProductsMapper.java ,ProductsMapper.xml 文件,这篇文章的重点是 整合 mybatis ,所以 没用到的文件 就不一一列举了。


2.5 ProductsCustomMapper.xml:


上图的查询虽然 sql 语句很简单,但我们是为了模拟联合查询,这时候就必须使用 包装对象扩展对象了。


2.6 ProductsCustom.java:
package lxf.po;

//商品信息扩展类
public class ProductsCustom extends Products{
    
	//这里可以再 添加商品信息的扩展信息
}
2.7 ProductsQueryVo.java:
package lxf.po;

//商品包装对象(查询条件)
public class ProductsQueryVo {
   
	private ProductsCustom productsCustom;
	
	//这里可以添加 其他类 查询条件

	public ProductsCustom getProductsCustom() {
		return productsCustom;
	}

	public void setProductsCustom(ProductsCustom productsCustom) {
		this.productsCustom = productsCustom;
	}
	
}

三、整合 Service 层:

3.1 ProductsService.java:


3.2 ProductsServiceImpl.java:


3.3 applicationContext-service.xml:



3.4 事务管理:

applicationContext-transaction.xml:



四、整合 springMVC:

4.1 配置前端控制器

web.xml:



4.2 配置处理器映射器、适配器,视图解析器

springmvc.xml:



4.3 ProductsController.java:



4.4 productsList.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
</head>
<body> 
<%-- <form action="${pageContext.request.contextPath }/item/queryItem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table> --%>
商品列表:
<table width="100%" border=1>
<tr>
	<td>商品名称</td>
	<td>商品价格</td>
	<td>生产日期</td>
	<td>商品描述</td>
	<!-- <td>操作</td> -->
</tr>
<c:forEach items="${proList }" var="pro">
<tr>
	<td width="18%">${pro.name }</td>
	<td width="18%">${pro.price }</td>
	<td width="18%"><fmt:formatDate value="${pro.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
	<td>${pro.detail }</td>
	
	<%-- <td><a href="${pageContext.request.contextPath }/item/editItem.action?id=${item.id}">修改</a></td> --%>

</tr>
</c:forEach>

</table>
</form>
</body>

</html>


五、加载 spring 容器

5.1 web.xml:



六、测试

测试地址:http://localhost:8080/springMVCAndMybatis/queryProducts.action

结果:



猜你喜欢

转载自blog.csdn.net/qq_30715329/article/details/80404605
今日推荐