目录


环境

配置说明

所需jar包

配置db.properties

配置log4j.properties

配置spring.xml

配置mybatis-spring.xml

配置springmvc.xml

配置web.xml

测试


环境

  操作系统:Ubuntu 18.04

  开发工具:Eclipse

  Java版本:Java 8

  服务器:Tomcat 8

  Spring版本:4.1.6

  Mybatis版本:3.2.7

  配置方式:使用手动导入jar包的方式

配置说明

  正如标题所说的,本篇博客是使用xml方式,基于Spring4 + Mybatis3搭建的一套简单的ssm环境,此次搭建环境没有使用Maven,如果想要查看使用Maven怎么搭建SSM整合环境,可以参考:使用Maven完成SSM框架整合环境构建——基于Spring4和Mybatis3

  因为Spring MVC只是Spring的一个子产品,所以并不需要整合Spring和SpringMVC,实际上,SSM的整合只是Spring和Mybatis,以及SpringMVC和Mybatis之间的整合。

  本篇博客的源码已经上传到github,地址:https://github.com/searchingbeyond/ssm-simple-config.git

  最基本的ssm环境:click here

  后续会在这个基础上增加一些其他框架的整合。

  如果要想用maven搭建环境,可以参考:https://www.cnblogs.com/-beyond/p/10766468.html

所需jar包

  下面是环境搭建需要的jar包,可以直接clone上面的git库,这些jar包我都已经上传到lib了。

asm-3.3.1.jar
cglib-2.2.2.jar
javassist-3.17.1-GA.jar commons-fileupload-1.3.1.jar
commons-io-2.6.jar
commons-logging-1.1.3.jar jackson-annotations-2.4.0.jar
jackson-core-2.4.1.jar
jackson-databind-2.4.1.jar log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar mysql-connector-java-5.1.39-bin.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.3.jar
druid-1.0.16.jar spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar aopalliance.jar
aspectjweaver.jar jstl-1.2.jar
taglibs-standard-1.1.2.jar

  

配置db.properties

  db.properties文件存放在项目的src目录下,保存数据库连接信息以及连接池相关配置。

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123456
jdbc.maxActive=10
jdbc.initialSize=5
jdbc.maxWait=10000
jdbc.minIdle=5  

配置log4j.properties

  log4j.properties存放在项目的src目录下。用来记录日志,主要是配置日志的记录等级,以及日志输出文件的位置。

  如果是Windows操作系统,修改日志输出文件的录几路径即可。

log4j.rootCategory=INFO, CONSOLE,LOGFILE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=-%p - %d{yyyy-MM-dd HH:mm:ss} - %C - line:%L - %m%n log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=/var/work/log/server.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=-%p - %d{yyyy-MM-dd HH:mm:ss} - %C - line:%L - %m%n

  

配置spring.xml

  spring.xml存放在项目的src目录下,用来配置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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 注解扫描 -->
<context:component-scan base-package="cn.ganlixin.dao"></context:component-scan>
<context:component-scan base-package="cn.ganlixin.service"></context:component-scan> <!-- 加载属性文件 -->
<context:property-placeholder location="classpath:db.properties" /> <!-- 配置数据源 -->
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
--> <!-- 使用Druid连接池,配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="initialSize" value="${jdbc.initialSize}"></property>
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="maxWait" value="${jdbc.maxWait}"></property>
<property name="minIdle" value="${jdbc.minIdle}"></property>
</bean> <!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-spring.xml"></property>
<property name="mapperLocations" value="classpath:cn/ganlixin/mapper/*.xml"></property>
</bean> <!-- 扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.ganlixin.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> <!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 声明式事务 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="ins*"/>
<tx:method name="del*"/>
<tx:method name="upd*"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice> <!-- 配置aop,将定义的切点与声明式事务绑定 -->
<aop:config>
<aop:pointcut expression="execution(* cn.ganlixin.service.*.*(..))" id="mypoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
</aop:config>
</beans>

  

配置mybatis-spring.xml

  mybatis-spring.xml这个文件存放在项目的src目录下,其实这配置文件可以省略的,完全可以在spring.xml中配置。

  这个文件单纯的用来配置mybatis,但是spring.xml中已经配置了很多关于mybatis的配置,所以mybatis并不需要做什么。

<?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>
<settings>
<!-- 使用log4j记录日志 -->
<setting name="logImpl" value="log4J"></setting>
</settings> <!-- 设置类型别名 -->
<typeAliases>
<package name="cn.ganlixin.pojo" />
</typeAliases>
</configuration>

  

配置springmvc.xml

  springmvc同样是存放在项目的src目录下。

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 扫描直接,只扫描controller包 -->
<context:component-scan
base-package="cn.ganlixin.controller"></context:component-scan> <!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!-- 注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven> <!-- 设置静态资源路径 -->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
</beans>

  

配置web.xml

  web.xml存放在项目的WebRoot/WEB-INF目录下,如果是myeclipse,应该存放到WebContent/WEB-INF下。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param> <!-- 监听器 -->
