【SpringMVC整合MyBatis】商品查询工程框架配置

mybatis和spring进行整合,来编写一个商品查询的工程。

一.整合dao

1.sqlMapConfig.xml

mybatis自己的配置文件---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>

    <!-- 全局setting配置,根据需要添加 -->

    <!-- 配置别名 -->
    <typeAliases>
    	<!-- 批量扫描别名 -->
    	<package name="cn.edu.hpu.ssm.po"/>
    </typeAliases>

    <!-- 配置mapper
    由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。
    但必须遵循:mapper.xml和mapper.java文件同名且在一个目录-->
    <!-- <mappers></mappers> -->

</configuration>

2.applicationContext-dao.xml
spring的配置文件
配置:
数据源
SqlSessionFactory
mapper扫描器

applicationContext-dao.xml内容:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 配置数据源 ,dbcp -->

	<!-- 数据源,使用dbcp -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="30" />
		<property name="maxIdle" value="5" />
	</bean>

	<!-- sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 加载mybatis的全局配置文件 -->
		<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
	</bean>

	<!-- mapper扫描器 -->
	<!-- mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注入
	遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录中.
	自动扫描出来的mapper的bean的id为mapper类名(首字母小写)-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
		<property name="basePackage" value="cn.edu.hpu.ssm.mapper"></property>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>

</beans>

3.逆向工程生成po类及mapper(单表增删改查)

如图.逆向工程生成的文件

将生成的文件拷贝至工程中。

4.手动定义商品查询mapper

针对综合查询mapper,一般情况会有关联查询,建议自定义mapper

5.自定义mapper的映射配置文件ItemsMapperCustom.xml

sql语句:
SELECT * FROM items WHERE items.name LIKE '%洗衣机%'

ItemsMapperCustom.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="cn.edu.hpu.ssm.mapper.ItemsMapperCustom" >

  	<!-- 定义商品查询的sql片段,就是商品查询条件 -->
  	<sql id="query_items_where">
  	   <!-- 使用动态sql,通过if判断,满足条件进行sql拼接 -->
  	   <!-- 商品查询条件通过ItemsQueryVo包装对象中itemsCustom属性传递 -->
  	      <if test="itemsCustom!=null">
  	          <if test="itemsCustom.name!=null and itemsCustom.name!=''">
  	          		items.name LIKE '%${itemsCustom.name}%'
  	          </if>
  	      </if>

  	</sql>

  	<!-- 商品列表查询 -->
  	<!-- parameterType要传入包装对象,包装了查询条件 -->
  	<select id="findItemsList" parameterType="ItemsQueryVo"
  	resultType="cn.edu.hpu.ssm.po.ItemsCustom">
  		SELECT items.* FROM items
  		<where>
  		    <include refid="query_items_where"></include>
  		</where>
  	</select>

</mapper>

6.自定义mapper的映射接口文件ItemsMapperCustom.java

package cn.edu.hpu.ssm.mapper;

import java.util.List;

import cn.edu.hpu.ssm.po.ItemsCustom;
import cn.edu.hpu.ssm.po.ItemsQueryVo;

public interface ItemsMapperCustom {
    //商品查询列表
	public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}

二.整合service
让spring管理service接口。

1.定义service接口

package cn.edu.hpu.ssm.service.impl;

import java.util.List;

import cn.edu.hpu.ssm.po.ItemsCustom;
import cn.edu.hpu.ssm.po.ItemsQueryVo;

//商品管理service
public interface ItemsService {

	//商品查询列表
	public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}

2.接口的实现ItemsServiceImpl:

package cn.edu.hpu.ssm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.hpu.ssm.mapper.ItemsMapperCustom;
import cn.edu.hpu.ssm.po.ItemsCustom;
import cn.edu.hpu.ssm.po.ItemsQueryVo;
import cn.edu.hpu.ssm.service.ItemsService;

//商品管理
public class ItemsServiceImpl implements ItemsService{

	@Autowired
	private ItemsMapperCustom itemsMapperCustom;

	@Override
	public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)
			throws Exception {
		//通过ItemsMapperCustom查询数据库
		return itemsMapperCustom.findItemsList(itemsQueryVo);
	}

}

3.在spring容器配置service(applicationContext-service.xml)

创建applicationContext-service.xml,文件中配置service。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 商品管理的service -->
	<bean id="itemsService" class="cn.edu.hpu.ssm.service.impl.ItemsServiceImpl"></bean>

</beans>

