Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】

关于Spring+SpringMVC+Mybatis 整合,见还有不少初学者一头雾水,于是写篇教程,初学者按部就班的来一次,可能就会少走不少弯路了。

一:框架介绍(啰嗦两句,可自行度娘)

1.1:Spring

Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。

1.2:SpringMVC

Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。

1.3:Mybatis

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

二: 项目创建(Maven Web)

项目创建教程:http://www.zuidaima.com/share/2635756355570688.htm

三:SSM整合

下面主要介绍三大框架的整合。这次整合我分了2个配置文件,分别是spring-mybatis.xml,包含spring和mybatis的配置文件,还有个是spring-mvc的配置文件,此外有2个资源文件:jdbc.propertis和log4j.properties。完整目录结构如下:

《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》

3.1:Maven引入需要的JAR包

为了方便后面说的时候不需要引入JAR包,我这里直接给出所有需要的JAR包,这都是基本的JAR包,每个包的是干什么的都有注释,就不再多说了。

3.2:Spring与MyBatis的整合

所有需要的JAR包都引入以后,首先进行Spring与MyBatis的整合,然后再进行JUnit测试,先看一个项目结构图:

3.2.1:建立JDBC属性文件

文件:jdbc.properties(文件编码修改为utf-8

#本地

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://127.0.0.1:3307/DB_shq?useUnicode=true&characterEncoding=UTF-8

username=root

password=

#定义初始连接数

initialSize=0

#定义最大连接数

maxActive=20

#定义最大空闲

maxIdle=20

#定义最小空闲

minIdle=1

#定义最长等待时间

maxWait=60000

3.2.2:建立spring-mybatis.xml配置文件

文件:spring-mybatis.xm

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:p=”http://www.springframework.org/schema/p”

xmlns:context=”http://www.springframework.org/schema/context”

xmlns:mvc=”http://www.springframework.org/schema/mvc”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd”>

class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>

destroy-method=”close”

class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>

3.2.3:Log4j的配置

文件:log4j.properties

#定义LOG输出级别

log4j.rootLogger=INFO,Console,File

#定义日志输出目的地为控制台

log4j.appender.Console=org.apache.log4j.ConsoleAppender

log4j.appender.Console.Target=System.out

#可以灵活地指定日志输出格式,下面一行是指定具体的格式

log4j.appender.Console.layout = org.apache.log4j.PatternLayout

log4j.appender.Console.layout.ConversionPattern=[%c] – %m%n

#文件大小到达指定尺寸的时候产生一个新的文件

log4j.appender.File = org.apache.log4j.RollingFileAppender

#指定输出目录

log4j.appender.File.File = logs/weixin_springmvcMybist/ssm.log

#定义文件最大大小

log4j.appender.File.MaxFileSize = 10MB

#输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志

log4j.appender.File.Threshold = ALL

log4j.appender.File.layout = org.apache.log4j.PatternLayout

log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

3.2.4:JUnit测试

经过以上步骤(到4.2.2,log4j不配也没影响),我们已经完成了Spring和mybatis的整合,这样我们就可以编写一段测试代码来试试是否成功了。

《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》

3.2.4:创建测试用表

DROP TABLE IF EXISTS `user_t`;

CREATE TABLE `user_t` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`user_name` varchar(40) NOT NULL,

`password` varchar(255) NOT NULL,

`age` int(4) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

/*Data for the table `user_t` */

insert  into `user_t`(`id`,`user_name`,`password`,`age`) values (1,’测试’,’sfasgfaf’,24);

3.2.5:利用MyBatis Generator自动创建代码

之前发过教程(不会的可以看看啊):http://www.zuidaima.com/share/2148093791816704.htm

把生产的文件拷贝到项目中对应的包中即可

《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》

3.2.6:建立Service接口和实现类

具体代码:

IUserService:

package com.shq.service;

import com.shq.pojo.Tb_user;

/**

* @author  作者:Shengke

* @date 创建时间:2015年11月26日 下午1:21:35

* @version 1.0

*/

public interface IUserService {

public Tb_user getUserById(int userId);

}

UserService:

package com.shq.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.shq.dao.Tb_userMapper;

import com.shq.pojo.Tb_user;

import com.shq.service.IUserService;

/**

* @author  作者:Shengke

* @date 创建时间:2015年11月26日 下午1:54:44

* @version 1.0

*/

@Service(“userService”)

public class UserService implements IUserService {

@Resource

private Tb_userMapper userDao;

public Tb_user getUserById(int userId) {

// TODO Auto-generated method stub

return this.userDao.selectByPrimaryKey(userId);

}

}

《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》

3.2.7:建立测试类(JUnit4)

在src/test/java中建立测试类,如果测试成功,表示Spring和Mybatis已经整合成功了。输出信息使用的是Log4j打印到控制台。

[org.springframework.beans.factory.xml.XmlBeanDefinitionReader] – Loading XML bean definitions from class path resource [spring-mybatis.xml]

[org.springframework.context.support.GenericApplicationContext] – Refreshing org.springframework.context.support.GenericApplicationContext@14b25f37: startup date [Thu Dec 10 09:43:44 CST 2015]; root of context hierarchy

[org.springframework.beans.factory.config.PropertyPlaceholderConfigurer] – Loading properties file from class path resource [jdbc.properties]

[org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] – JSR-330 ‘javax.inject.Inject’ annotation found and supported for autowiring

[com.shq.test.Test] – {“age”:12,”id”:1,”name”:”shengke”,”pass”:”1q2w3e”}

[org.springframework.context.support.GenericApplicationContext] – Closing org.springframework.context.support.GenericApplicationContext@14b25f37: startup date [Thu Dec 10 09:43:44 CST 2015]; root of context hierarchy

《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》

完成Spring和mybatis这两大框架的整合,下面在继续进行SpringMVC的整合

3.2.8:整合SpringMVC

上面已经完成了2大框架的整合,SpringMVC的配置文件单独放,然后在web.xml中配置整合。

文件:spring-mvc.xml

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:p=”http://www.springframework.org/schema/p”

xmlns:context=”http://www.springframework.org/schema/context”

xmlns:mvc=”http://www.springframework.org/schema/mvc”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd”>

class=”org.springframework.http.converter.json.MappingJacksonHttpMessageConverter”>

text/html;charset=UTF-8

class=”org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”>

    

–>

class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”>

3.2.9:配置web.xml文件

文件:web.xml

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_3_0.xsd”

version=”3.0″>

Archetype Created Web Application

contextConfigLocation

classpath:spring-mybatis.xml

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

true

encoding

UTF-8

encodingFilter

/*

org.springframework.web.context.ContextLoaderListener

org.springframework.web.util.IntrospectorCleanupListener

SpringMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

true

SpringMVC

/

/index.jsp

3.2.10:测试

至此已经完成了SSM三大框架的整合了,接下来测试一下,如果成功了,那么恭喜你,如果失败了,继续调试吧!

3.2.11:新建jsp页面

根据spring-mvc.xml的配置在 WEB-INF/jsp/下建立User.jsp

文件:User.jsp

<%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8″%>

测试

${user.userName}

《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》

SSM三大框架的整合就完成了,在此基础上可再添加其他功能

《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》

核心技术:Maven,Springmvc mybatis shiro, Druid, Restful, Dubbo, ZooKeeper,Redis,FastDFS,ActiveMQ,Nginx

1.     项目核心代码结构截图

《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》

项目模块依赖

《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》

特别提醒:开发人员在开发的时候可以将自己的业务REST服务化或者Dubbo服务化

2.    项目依赖介绍

2.1 后台管理系统、Rest服务系统、Scheculer定时调度系统依赖如下图:

《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》

2.2 Dubbo独立服务项目依赖如下图:

《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》

3.  项目功能部分截图:

《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》
《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》
《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》
《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》
《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》

《Spring+Spring MVC+MyBatis实现SSM框架整合详细教程【转】》

    原文作者:mingli_jianshu1
    原文地址: https://www.jianshu.com/p/4279a4e4fa78
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