ssm框架搭建

Posted wu1ao2ya3

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssm框架搭建相关的知识,希望对你有一定的参考价值。

技术分享图片2019-01-03技术分享图片技术分享图片

第一步

准备环境:idea,maven,jdk,tomcat,mysql,oracle

以上软件的安装配置不再赘述。

第二步

创建项目,添加maven管理jar包

 

引入jar包到pom文件

 

由于oralce连接数据库的驱动jar包maven没有,需要手动引入

pom文件的jar:

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.2</version>
</dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.9.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
  <groupId>aopalliance</groupId>
  <artifactId>aopalliance</artifactId>
  <version>1.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
  <groupId>commons-logging</groupId>
  <artifactId>commons-logging</artifactId>
  <version>1.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
  <groupId>commons-dbcp</groupId>
  <artifactId>commons-dbcp</artifactId>
  <version>1.4</version>
</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.46</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.25</version>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.7.12</version>
</dependency>

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>2.10.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.47</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.3.3</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.0.6</version>
</dependency>

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
  <version>1.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle/ojdbc6 -->
<dependency>
  <groupId>com.oracle</groupId>
  <artifactId>ojdbc6</artifactId>
  <version>11.1.0.6.0</version>
  <scope>system</scope>
  <systemPath>${basedir}/web/WEB-INF/lib/ojdbc6-11.1.0.6.0.jar</systemPath>
</dependency>

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.4.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.1</version>
</dependency>

第三步

 

搭建项目框架

 

(导入web支持,spring支持和mybatis支持会自动导入)

技术分享图片

技术分享图片

 第四步

配置相关配置文件

配置文件:

applicationContent.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:aop="http://www.springframework.org/schema/aop"

   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.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:annotation-config></context:annotation-config>
<!--定义注解扫描的包-->
<context:component-scan base-package="com.qy.mapper"></context:component-scan>
<context:component-scan base-package="com.qy.service"></context:component-scan>
<context:component-scan base-package="com.qy.domain"></context:component-scan>
<!--开启aop自动代理模式-->
<aop:aspectj-autoproxy proxy-target-class="true"/>

<!--从上下文环境中读取db.properties配置文件-->
<context:property-placeholder location="classpath*:db.properties"></context:property-placeholder>

<bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${oracle.driverClassName}"></property>
    <property name="url" value="${oracle.url}"></property>
    <property name="username" value="${oracle.username}"></property>
    <property name="password" value="${oracle.password}"></property>
    <property name="maxActive" value="${oracle.maxActive}"></property>
    <property name="initialSize" value="${oracle.initialSize}"></property>
</bean>

<!--通过mybatis和spring结合的类,创建sqlsessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!--指定myabtis配置文件位置-->
    <property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- scan for mappers and let them be autowired
MapperScannerConfigurer Mybatis-Spring 会自动为我们注册Mapper对应的MapperFactoryBean对象-->
<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.qy.mapper" />
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<!-- 配置事务属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="save*"    propagation="REQUIRED"/>
        <tx:method name="update*"  propagation="REQUIRED"/>
        <tx:method name="delete*"  propagation="REQUIRED"/>
        <tx:method name="*" propagation="NOT_SUPPORTED"></tx:method>
    </tx:attributes>
</tx:advice>

<!-- 配置事务切入点 -->
<aop:config>
    <aop:pointcut expression="execution(* com.qy.service.impl.*.*(..))" id="pointCut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
</aop:config>
</beans>

db.properties 数据库配置文件

mysql.driverClassName=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/java1807
mysql.username=root
mysql.password=123456
mysql.maxActive=100
mysql.initialSize=5

oracle.driverClassName=oracle.jdbc.OracleDriver
oracle.url=jdbc:oracle:thin:@localhost:1521:orcl
oracle.username=scott
oracle.password=tiger
oracle.maxActive=100
oracle.initialSize=5

log4j2.xml 日志配置文件

(需要在mybatis-config中配置setting)

 

<!--使用log4j2 配置输出-->
<setting name="logImpl" value="LOG4J2"/>
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="Console"/>
        </Root>
        <Logger name="com.qy.mapper" level="trace" additivity="false">
            <AppenderRef ref="Console"/>
        </Logger>

    </Loggers>
