本文是基于xml配置的方式来整合SpringMVC、Spring和Mybatis(基于注解的方式会再写一篇文章),步骤如下:
(1)首先自然是依赖包的配置文件 pom.xml
<project xmlns&#61;"http://maven.apache.org/POM/4.0.0" xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0modelVersion><groupId>com.xuebusigroupId><artifactId>xssmartifactId><packaging>warpackaging><version>1.0-SNAPSHOTversion><name>ssm Maven Webappname><url>http://maven.apache.orgurl><properties><generator.version>1.3.2generator.version><junit.version>4.12junit.version><spring.version>4.1.3.RELEASEspring.version><mybatis.version>3.2.8mybatis.version><mybatis.spring.version>1.2.2mybatis.spring.version><mybatis.paginator.version>1.2.15mybatis.paginator.version><mysql.version>5.1.32mysql.version><slf4j.version>1.6.4slf4j.version><jackson.version>2.4.2jackson.version><druid.version>1.0.9druid.version><httpclient.version>4.3.5httpclient.version><jstl.version>1.2jstl.version><servlet-api.version>2.5servlet-api.version><jsp-api.version>2.0jsp-api.version><joda-time.version>2.5joda-time.version><commons-lang3.version>3.3.2commons-lang3.version><commons-io.version>1.3.2commons-io.version><commons-net.version>3.3commons-net.version><pagehelper.version>3.4.2pagehelper.version><jsqlparser.version>0.9.1jsqlparser.version><commons-fileupload.version>1.3.1commons-fileupload.version><jedis.version>2.7.2jedis.version><solrj.version>4.10.3solrj.version>properties><dependencies><dependency><groupId>org.mybatis.generatorgroupId><artifactId>mybatis-generator-coreartifactId><version>${generator.version}version>dependency><dependency><groupId>joda-timegroupId><artifactId>joda-timeartifactId><version>${joda-time.version}version>dependency><dependency><groupId>org.apache.commonsgroupId><artifactId>commons-lang3artifactId><version>${commons-lang3.version}version>dependency><dependency><groupId>org.apache.commonsgroupId><artifactId>commons-ioartifactId><version>${commons-io.version}version>dependency><dependency><groupId>commons-netgroupId><artifactId>commons-netartifactId><version>${commons-net.version}version>dependency><dependency><groupId>com.fasterxml.jackson.coregroupId><artifactId>jackson-databindartifactId><version>${jackson.version}version>dependency><dependency><groupId>org.apache.httpcomponentsgroupId><artifactId>httpclientartifactId><version>${httpclient.version}version>dependency><dependency><groupId>org.slf4jgroupId><artifactId>slf4j-log4j12artifactId><version>${slf4j.version}version>dependency><dependency><groupId>org.mybatisgroupId><artifactId>mybatisartifactId><version>${mybatis.version}version>dependency><dependency><groupId>org.mybatisgroupId><artifactId>mybatis-springartifactId><version>${mybatis.spring.version}version>dependency><dependency><groupId>com.github.miemiedevgroupId><artifactId>mybatis-paginatorartifactId><version>${mybatis.paginator.version}version>dependency><dependency><groupId>com.github.pagehelpergroupId><artifactId>pagehelperartifactId><version>${pagehelper.version}version>dependency><dependency><groupId>mysqlgroupId><artifactId>mysql-connector-javaartifactId><version>${mysql.version}version>dependency><dependency><groupId>com.alibabagroupId><artifactId>druidartifactId><version>${druid.version}version>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-contextartifactId><version>${spring.version}version>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-beansartifactId><version>${spring.version}version>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-webmvcartifactId><version>${spring.version}version>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-jdbcartifactId><version>${spring.version}version>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-aspectsartifactId><version>${spring.version}version>dependency><dependency><groupId>jstlgroupId><artifactId>jstlartifactId><version>${jstl.version}version>dependency><dependency><groupId>javax.servletgroupId><artifactId>servlet-apiartifactId><version>${servlet-api.version}version><scope>providedscope>dependency><dependency><groupId>javax.servletgroupId><artifactId>jsp-apiartifactId><version>${jsp-api.version}version><scope>providedscope>dependency><dependency><groupId>commons-fileuploadgroupId><artifactId>commons-fileuploadartifactId><version>${commons-fileupload.version}version>dependency><dependency><groupId>redis.clientsgroupId><artifactId>jedisartifactId><version>${jedis.version}version>dependency><dependency><groupId>org.apache.solrgroupId><artifactId>solr-solrjartifactId><version>${solrj.version}version>dependency><dependency><groupId>junitgroupId><artifactId>junitartifactId><version>${junit.version}version><scope>testscope>dependency>dependencies><build><resources><resource><directory>src/main/javadirectory><includes><include>**/*.propertiesinclude><include>**/*.xmlinclude>includes><filtering>falsefiltering>resource>resources><plugins><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-resources-pluginartifactId><version>2.7version><configuration><encoding>UTF-8encoding>configuration>plugin><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-compiler-pluginartifactId><version>3.2version><configuration><source>1.7source><target>1.7target><encoding>UTF-8encoding>configuration>plugin>plugins><pluginManagement><plugins><plugin><groupId>org.apache.tomcat.mavengroupId><artifactId>tomcat7-maven-pluginartifactId><version>2.2version>plugin>plugins>pluginManagement>build>
project>
&#xff08;2&#xff09;既然是web项目&#xff0c;首先得配置web.xml
xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<web-app xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xmlns&#61;"http://java.sun.com/xml/ns/javaee"xsi:schemaLocation&#61;"http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id&#61;"xuebusi" version&#61;"2.5"><display-name>xuebusidisplay-name><welcome-file-list><welcome-file>index.htmlwelcome-file><welcome-file>index.htmwelcome-file><welcome-file>index.jspwelcome-file><welcome-file>default.htmlwelcome-file><welcome-file>default.htmwelcome-file><welcome-file>default.jspwelcome-file>welcome-file-list><context-param><param-name>contextConfigLocationparam-name><param-value>classpath:spring/applicationContext-*.xmlparam-value>context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>listener><filter><filter-name>CharacterEncodingFilterfilter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class><init-param><param-name>encodingparam-name><param-value>utf-8param-value>init-param>filter><filter-mapping><filter-name>CharacterEncodingFilterfilter-name><url-pattern>/*url-pattern>filter-mapping><servlet><servlet-name>dispatcherServletservlet-name><servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class><init-param><param-name>contextConfigLocationparam-name><param-value>classpath:spring/springmvc.xmlparam-value>init-param><load-on-startup>1load-on-startup>servlet><servlet-mapping><servlet-name>dispatcherServletservlet-name><url-pattern>/url-pattern>servlet-mapping>
web-app>
&#xff08;3&#xff09;数据库连接配置 db.properties
jdbc.driver&#61;com.mysql.jdbc.Driver
jdbc.url&#61;jdbc:mysql://localhost:3306/xssm?characterEncoding&#61;utf-8
jdbc.username&#61;root
jdbc.password&#61;root
&#xff08;4&#xff09;Mybatis的配置文件 SqlMapConfig.xml
xml version&#61;"1.0" encoding&#61;"UTF-8" ?>
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><plugins><plugin interceptor&#61;"com.github.pagehelper.PageHelper"><property name&#61;"dialect" value&#61;"mysql"/>plugin>plugins>
configuration>
&#xff08;5&#xff09;springmvc的配置文件 springmvc.xml
xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xmlns:context&#61;"http://www.springframework.org/schema/context"xmlns:mvc&#61;"http://www.springframework.org/schema/mvc"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package&#61;"com.xuebusi.xssm.controller" /><mvc:annotation-driven /><beanclass&#61;"org.springframework.web.servlet.view.InternalResourceViewResolver"><property name&#61;"prefix" value&#61;"/WEB-INF/jsp/" /><property name&#61;"suffix" value&#61;".jsp" />bean><mvc:resources location&#61;"/WEB-INF/css/" mapping&#61;"/css/**"/><mvc:resources location&#61;"/WEB-INF/js/" mapping&#61;"/js/**"/><bean id&#61;"multipartResolver"class&#61;"org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name&#61;"defaultEncoding" value&#61;"UTF-8">property><property name&#61;"maxUploadSize" value&#61;"5242880">property>bean>
beans>
&#xff08;6&#xff09;Spring的配置文件applicationContext-service.xml 扫描&#64;Service注解
xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:context&#61;"http://www.springframework.org/schema/context"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd"><context:component-scan base-package&#61;"com.xuebusi.xssm.service">context:component-scan>
beans>
&#xff08;7&#xff09;Spring的事物配置 applicationContext-trans.xml
xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:aop&#61;"http://www.springframework.org/schema/aop"xmlns:tx&#61;"http://www.springframework.org/schema/tx"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsd"><bean id&#61;"transactionManager" class&#61;"org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name&#61;"dataSource" ref&#61;"dataSource"/>bean><tx:advice id&#61;"txAdvice" transaction-manager&#61;"transactionManager"><tx:attributes><tx:method name&#61;"save*" propagation&#61;"REQUIRED"/><tx:method name&#61;"insert*" propagation&#61;"REQUIRED"/><tx:method name&#61;"add*" propagation&#61;"REQUIRED"/><tx:method name&#61;"create*" propagation&#61;"REQUIRED"/><tx:method name&#61;"delete*" propagation&#61;"REQUIRED"/><tx:method name&#61;"update*" propagation&#61;"REQUIRED"/><tx:method name&#61;"find*" propagation&#61;"SUPPORTS" read-only&#61;"true"/><tx:method name&#61;"select*" propagation&#61;"SUPPORTS" read-only&#61;"true"/><tx:method name&#61;"get*" propagation&#61;"SUPPORTS" read-only&#61;"true"/>tx:attributes>tx:advice><aop:config><aop:advisor advice-ref&#61;"txAdvice" pointcut&#61;"execution(* com.xuebusi.xssm.service.*.*(..))"/>aop:config>
beans>
&#xff08;8&#xff09;Spring的数据源配置文件 applicationContext-dao.xml
xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:context&#61;"http://www.springframework.org/schema/context"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd"><context:property-placeholder location&#61;"classpath:resource/*.properties" /><bean id&#61;"dataSource" class&#61;"com.alibaba.druid.pool.DruidDataSource" destroy-method&#61;"close"><property name&#61;"url" value&#61;"${jdbc.url}" /><property name&#61;"username" value&#61;"${jdbc.username}" /><property name&#61;"password" value&#61;"${jdbc.password}" /><property name&#61;"driverClassName" value&#61;"${jdbc.driver}" /><property name&#61;"maxActive" value&#61;"10" /><property name&#61;"minIdle" value&#61;"5" />bean><bean id&#61;"sqlSessionFactory" class&#61;"org.mybatis.spring.SqlSessionFactoryBean"><property name&#61;"configLocation" value&#61;"classpath:mybatis/SqlMapConfig.xml">property><property name&#61;"dataSource" ref&#61;"dataSource">property>bean><bean class&#61;"org.mybatis.spring.mapper.MapperScannerConfigurer"><property name&#61;"basePackage" value&#61;"com.xuebusi.xssm.mapper">property>bean>
beans>
&#xff08;9&#xff09;redis配置文件 applicationContext-jedis.xml
xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd"><bean id&#61;"jedisPoolConfig" class&#61;"redis.clients.jedis.JedisPoolConfig"><property name&#61;"maxTotal" value&#61;"30" /><property name&#61;"maxIdle" value&#61;"10" /><property name&#61;"numTestsPerEvictionRun" value&#61;"1024" /><property name&#61;"timeBetweenEvictionRunsMillis" value&#61;"30000" /><property name&#61;"minEvictableIdleTimeMillis" value&#61;"1800000" /><property name&#61;"softMinEvictableIdleTimeMillis" value&#61;"10000" /><property name&#61;"maxWaitMillis" value&#61;"1500" /><property name&#61;"testOnBorrow" value&#61;"true" /><property name&#61;"testWhileIdle" value&#61;"true" /><property name&#61;"blockWhenExhausted" value&#61;"false" />bean> <bean id&#61;"redisClient" class&#61;"redis.clients.jedis.JedisCluster"><constructor-arg name&#61;"nodes"><set><bean class&#61;"redis.clients.jedis.HostAndPort"><constructor-arg name&#61;"host" value&#61;"192.168.71.13">constructor-arg><constructor-arg name&#61;"port" value&#61;"7001">constructor-arg>bean><bean class&#61;"redis.clients.jedis.HostAndPort"><constructor-arg name&#61;"host" value&#61;"192.168.71.13">constructor-arg><constructor-arg name&#61;"port" value&#61;"7002">constructor-arg>bean><bean class&#61;"redis.clients.jedis.HostAndPort"><constructor-arg name&#61;"host" value&#61;"192.168.71.13">constructor-arg><constructor-arg name&#61;"port" value&#61;"7003">constructor-arg>bean><bean class&#61;"redis.clients.jedis.HostAndPort"><constructor-arg name&#61;"host" value&#61;"192.168.71.13">constructor-arg><constructor-arg name&#61;"port" value&#61;"7004">constructor-arg>bean><bean class&#61;"redis.clients.jedis.HostAndPort"><constructor-arg name&#61;"host" value&#61;"192.168.71.13">constructor-arg><constructor-arg name&#61;"port" value&#61;"7005">constructor-arg>bean><bean class&#61;"redis.clients.jedis.HostAndPort"><constructor-arg name&#61;"host" value&#61;"192.168.71.13">constructor-arg><constructor-arg name&#61;"port" value&#61;"7006">constructor-arg>bean>set>constructor-arg><constructor-arg name&#61;"poolConfig" ref&#61;"jedisPoolConfig">constructor-arg>bean><bean id&#61;"jedisClientCluster" class&#61;"com.xuebusi.xssm.dao.impl.JedisClientCluster">bean>beans>
&#xff08;10&#xff09;在MySQL中创建一张x_user表
CREATE TABLE &#96;x_user&#96; (&#96;id&#96; int(11) NOT NULL AUTO_INCREMENT,&#96;name&#96; varchar(255) DEFAULT NULL,&#96;address&#96; varchar(255) DEFAULT NULL,&#96;age&#96; int(11) DEFAULT NULL,&#96;phone&#96; varchar(255) DEFAULT NULL,PRIMARY KEY (&#96;id&#96;)
) ENGINE&#61;InnoDB AUTO_INCREMENT&#61;4 DEFAULT CHARSET&#61;utf8;
往表里插入几条测试数据&#xff1a;
&#xff08;11&#xff09;通过mybatis的逆向工程生成x_user表对应的实体类、接口类、Example类以及Mapper.xml配置文件&#xff0c;具体方法移步另一篇文章《mybatis逆向工程自动生成实体类、接口以及映射Mapper.xml配置文件》&#xff0c;地址&#xff1a;http://www.cnblogs.com/jun1019/p/8073229.html
&#xff08;12&#xff09;编写Controller类测试下
package com.xuebusi.xssm.controller;import com.xuebusi.xssm.pojo.XUser;
import com.xuebusi.xssm.service.XUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;/*** Created by SYJ on 2017/12/18.*/
&#64;Controller
&#64;RequestMapping(value &#61; "/user")
public class XUserController {&#64;Autowiredprivate XUserService userService;/*** 根据id查询用户* &#64;param id* &#64;return*/&#64;RequestMapping(value &#61; "/{id}")&#64;ResponseBodypublic XUser get(&#64;PathVariable("id") Integer id) {return userService.selectByPrimaryKey(id);}/*** 查询所有用户* &#64;return*/&#64;RequestMapping(value &#61; "/list")&#64;ResponseBodypublic List
&#xff08;13&#xff09; 编写Service接口&#xff1a;
package com.xuebusi.xssm.service;import com.xuebusi.xssm.pojo.XUser;import java.util.List;/*** Created by SYJ on 2017/12/18.*/
public interface XUserService {XUser selectByPrimaryKey(Integer id);int insert(XUser user);List
}
&#xff08;14&#xff09;编写Service实现类&#xff1a;
package com.xuebusi.xssm.service.impl;import com.xuebusi.xssm.mapper.XUserMapper;
import com.xuebusi.xssm.pojo.XUser;
import com.xuebusi.xssm.service.XUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;/*** Created by SYJ on 2017/12/18.*/
&#64;Service
public class XUserServiceImpl implements XUserService {&#64;Autowiredprivate XUserMapper userMapper;/*** 根据id查询用户* &#64;param id* &#64;return*/&#64;Overridepublic XUser selectByPrimaryKey(Integer id) {XUser user &#61; userMapper.selectByPrimaryKey(id);return user;}/*** 添加用户* &#64;param user* &#64;return*/&#64;Overridepublic int insert(XUser user) {return userMapper.insert(user);}/*** 查询所有用户* &#64;return*/&#64;Overridepublic List
}
&#xff08;15&#xff09;启动项目&#xff0c;通过浏览器访问下&#xff1a;
本文源码&#xff1a; https://github.com/xuebus/xssm