SSM框架整合环境构建——基于Spring4和Mybatis3
目录
环境
操作系统: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的更多相关文章
- 使用Maven完成SSM框架整合环境构建——基于Spring4和Mybatis3
只言片语 使用Maven来搭建一个SSM环境,其实和使用手工倒入jar的过程没有多大区别,所用的jar包都是一样的,但是区别在与不用你手动导入jar包了,而是只修改pom.xml,maven会自动根据 ...
- 基于maven的ssm框架整合
基于maven的ssm框架整合 第一步:通过maven建立一个web项目. 第二步:pom文件导入jar包 (1 ...
- IDEA下基于MAVEN的SSM框架整合
源码可以以上传github https://github.com/ingxx/ssm_first 最近把用IDEA把SSM框架整合一遍遇到了不少坑,在这里写出来 这里maven我使用的是自己下载的3. ...
- JAVAEE——宜立方商城01:电商行业的背景、商城系统架构、后台工程搭建、SSM框架整合
1. 学习计划 第一天: 1.电商行业的背景. 2.宜立方商城的系统架构 a) 功能介绍 b) 架构讲解 3.工程搭建-后台工程 a) 使用maven搭建工程 b) 使用maven的tomcat插件启 ...
- SSM框架整合模板
SSM框架整合--MAVEN依赖 spring方面(包含了springmvc): spring-webmvc:spring与mvc的整合依赖,主要包括spring的核心包和springmvc需要的包 ...
- SpringMVC详解及SSM框架整合项目
SpringMVC ssm : mybatis + Spring + SpringMVC MVC三层架构 JavaSE:认真学习,老师带,入门快 JavaWeb:认真学习,老师带,入门快 SSM框架: ...
- SSM框架整合(实现从数据库到页面展示)
SSM框架整合(实现从数据库到页面展示) 首先创建一个spring-web项目,然后需要配置环境dtd文件的引入,环境配置,jar包引入. 首先让我来看一下ssm的基本项目配件.(代码实现) 1.首先 ...
- SpringMVC札集(10)——SSM框架整合
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- (转)淘淘商城系列——SSM框架整合之Dao层整合
http://blog.csdn.net/yerenyuan_pku/article/details/72721093 一个项目中往往有三层即Dao层.Service层和Web层,看标题就知道了,本文 ...
随机推荐
- 【Keras篇】---Keras初始,两种模型构造方法,利用keras实现手写数字体识别
一.前述 Keras 适合快速体验 ,keras的设计是把大量内部运算都隐藏了,用户始终可以用theano或tensorflow的语句来写扩展功能并和keras结合使用. 二.安装 Pip insta ...
- 性能测试工具 wrk 使用教程
文章首发自个人微信公众号:小哈学Java 个人网站地址:https://www.exception.site/wrk 被面试官经常问到之前开发的系统接口 QPS 能达到多少,经常给不出一个数值,支支吾 ...
- .NET Core微服务之ASP.NET Core on Docker
Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.Docker极简介绍 1.1 总体介绍 Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.D ...
- Spring Boot 2.0 的快速入门(图文教程)
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! Spring Boot 2.0 的快速入门(图文教程) 大家都 ...
- 利用shell脚本生成CHANGELOG.md(包含git提交规范)
前言 我们经常看到github上面有很多CHANGELOG.MD包含版本的更新信息,如果我们的git提交能遵循一定的规范,那么使用gitlog就能很方便的生成它 生成结果  shell脚本 http ...
- Spring Boot 2.X 如何优雅的解决跨域问题?
一.什么是源和跨域 源(origin)就是协议.域名和端口号.URL由协议.域名.端口和路径组成,如果两个URL的协议.域名和端口全部相同,则表示他们同源.否则,只要协议.域名.端口有任何一个不同,就 ...
- 并发concurrent---3
背景:并发知识是一个程序员段位升级的体现,同样也是进入BAT的必经之路,有必要把并发知识重新梳理一遍. ConcurrentHashMap:在有了并发的基础知识以后,再来研究concurrent包.普 ...
- win10安装tensorflow-gpu1.13.1+cuda10.0+cudnn7.3.1
一,本机配置 Win10 64bit NVIDIA GeForce GTX 960M Python3.7(Anaconda) 二,安装CUDA 亲测,TensorFlow-gpu1.13.1支持cud ...
- C# xml序列化与反序列化 特性的使用
以下为将被序列化的类Entity: [XmlRoot("Root")] public class Entity { [XmlAttribute(AttributeName = &q ...
- 解决vue数据渲染过程中的闪动问题
关键代码 主要解决vue双大括号{{}}在数据渲染和加载过程中的闪动问题,而影响客服体验. html代码: <span class="tableTitle selftab" ...