• SSM框架整合环境构建——基于Spring4和Mybatis3


    目录


    环境

    配置说明

    所需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,包含一个完整的示例,自己可以运行一下。

  • 相关阅读:
    java.lang.ClassNotFoundException: org.jaxen.JaxenException
    hdu 4882 ZCC Loves Codefires(贪心)
    C++ STL 源代码学习(之deque篇)
    算法导论学习笔记(2)-归并排序
    机器学习方法:回归(一):线性回归Linear regression
    HDU 2028 Lowest Common Multiple Plus
    C++11新特性应用--实现延时求值(std::function和std::bind)
    大数减法
    hive 运行sqlclient异常
    Oracle 12c agent install for windows
  • 原文地址:https://www.cnblogs.com/-beyond/p/10759625.html
Copyright © 2020-2023  润新知