springMVC、spring、Mybatis简单整合

»Spring

整合过程分为:

  1. 导包
  2. springMVC与spring整合
  3. 书写配置文件
    • 书写web.xml:配置springMVC的前端控制器、spring的监听器,包括各自的配置文件位置、还有解决POST提交参数乱码的过滤器(在spring-web包下)
    • 书写springmvc.xml:开启注解扫描、注解驱动、视图解析器(在spring-webmvc包下)
    • 书写application配置文件。暂时不需要写内容。
  4. 测试整合情况
  5. 整合spring和mybatis
    • 书写sqlMapConfig.xml配置文件:暂时不需要写
    • 书写applicationContext.xml:配置连接池、配置sqlSessionFactoryBean(在mybatisspring包),需要注入连接池和configLocation、开启mapper扫描MapperScannerConfigure(在mybatisspring包)、配置事务扫描

项目结构:

image


配置文件

1.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>ssm</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

	<!--配置修改乱码问题的过滤器 -->
	<filter>
		<filter-name>characterFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>characterFilter</filter-name>
		<url-pattern>/ * </url-pattern>
	</filter-mapping>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<!--spring的监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!--springmvc的前端控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

2.applicationContext.xml

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

	<context:property-placeholder location="classpath:dbcp.properties"/>
	<!--配置连接池  -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${dbcp.Driver}"/>
		<property name="url" value="${dbcp.url}"/>
		<property name="username" value="${dbcp.username}"/>
		<property name="password" value="${dbcp.password}"/>
	</bean>

	<!--配置mybatisSqlSession工厂  -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
	</bean>

	<!--配置mapper扫描  -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.ruanwenjun.dao"/>
	</bean>

	<!--配置事务扫描  -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

3.springmvc.xml

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

	<!--开启注解扫描 controller -->
	<context:component-scan base-package="cn.ruanwenjun"/>

	<!--注解驱动,自动加载处理器映射器和处理器适配器  -->
	<mvc:annotation-driven conversion-service="formattingConversionServiceFactoryBean"/>
	<!--参数格式转换-->
	<bean id="formattingConversionServiceFactoryBean"
		class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<property name="converters">
			<list>
				<bean class="cn.ruanwenjun.convertor.DateConvert"></bean>
			</list>
		</property>
	</bean>

	<!--视图解析器  -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>

4.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="cn.ruanwenjun.domain"/>
	</typeAliases>

	<!--包扫描  -->
	<!-- <mappers>
		<package name="cn.ruanwenjun.dao"/>
	</mappers> -->
</configuration>

书写测试代码

package cn.ruanwenjun.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import cn.ruanwenjun.domain.Items;
import cn.ruanwenjun.service.ItemService;

/**
 * @author ruanwenjun E-mail:861923274@qq.com
 * @date 2018年4月8日 下午7:36:33
*/
@Controller
public class ItemController {
	@Autowired
	private ItemService is;

	//商品页面
	@RequestMapping(value="/itemlist.action")
	public ModelAndView itemList() {
		List<Items> itemList = is.findAllItems();


		ModelAndView mav = new ModelAndView();
		mav.setViewName("itemList");
		mav.addObject("itemList", itemList);

		return mav;
	}

	//跳转到修改页面
	@RequestMapping("itemEdit.action")
	public ModelAndView itemEdit(Integer id) {
		Items item = is.selectItemById(id);

		ModelAndView mav = new ModelAndView();
		mav.setViewName("editItem");
		mav.addObject("item", item);
		return mav;
	}
	//修改
	@RequestMapping("updateitem.action")
	public ModelAndView itemUpdate(Items item) {
		is.itemUpdate(item);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("success");
		return mav;
	}
}

package cn.ruanwenjun.service;

import java.util.Date;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.ruanwenjun.dao.ItemsMapper;
import cn.ruanwenjun.domain.Items;
import cn.ruanwenjun.domain.ItemsExample;

/**
 * @author ruanwenjun E-mail:861923274@qq.com
 * @date 2018年4月8日 下午8:28:32
*/
@Service
@Transactional
public class ItemService {

	@Autowired
	private ItemsMapper im ;

	//查询商品列表
	public List<Items> findAllItems(){
		ItemsExample ie = new ItemsExample();
		List<Items> list = im.selectByExampleWithBLOBs(ie);

		return list;
	}
	//通过ID查询商品
	public Items selectItemById(Integer id) {
		Items item = im.selectByPrimaryKey(id);
		return item;
	}
	//修改商品
	public void itemUpdate(Items item) {
		im.updateByPrimaryKeyWithBLOBs(item);
	}
}

package cn.ruanwenjun.convertor;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;

/**
 * @author ruanwenjun E-mail:861923274@qq.com
 * @date 2018年4月9日 下午2:40:29
*/
public class DateConvert implements Converter<String, Date>{

	public Date convert(String source) {
		if(source != null) {
			Format f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			try {
				Date date = (Date) f.parseObject(source);
				return date;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}


基本测试成功