4.事务控制(applicationContext-transaction.xml)
在applicationContext-transaction.xml中使用spring声明式事务控制方法。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 事务管理器
	     对mybatis操作数据库事务控制,spring使用jdbc的事务控制类
	-->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 数据源
		dataSource在applicationContext-dao.xml中配置了-->
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
		   <!-- 传播行为 -->
		   <tx:method name="save*" propagation="REQUIRED"/>
		   <tx:method name="delete*" propagation="REQUIRED"/>
		   <tx:method name="insert*" propagation="REQUIRED"/>
		   <tx:method name="update*" propagation="REQUIRED"/>
		   <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
		   <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
		   <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
		</tx:attributes>
	</tx:advice>

	<!-- aop -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.edu.hpu.ssm.service.impl.*.*(..))"/>
	</aop:config>

</beans>

5.整合springmvc
springmvc.xml
创建springmvc.xml文件,配置处理器映射器、适配器、视图解析器。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 可以扫描controller、service、...
	这里让扫描controller,指定controller的包-->
	<context:component-scan base-package="cn.edu.hpu.ssm.controller"></context:component-scan>

	<!-- 注解映射器 -->
	<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>	 -->
	<!-- 注解适配器 -->
	<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>	 -->

	<!-- 使用mvc:annotation-driven代替上边注解映射器和注解适配器配置
	mvc:annotation-driven默认加载很多的参数绑定方法,
	比如json转换解析器就默认加载了,如果使用mvc:annotation-driven就不用配置上边的
    RequestMappingHandlerMapping和RequestMappingHandlerAdapter
	实际开发时使用mvc:annotation-driven-->
	<mvc:annotation-driven></mvc:annotation-driven>

	<!-- 视图解析器
	解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包
	 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
	    <property name="suffix" value=".jsp"></property>
	</bean>
</beans>

6.配置前端控制器

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<!-- SpringMvc前端控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器,适配器等)
		如果不配置contextConfigLoaction,默认加载的是/WEB-INF/servlet名称-servlet.xml(springmvc-servlet.xml)
		-->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/springmvc.xml</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<!-- 第一种:*.action。访问以.action结尾由DispatcherServlet进行解析;
		 第二种:/,所有访问的地址都由DispatcherServlet进行解析,对于静态文件的解析,
		 我们要配置不让DispatcherServlet进行解析。使用此种方法可以实现RESTful风格的url;
		 第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面时,仍然会由
		 DispatcherServlet进行解析jsp地址,它不能根据jsp页面找到Handler,会报错-->
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

7.编写Controller(就是Handler)

package cn.edu.hpu.ssm.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.hpu.ssm.po.Items;
import cn.edu.hpu.ssm.po.ItemsCustom;
import cn.edu.hpu.ssm.service.ItemsService;

//商品的Controller
public class ItemsController {

	@Autowired
	private ItemsService itemsService;

	//商品查询列表
	//@RequestMapping实现 对queryItems方法和url进行映射,一个方法对应一个url
	//一般建议将url和方法写成一样
	@RequestMapping("/queryItems")
	public ModelAndView queryItems()throws Exception{

		//调用Service查找数据库,查询商品列表,这里使用静态数据模拟
		List<ItemsCustom> itemsList=itemsService.findItemsList(null);

		//返回ModelAndView
		ModelAndView modelAndView=new ModelAndView();
		//相当于request的setAttribut,在jsp页面中通过这个来取数据
		modelAndView.addObject("itemsList",itemsList);

		//指定视图
		//下边的路径,如果在视图解析器中配置jsp的路径前缀和后缀,修改为items/itemsList
		//modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp")
		//下边的路径配置就可以不在程序中指定jsp路径的前缀和后缀
		modelAndView.setViewName("items/itemsList");

		return modelAndView;
	}

}



8.编写jsp如图-2.b.jsp的目录.png

<%@ 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 }/items/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="${itemsList }" var="item">
<tr>
	<td>${item.name }</td>
	<td>${item.price }</td>
	<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
	<td>${item.detail }</td>

	<td><a href="${pageContext.request.contextPath }/items/editItem.action?id=${item.id}">修改</a></td>

</tr>
</c:forEach>

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

</html>

9.加载spring容器

将mapper、service、controller加载到spring容器中。

图application配置文件

建议使用通配符加载上边的配置文件。
在web.xml中,添加spring容器监听器,加载spring容器。

<!-- 加载spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

凡是符合/WEB-INF/classes/spring/applicationContext-*.xml规则的配置文件就自动加载了。

测试:
现将工程部署到tomcat中,启动tomcat,我们来测试
http://localhost:8080/springmvc_mybatis1507/queryItems.action

测试结果如图

实际的数据库表中的情况如图数据库情况

说明测试成功!

整个的整合和环境配置工作就到此结束了,接下来就是开发了。