</Configuration>

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>

    <settings>
        <!--是否使用缓存 开发中禁用-->
        <setting name="cacheEnabled" value="false"/>
        <!--下划线和驼峰式命名法的匹配  -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--使用启用懒加载机制  true启用-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="multipleResultSetsEnabled" value="true"/>
        <setting name="useColumnLabel" value="true"/>
        <setting name="useGeneratedKeys" value="true"/>
        <setting name="autoMappingBehavior" value="PARTIAL"/>
        <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
        <setting name="defaultExecutorType" value="SIMPLE"/>
        <setting name="defaultStatementTimeout" value="25"/>
        <setting name="defaultFetchSize" value="100"/>
        <setting name="safeRowBoundsEnabled" value="false"/>
        <setting name="localCacheScope" value="SESSION"/>
        <setting name="jdbcTypeForNull" value="OTHER"/>
        <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
    </settings>

    <typeAliases>
        <!--给类起别名-->
        <!--<typeAlias type="com.qy.domain.Question" alias="Question"></typeAlias>-->
        <!--给包中所有的类起别名 默认名字为类名 -->
        <package name="com.qy.domain"></package>
    </typeAliases>

    <mappers>
        <!--引入mapper映射文件的位置-->
       <!-- <mapper resource="mapper/UserMapper.xml"/>
        <mapper resource="com/qy/mapper/QuestionMapper.xml"/>
        <mapper resource="com/qy/mapper/DeptMapper.xml"/>
        <mapper resource="com/qy/mapper/EmpMapper.xml"/>-->
    </mappers>


</configuration>

springmvc-servlet.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: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">

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--指定视图的前缀和后缀,Controller返回的String类型与这里的前后缀拼接,构成返回的视图页面地址-->
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--开启注解扫描功能-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 处理请求返回json字符串的中文乱码问题 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- 解决Controller返回json中文乱码问题 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <!-- <property name="supportedMediaTypes" value="text/html;charset=UTF-8" /> -->
                <!-- <property name="supportedMediaTypes" value="application/json;charset=UTF-8" > -->
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>

            <!-- fastJson配置 -->
            <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/json;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>

        </mvc:message-converters>
    </mvc:annotation-driven>


    <!--定义注解扫描的包-->
    <context:component-scan base-package="com.qy.controller"></context:component-scan>

    <!--处理静态资源-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>

    <!--将链接中的静态的访问路径映射为URL,常用于加载html、js、css、图片、视频等静态资源-->
   <!-- <mvc:resources mapping="/js/**" location="/background/js/"/>
    <mvc:resources mapping="/images/**" location="/background/images/"/>
    <mvc:resources mapping="/css/**" location="/background/css/"/>
    <mvc:resources mapping="/fonts/**" location="/background/fonts/"/>
    <mvc:resources mapping="/lib/**" location="/background/lib/"/>-->

    <!--登录拦截器-->
  <!--  <mvc:interceptors>
        <mvc:interceptor>
            &lt;!&ndash; 拦截所有URL中包含/user/的请求 &ndash;&gt;
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/**/fonts/**"/>
            <mvc:exclude-mapping path="/**/css/**"/>
            <mvc:exclude-mapping path="/**/js/**"/>
            <mvc:exclude-mapping path="/**/images/**"/>
            <mvc:exclude-mapping path="/**/lib/**"/>
            <mvc:exclude-mapping path="/**/login.html"/>
            <mvc:exclude-mapping path="/user/toLogin"/>
            <bean class="com.qy.controller.LoginInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>-->

    <!--springmvc定义好的,用来处理上传文件的类-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置上传文件的最大尺寸为1MB -->
        <property name="maxUploadSize">
            <value>1048576</value>
        </property>
    </bean>

 </beans>

web.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!--filter 过滤器 用来解决中文乱码问题-->
    <filter>
        <filter-name>characterEncoding</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>characterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>default1</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>default1</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--配置spring-->
    <!-- 配置去哪裡加載spring的配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- 配置spring的監聽器,加載資源初始化文件 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

第五步

java目录下的mapper层代替了原来的Dao层(写接口)

原来dao层实现类变成了resources目录下相应级目录的***Mapper.xml

Mapper.xml里面写mybatis特有的crue(sql)语句。

 

 

大功告成!

以上是关于ssm框架搭建的主要内容,如果未能解决你的问题,请参考以下文章

SSM 项目整合详细解读

SSM-框架搭建-tank后台学习系统

[转]SSM(Spring+SpringMVC+Mybatis)框架搭建详细教程附源代码Demo

ssm框架的搭建

IDEA 使用IDEA搭建SSM框架项目

使用IDEA搭建SSM框架