<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>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- 字符编码过滤器 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

  

测试

  这里就不进行测试了,可以下载 demo,包含一个完整的示例,自己可以运行一下。

SSM框架整合环境构建——基于Spring4和Mybatis3的更多相关文章

  1. 使用Maven完成SSM框架整合环境构建——基于Spring4和Mybatis3

    只言片语 使用Maven来搭建一个SSM环境,其实和使用手工倒入jar的过程没有多大区别,所用的jar包都是一样的,但是区别在与不用你手动导入jar包了,而是只修改pom.xml,maven会自动根据 ...

  2. 基于maven的ssm框架整合

    基于maven的ssm框架整合 第一步:通过maven建立一个web项目.                第二步:pom文件导入jar包                              (1 ...

  3. IDEA下基于MAVEN的SSM框架整合

    源码可以以上传github https://github.com/ingxx/ssm_first 最近把用IDEA把SSM框架整合一遍遇到了不少坑,在这里写出来 这里maven我使用的是自己下载的3. ...

  4. JAVAEE——宜立方商城01:电商行业的背景、商城系统架构、后台工程搭建、SSM框架整合

    1. 学习计划 第一天: 1.电商行业的背景. 2.宜立方商城的系统架构 a) 功能介绍 b) 架构讲解 3.工程搭建-后台工程 a) 使用maven搭建工程 b) 使用maven的tomcat插件启 ...

  5. SSM框架整合模板

    SSM框架整合--MAVEN依赖 spring方面(包含了springmvc): spring-webmvc:spring与mvc的整合依赖,主要包括spring的核心包和springmvc需要的包 ...

  6. SpringMVC详解及SSM框架整合项目

    SpringMVC ssm : mybatis + Spring + SpringMVC MVC三层架构 JavaSE:认真学习,老师带,入门快 JavaWeb:认真学习,老师带,入门快 SSM框架: ...

  7. SSM框架整合(实现从数据库到页面展示)

    SSM框架整合(实现从数据库到页面展示) 首先创建一个spring-web项目,然后需要配置环境dtd文件的引入,环境配置,jar包引入. 首先让我来看一下ssm的基本项目配件.(代码实现) 1.首先 ...

  8. SpringMVC札集(10)——SSM框架整合

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  9. (转)淘淘商城系列——SSM框架整合之Dao层整合

    http://blog.csdn.net/yerenyuan_pku/article/details/72721093 一个项目中往往有三层即Dao层.Service层和Web层,看标题就知道了,本文 ...

随机推荐

  1. 如何在ASP.NET Core程序启动时运行异步任务(1)

    原文:Running async tasks on app startup in ASP.NET Core (Part 1) 作者:Andrew Lock 译者:Lamond Lu 背景 当我们做项目 ...

  2. MySQL系列--3.数据类型和连接查询

    1.存储引擎 数据创建,查询,更新和删除操作都是通过数据引擎来进行的.不同的存储引擎存储限制不同,支持不同的索引机制等. 查询数据库支持的存储引擎 MySQL 5.7.2支持的存储引擎有:InnoDB ...

  3. 学习ASP.NET Core Razor 编程系列十一——把新字段更新到数据库

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  4. Service Worker

    Service Worker 随着前端快速发展,应用的性能已经变得至关重要,关于这一点大佬做了很多统计.你可以去看看. 如何降低一个页面的网络请求成本从而缩短页面加载资源的时间并降低用户可感知的延时是 ...

  5. Python:鲜为人知的功能特性(下)

    GitHub 上有一个名为<What the f*ck Python!>的项目,这个有趣的项目意在收集 Python 中那些难以理解和反人类直觉的例子以及鲜为人知的功能特性,并尝试讨论这些 ...

  6. Python编程从入门到实践笔记——函数

    Python编程从入门到实践笔记——函数 #coding=gbk #Python编程从入门到实践笔记——函数 #8.1定义函数 def 函数名(形参): # [缩进]注释+函数体 #1.向函数传递信息 ...

  7. ACCP8.0 HTML标签

    ACCP8.0 HTML标签 第一章1.HTML超文本标记语言2.网页<html></html>3.网页头部<head></head>4.网页标题< ...

  8. c#进阶一:使用ILDASM来查看c#中间语言

    平时工作的时候总是使用ctrl c+ctrl v去快速开发实现业务功能,但是在工作之余,我们也应该要注意静下心来去学习和提高自己.进阶的文章随性来写,不定时更新.希望可以和大家共同学习,共同进步.今天 ...

  9. 折腾Java设计模式之迭代器模式

    迭代器模式 Provide a way to access the elements of an aggregate object sequentially without exposing its ...

  10. Django学习之八:forms组件【对form舒心了】

    目录 Django forms组件 bound and unbound form instance forms渲染有关 隐藏一个字段,不渲染它 form 校验 form类 ModelForm 利用Mo ...