在此基础上接下来的开发请看以后发布的总结文章。

转载请注明出处:http://blog.csdn.net/acmman/article/details/46997813

时间: 2024-08-02 03:09:24

【SpringMVC整合MyBatis】商品查询工程框架配置的相关文章

SpringMVC整合mybatis实例代码_java

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis . 一.逆向工程生成基础信息 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis G

【SpringMVC整合MyBatis】商品修改功能分析

结合之前我们搭建好的环境,我们下面来编写商品修改的功能. 商品修改功能开发 1.需求 操作流程: (1)进入商品查询列表页面 (2)点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询)要修改的商品从数据库查询,根据商品id(主键)查询商品信息 (3)在商品修改页面,修改商品信息,修改后,点击提交 2.开发mapper mapper:根据id查询商品信息根据id更新Items表的数据 不用开发了,使用逆向工程生成的代码. 3.开发service 接口功能:根据id查询商品信息修改商

【SpringMVC整合MyBatis】整合思路与工程结构

springmvc和mybatis整合 1.需求 使用springmvc和mybatis完成商品列表查询. 2.整合思路 springmvc+mybaits的系统架构: 如图 第一步:整合dao层mybatis和spring整合,通过spring管理mapper接口.使用mapper的扫描器自动扫描mapper接口在spring中进行注册. 第二步:整合service层通过spring管理 service接口.使用配置方式将service接口配置在spring配置文件中.实现事务控制. 第三步:

【SpringMVC整合MyBatis】数据回显

数据回显 1.什么数据回显 提交后,如果出现错误,将刚才提交的数据回显到刚才的提交页面. 2.pojo数据回显方法 2.1springmvc默认对pojo数据进行回显. pojo数据传入controller方法后,springmvc自动将pojo数据放到request域,key等于pojo类型(首字母小写) 说白了就是items类 public class Items { private Integer id; private String name; private Float price;

【SpringMVC整合MyBatis】提供学习参考的项目源码

最近很多博友私信向我索要此开发专栏的源代码,为了发挥开源精神,我决定将<MyBatis+SpringMVC>专栏所有源代码工程公布给大家,供大家学习参考! 共享的源代码分别是,MyBatis单独的工程,SpringMVC单独的工程,Spring与MyBatis整合的工程,SpringMVC与MyBatis整合的工程.另外感谢光临博客,祝大家学习愉快 源代码链接地址:http://download.csdn.net/detail/u013517797/9031455

【SpringMVC整合MyBatis】案例驱动-包装类型pojo参数绑定

包装类型pojo参数绑定 1.需求 商品查询controller方法中实现商品查询条件传入. 2.实现方法 第一种方法:在形参中 添加HttpServletRequest request参数,通过request接收查询条件参数. 第二种方法:在形参中让包装类型的pojo接收查询条件参数. 分析: 页面传参数的特点:复杂,多样性.条件包括:用户账号.商品编号.订单信息...... 如果将用户账号.商品编号.订单信息等放在简单pojo(属性是简单类型)中,pojo类属性比较多,比较乱. 建议使用包装

【SpringMVC整合MyBatis】RequestMapping注解与controller方法返回值

我们讲解一下之前用的@RequestMapping注解和controller方法返回值 一.@RequestMapping注解作用 1.url映射 定义controller方法对应的url,进行处理器映射使用. //商品查询列表 //@RequestMapping实现 对queryItems方法和url进行映射,一个方法对应一个url //一般建议将url和方法写成一样 @RequestMapping("/queryItems") public ModelAndView queryIt

【SpringMVC整合MyBatis】案例驱动-集合类型参数绑定

集合类型绑定 1.数组绑定 1.1需求 商品批量删除,用户在页面选择多个商品,批量删除. 1.2表现层实现 关键:将页面选择(多选)的商品id,传到controller方法的形参,方法形参使用数组接收页面请求的多个商品id. controller方法定义: //批量删除商品的信息 //批量删除商品的信息 @RequestMapping("/deleteItems") public String deleteItems(int[] items_id)throws Exception{ /

【SpringMVC整合MyBatis】validation校验-商品修改校验

springmvc校验 1.校验理解 项目中,通常使用较多是前端的校验,比如页面中js校验.对于安全要求较高点建议在服务端进行校验. 服务端校验:控制层controller:校验页面请求的参数的合法性.在服务端控制层controller校验,不区分客户端类型(浏览器.手机客户端.远程调用)业务层service(使用较多):主要校验关键业务参数,仅限于service接口中使用的参数.持久层dao:一般是不校验的. 2.springmvc校验需求 springmvc使用hibernate的校验框架v