ssm框架搭建(Spring+spring mvc+ mybatis)

这几天一直想自己搭建一个ssm框架(使用的是eclipse),因为没有自己搭建过,所以真心废了好大劲儿,今天与大家分享下,也记录下,方便以后可以看看(如果各位大拿有任何好的建议,欢迎批评)。中间参考微博链接

https://www.cnblogs.com/cuglkb/p/6734666.html
 
 
先贴一下框架内容吧

其实整体结构我相信大家都会写,最主要的就是配置文件的那部分和jar包(我会放到网盘,供大家下载,还是比较全的)

jar包链接链接: https://pan.baidu.com/s/1kVqvs1L 密码: yfu4

 jdbc.properties:

#jdbc config
jdbc.driver = com.mysql.jdbc.Driver
#jdbc.url = jdbc:mysql://localhost:3306/mybatis
jdbc.url = jdbc\:mysql\://localhost\:3306/yxx?useUnicode\=true&characterEncoding\=gbk  
jdbc.username = root
jdbc.password = 960530

 

mybatis-config.xml:

<?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>
    <!-- typeAliases 类型别名是为 Java 类型设置一个短的名字。它只和 XML 配置有关,存在的意义仅在于用来减少类完全限定名的冗余。 -->
    <typeAliases>
        <typeAlias alias="User" type="com.model.User"/>
    </typeAliases>
    <!-- mappers 告诉 MyBatis 到哪里去找映射文件 -->
    <mappers>
        <mapper resource="com/mapper/userMapper.xml" />
    </mappers>
</configuration>

 

spring-common.xml:

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
 
  <!-- 引入配置文件 -->
  <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:config/jdbc.properties"/>
  </bean>
 
    <!-- 1. 数据源 : DriverManagerDataSource -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>  
    
    <!--2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:
        引用数据源MyBatis定义数据源,同意加载配置-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:config/mybatis-config.xml" />
    </bean>

    <!--3. mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory
        basePackage:指定sql映射文件/接口所在的包(自动扫描)    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mapper"></property>
        <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> -->  <!-- 会引起无法加载配置文件 -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    
    <!--4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源    -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 5. 使用声明式事务: transaction-manager:引用上面定义的事务管理器 -->
    <tx:annotation-driven transaction-manager="txManager" />

</beans>

 

spring-mvc.xml:

<?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: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-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
    <!-- 注解扫描包 -->
    <context:component-scan base-package="com" />

    <!-- 开启注解 -->
    <mvc:annotation-driven />
    
    <!--  配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理 -->
    <mvc:resources mapping="/img/**" location="/img/" />
    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/html/**" location="/html/" />
    
    <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />    
    </bean>
    
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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" id="WebApp_ID" version="3.0">
     
     <!-- 加载Spring容器配置 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
     
    <!-- 设置Spring容器加载所有的配置文件的路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:config/spring-*.xml</param-value>
    </context-param>

    <context-param>  
        <param-name>webAppRootKey</param-name>    
        <param-value>webapp.root</param-value>    
    </context-param>  
    <!-- 设置log4j配置文件的路径 -->
      <context-param>  
        <param-name>log4jConfigLocation</param-name>  
        <!--  <param-value>classpath*:config/log4j.properties</param-value>  -->
        <param-value>classpath:config/log4j.properties</param-value>  
        <!--  <param-value>WEB-INF/log4j.properties</param-value> -->
    </context-param>  
    <!-- 3000表示 开一条watchdog线程每60秒扫描一下配置文件的变化;这样便于日志存放位置的改变 -->  
    <context-param>    
        <param-name>log4jRefreshInterval</param-name>    
        <param-value>3000</param-value>    
    </context-param>   
    <listener>  
           <listener-class>org.springframework.web.util.Log4jConfigListener</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*:config/spring-mvc.xml</param-value>
        </init-param>
        <!-- 启动加载 -->  
        <load-on-startup>1</load-on-startup>
     </servlet>
 
    <!--为DispatcherServlet建立映射 -->
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <!-- 此处可以可以配置成*.do -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 
    <!-- 防止Spring内存溢出监听器 -->
    <listener>
         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    
   <!-- 解决工程编码过滤器 -->
   <filter>
        <filter-name>encodingFilter</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
     
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

这些主要是配置文件,其他的Java文件内容太多,就不上传了。这也是我第一次,多多包涵

 

 
posted @ 2017-12-06 16:49  慕妍  阅读(205)  评论(0)    收藏  举报